Remove cached files after complete uploads.

This commit is contained in:
Senén Rodero Rodríguez
2017-09-23 23:23:26 +02:00
parent 93e5c9656c
commit eb8ab83ac9
2 changed files with 18 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ class Document < ActiveRecord::Base
validates :documentable_type, presence: true, if: -> { persisted? }
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
after_save :remove_cached_attachment, if: -> { cached_attachment.present? }
def set_cached_attachment_from_attachment(prefix)
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
@@ -81,4 +82,12 @@ class Document < ActiveRecord::Base
end
end
def remove_cached_attachment
document = Document.new(documentable: documentable,
cached_attachment: cached_attachment,
user: user)
document.set_attachment_from_cached_attachment
document.attachment.destroy
end
end

View File

@@ -30,6 +30,7 @@ class Image < ActiveRecord::Base
validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? }
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
after_save :remove_cached_attachment, if: -> { cached_attachment.present? }
def set_cached_attachment_from_attachment(prefix)
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
@@ -100,4 +101,12 @@ class Image < ActiveRecord::Base
attachment.present? && imageable_accepted_content_types.include?(attachment_content_type)
end
def remove_cached_attachment
image = Image.new(imageable: imageable,
cached_attachment: cached_attachment,
user: user)
image.set_attachment_from_cached_attachment
image.attachment.destroy
end
end