Question

Well, I'm using carrierwave and generating versions (thumbnails) with it. The storage backend is Amazon S3.

class Image < ActiveRecord::Base
   mount_uploader :attachment, ImageUploader
end

When I call Image.find(1).destroy, the original file is removed from S3:

https://myapp.s3.amazonaws.com/assets/9e966591195c64636825d3ea695f0fc3/61bf4c0c-f9f8-4d29-b794-7daf6d050446.jpg

but a version of it stills there:

https://myapp.s3.amazonaws.com/assets/9e966591195c64636825d3ea695f0fc3/61bf4c0c-f9f8-4d29-b794-7daf6d050446_square.jpg

I'm suspecting of the method def full_filename(for_file)

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  include CarrierWaveDirect::Uploader

  def store_dir
    "assets"
  end

  def cache_dir
    "#{Rails.root}/tmp/images"
  end

  version :square do
    process resize_to_fill: [200, 200]
    def full_filename(for_file)
      ext         = File.extname(for_file)
      base_name   = for_file.chomp(ext)
      [base_name, version_name].compact.join('_') + ".jpg"
    end
  end

end

Thanks in advance!

Was it helpful?

Solution

Replace your version definition as below:

version :square do
    process resize_to_fill: [200, 200]
 end

You do not need to define the file name as Carrierwave will make a new version of file with square_ prefix. So, its unique anyway. When you call Image.find(1).destroy, Carrierwave destroys the original file and then looks for a file with square_* prefix to delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top