Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old image kept on cloudinary #311

Open
Nkadze opened this issue Jul 16, 2018 · 5 comments
Open

Old image kept on cloudinary #311

Nkadze opened this issue Jul 16, 2018 · 5 comments
Labels

Comments

@Nkadze
Copy link

Nkadze commented Jul 16, 2018

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.

@idobarnoam
Copy link

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.

@Nkadze
Copy link
Author

Nkadze commented Jul 19, 2018

@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

@taragano
Copy link
Collaborator

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

@toyflish
Copy link

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
Copy link

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

@roeeba roeeba closed this as completed Aug 28, 2018
@roeeba roeeba added the bug label Aug 28, 2018
@roeeba roeeba reopened this Aug 28, 2018
@roeeba roeeba removed the question label Aug 28, 2018
@roeeba roeeba assigned tocker and unassigned idobarnoam Aug 28, 2018
@tocker tocker removed their assignment Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants