How to delete recached images on promotion?

181 views
Skip to first unread message

pat...@mokolabs.com

unread,
Mar 4, 2018, 1:22:20 PM3/4/18
to Shrine
Is there any way to delete recached images on promotion?

So, with the recache plugin enabled, if I upload a new photo, two files are created in the cache storage. Once the photo has been promoted, one file is removed, but the second one is not deleted. Since I'm migrating over a hundred thousand images, I need to make sure I don't leave any files in the cache storage after each photo is processed.

After digging around, I figured out the recache plugin isn't deleting files it creates after the photo is promoted.

I'm already using the deleted_promoted plugin, but that only deletes the original upload -- not the recached upload.

- Is there a quick hack to fix this now?

- How can I help update the :delete_promoted plugin to also delete recached files?

Thanks!

Janko Marohnić

unread,
Mar 4, 2018, 1:47:59 PM3/4/18
to pat...@mokolabs.com, Shrine
Try this hack:

  Shrine.plugin :module_include

  Shrine.attacher_methods do
    def recache
      super
      delete!(@old) if cached?
    end
  end

Alternatively you can delete the cached file manually if you split up assignment and persistence:

  record.attachment = file
  cached_file = record.attachment
  record.save
  cached_file.delete

We're planning to implement a more explicit and flexible `versions` plugin, which will deprecate `processing`, `recache`, and the current `versions` plugin, so I don't think it would be worthwhile to extend the `delete_promoted` plugin with support for deleting recached files.

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+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/ed484ef8-d2e8-43a2-9f3c-d6402622721c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pat...@mokolabs.com

unread,
Mar 4, 2018, 1:58:43 PM3/4/18
to Shrine
Thanks for the quick response, as always, Janko.

I tried adding your code to my Shrine initalizer, but I'm getting this error:

 `<main>': undefined method `attacher_methods' for Shrine:Class (NoMethodError)
 
Here's my initializer:

require 'shrine'
require 'shrine/storage/file_system'
require 'image_processing/mini_magick'


# Custom plugin
class Shrine
 
module Plugins
   
module ImageSizeForRails
     
module FileMethods
       
def to_size
         
# Converts image dimensions array into :size param
         
# so it can be used by the standard Rails image_tag helper
         
# [1024,768] => "1024x768"
          dimensions
.join("x")
       
end
     
end
   
end
    register_plugin
(:image_size_for_rails, ImageSizeForRails)
 
end
end


Shrine.storages = {
  cache
: Shrine::Storage::FileSystem.new("tmp", prefix: "cache/uploads"), # temporary storage
  store
: Shrine::Storage::FileSystem.new("public", prefix: "uploads"),    # permanent storage
}


Shrine.plugin :activerecord
Shrine.plugin :backgrounding
Shrine.plugin :image_size_for_rails
Shrine.plugin :module_include


Shrine::Attacher.promote { |data| UploadJob.perform_later(data) }
Shrine::Attacher.delete { |data| DeleteJob.perform_later(data) }



Shrine.attacher_methods do
 
def recache
   
super
   
delete!(@old) if cached?
 
end
end
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.

Janko Marohnić

unread,
Mar 4, 2018, 3:08:25 PM3/4/18
to pat...@mokolabs.com, Shrine
Oops, it should be #attacher_module, not #attacher_methods.

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

To post to this group, send email to ruby-...@googlegroups.com.

pat...@mokolabs.com

unread,
Mar 4, 2018, 8:08:08 PM3/4/18
to Shrine
Okay, I'm still struggling with this.

When I use #attacher_module instead of #attacher_methods, I'm getting this error:

undefined method `delete' for nil:NilClass

So I guess that @old is nil when we try to delete it.

The second technique -- deleting the cached file in the controller -- does work, but it would be better to solve this using the plugin.

Any ideas what's going on here? I'm thinking maybe it's related to background jobs or other plugins I'm using.

Thanks!

Janko Marohnić

unread,
Mar 5, 2018, 8:47:32 AM3/5/18
to pat...@mokolabs.com, Shrine
I made a mistake, this should hopefully work:

  Shrine.attacher_module do
    def recache
      cached = get
      super
      delete!(cached) if cached
    end
  end

Kind regards,
Janko

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

To post to this group, send email to ruby-...@googlegroups.com.

pat...@mokolabs.com

unread,
Mar 5, 2018, 10:29:37 AM3/5/18
to Shrine
Thanks! That worked! :)
Reply all
Reply to author
Forward
0 new messages