attachinary icon indicating copy to clipboard operation
attachinary copied to clipboard

Would prefer that attachment content are not uploaded to Cloudinary until the model otherwise passes validations

Open rdnewman opened this issue 8 years ago • 1 comments

For instance, given the following model:

class SomeModel < ActiveRecord::Base
  has_attachment :image, accept: [:png]
  validates :name, presence: true     # where name is an unrelated field to the image attachment
end

and a statement

sm = SomeModel.new(image: File.open('/path/to/file.png'));

my tests indicate that the image given will be uploaded to Cloudinary even though the model itself can't be created due to a failed validation. This appears to be true because upload happens during the initialize assignment of values to model attributes.

I was able to work around it in my model to defer the upload until after_create, but I'd prefer not to. The other alternative I suppose is to rollback the upload (i.e., remove from Cloudinary) on a failed create.

rdnewman avatar May 13 '16 20:05 rdnewman

yes. that's true. I recognized the same behaviour. This leeds to massive unused resources on the cloudinary side exceeding the storage capacity quickly

However it would be ok when the tmp-tag would be added, so we could clean unused images as described here: https://github.com/assembler/attachinary/wiki/How-to-cleanup-unused-uploads

But that doesn't work.

When I build a new Object like Post.new(media_url: "http://example.com/your-image.jpg")

The file is uploaded directly: ok so far. But as I not saved the entity locally I assume the Attachinary::TMPTAG is added to the cloudinary-resource. But it is not. When I fetch the tagged resources I get an empty array: Cloudinary::Api.resources_by_tag(Attachinary::TMPTAG)

nicowenterodt avatar Aug 12 '16 10:08 nicowenterodt