Our previous system to delete cached attachments didn't work for documents because the `custom_hash_data` is different for files created from a file and files created from cached attachments. When creating a document attachment, the name of the file is taken into account to calculate the hash. Let's say the original file name is "logo.pdf", and the generated hash is "123456". The cached attachment will be "123456.pdf", so the generated hash using the cached attachment will be something different, like "28af3". So the file that will be removed will be "28af3.pdf", and not "123456.pdf", which will still be present. Furthermore, there are times where users choose a file and then they close the browser or go to a different page. In those cases, we weren't deleting the cached attachments either. So we're adding a rake task to delete these files once a day. This way we can simplify the logic we were using to destroy cached attachments. Note there's related a bug in documents: when editing a record (for example, a proposal), if the title of the document changes, its hash changes, and so it will be impossible to generate a link to that document. Changing the way this hash is generated is not an option because it would break links to existing files. We'll try to fix it when moving to Active Storage.
53 lines
1.4 KiB
Ruby
53 lines
1.4 KiB
Ruby
class Images::FieldsComponent < ApplicationComponent
|
|
attr_reader :f, :imageable
|
|
delegate :current_user, :render_image, to: :helpers
|
|
|
|
def initialize(f, imageable:)
|
|
@f = f
|
|
@imageable = imageable
|
|
end
|
|
|
|
private
|
|
|
|
def image
|
|
f.object
|
|
end
|
|
|
|
def file_name
|
|
image.attachment_file_name
|
|
end
|
|
|
|
def destroy_link
|
|
if !image.persisted? && image.cached_attachment.present?
|
|
link_to t("images.form.delete_button"), "#", class: "delete remove-cached-attachment"
|
|
else
|
|
link_to_remove_association t("images.form.delete_button"), f, class: "delete remove-image"
|
|
end
|
|
end
|
|
|
|
def file_field
|
|
klass = image.persisted? || image.cached_attachment.present? ? " hide" : ""
|
|
f.file_field :attachment,
|
|
label_options: { class: "button hollow #{klass}" },
|
|
accept: accepted_content_types_extensions,
|
|
class: "js-image-attachment",
|
|
data: { url: direct_upload_path }
|
|
end
|
|
|
|
def direct_upload_path
|
|
direct_uploads_path("direct_upload[resource_type]": imageable.class.name,
|
|
"direct_upload[resource_id]": imageable.id,
|
|
"direct_upload[resource_relation]": "image")
|
|
end
|
|
|
|
def accepted_content_types_extensions
|
|
Setting.accepted_content_types_for("images").map do |content_type|
|
|
if content_type == "jpg"
|
|
".jpg,.jpeg"
|
|
else
|
|
".#{content_type}"
|
|
end
|
|
end.join(",")
|
|
end
|
|
end
|