Delete file when record destroy

230 views
Skip to first unread message

Josue A. de Lima

unread,
Aug 9, 2019, 3:21:01 AM8/9/19
to Shrine
Hi, I have this models:

Photo: (Im using this to store photos of multiple models)

class Photo < ApplicationRecord
 include FileUploader[:file]
 belongs_to :imageable, polymorphic: true
end

require 'image_processing/mini_magick'

# File Uploader Class
class FileUploader < Shrine
plugin :derivation_endpoint,
secret_key: ENV['SECRET_KEY'],
prefix: 'derivations/thumbnails',
upload: true,
upload_redirect: true
plugin :infer_extension, force: true

derivation :thumbnail do |file, width, height|
ImageProcessing::MiniMagick
.source(file)
.resize_to_limit!(width.to_i, height.to_i)
end
end

class Variant < ApplicationRecord
belongs_to :item
has_many :options, dependent: :destroy
has_many :photos, as: :imageable, dependent: :destroy
end

When I run in Rails Console Variant.destroy_all, all the records destroy but the files don't get deleted.

¿How can I fire the delete? 

Thanks

Janko Marohnić

unread,
Aug 9, 2019, 3:37:03 AM8/9/19
to Shrine, Josue A. de Lima
Shrine isn't automatically deleting dynamic derivatives, because it doesn't know where they were uploaded, since the destination storage is configurable.

You'll need to delete them manually after the record is deleted, for example:

  [[800, 800], [500, 500], [300, 300], ...].each do |(width, height)|
    photo.file.derivation(:thumbnail, width, height).delete
  end

You can also send that to a background job, for example:

  DeleteDynamicDerivatives.perform_async(
    photo.file.shrine_class,
    photo.file.to_json,
    [[:thumbnail, 800, 800], [:thumbnail, 500, 500], [:thumbnail, 300, 300], ...],
  )

  class DeleteDynamicDerivatives
    include Sidekiq::Worker

    def perform(shrine_class, file_data, derivations)
      shrine_class = Object.const_get(shrine_class)
      file         = shrine_class.uploaded_file(file_data)

      derivations.each do |args|
        file.derivation(*args).delete
      end
    end
  end

I would like to make it automatic at some point, but at the moment I don't know how.

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/e8c77dae-1b6a-4154-9b07-d5ab4441c057%40googlegroups.com.
Message has been deleted

Josue A. de Lima

unread,
Aug 9, 2019, 4:03:45 AM8/9/19
to Shrine
Thanks for the response!, hope you can achieve the auto-delete at some point.

Regards
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.

Janko Marohnić

unread,
Aug 9, 2019, 4:07:18 AM8/9/19
to Shrine, Josue A. de Lima
Yes, that's the default storage, but the user can override it with the :upload_storage plugin option (in the Plugin Options section).

Another problem is that a Shrine storage currently doesn't have the ability to delete all files under a given prefix (like ActiveStorage's #delete_prefixed), nor is it able to list files under a given prefix. Both of these features were requested recently, so they will likely get added some time after Shrine 3.0 is released. That should hopefully make it easier to implement auto-delete.

Kind regards,
Janko
On 9 Aug 2019, 09:59 +0200, Josue A. de Lima <josuea...@gmail.com>, wrote:
I don't understand what you mean by: "since the destination storage is configurable" you have the prefix and all thumbnails have this filename: filenameSTR/thumbnail-w-h (for thumbnails) and the main file: filenameSTR. I'm I right here? 

If Im right you just need to search all files and folders that start with filename, and delete them, right?

Or Im missing something?


On Friday, August 9, 2019 at 2:37:03 AM UTC-5, Janko Marohnić wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages