cloudinary_gem icon indicating copy to clipboard operation
cloudinary_gem copied to clipboard

Old image kept on cloudinary

Open Nkadze opened this issue 7 years ago • 5 comments

Hi, When I'm updating image from rails form, through update_attributes and carrierwave uploader, on cloudinary both: new and old images are kept. Is it possible to override this behavior? Thanks in advance.

Nkadze avatar Jul 16 '18 00:07 Nkadze

Hi @Nkadze

Could you maybe share a code snippet so we could verify we are on the same page?

Also if you could share the versions that are being used of your Ruby, Rails, Cloudinary GEM and CarrierWave installations we could take a closer look.

idobarnoam avatar Jul 18 '18 08:07 idobarnoam

@idobarnoam, Thanks for quick response,

Those are package versions:

ruby '2.3.1'
'rails', '~> 5.1.5'
cloudinary (1.9.1)
carrierwave (1.2.3)

Uploader:

class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave

  process resize_to_fit: [1920, 1080]

  version :small do
    cloudinary_transformation \
    gravity:      'auto',
    width:        150,
    height:       100,
    crop:         'fill',
    quality:      'auto',
    fetch_format: :auto
  end

end

Model:

mount_uploader :featured_image, ImageUploader

Update Code:

obj.update_attributes(remote_featured_image_url: url)

or:

@model.update(model_params)
#--------------
def model_params
   params.require(:model).permit(:name, :featured_image, :featured_image_cache)
end

Nkadze avatar Jul 19 '18 11:07 Nkadze

Thank you @Nkadze. We're taking a look and we'll let you know of any insights.

taragano avatar Jul 19 '18 20:07 taragano

I have the same problem, updating an attachment through a fileupload creates a new file on cloudinary leaving the old one untouched. If I look at the carrier_wave.rb file, there is only a Cloudinary::Uploader.destroy call in the delete method, but no after_update functionality to destroy the old public_id.

toyflish avatar Jul 27 '18 07:07 toyflish

I fixed it by adding a callback to my uploaders. Since carrierwave stores the public_id within an identifier with resource_type ... and the destructuring method is not modular, I choose to initialize a new CloudinaryFile object and call the delete method.

class CarrierWave::Uploader::Base
  # has to be included before after :store
  include Cloudinary::CarrierWave

  after :store, :cleanup_changed

  def cleanup_changed(_arg)
    was_identifier = self.model.saved_changes[mounted_as][0]
    unless was_identifier.nil?
      was_file = CloudinaryFile.new(was_identifier, self)
      was_file.delete
    end
  end
end

toyflish avatar Jul 27 '18 08:07 toyflish