Remove cached files after complete uploads.
This commit is contained in:
@@ -22,6 +22,7 @@ class Document < ActiveRecord::Base
|
|||||||
validates :documentable_type, presence: true, if: -> { persisted? }
|
validates :documentable_type, presence: true, if: -> { persisted? }
|
||||||
|
|
||||||
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
|
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)
|
def set_cached_attachment_from_attachment(prefix)
|
||||||
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
||||||
@@ -81,4 +82,12 @@ class Document < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class Image < ActiveRecord::Base
|
|||||||
validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? }
|
validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? }
|
||||||
|
|
||||||
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
|
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)
|
def set_cached_attachment_from_attachment(prefix)
|
||||||
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
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)
|
attachment.present? && imageable_accepted_content_types.include?(attachment_content_type)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user