jrgifford / delayed_paperclip

Process your Paperclip attachments in the background with delayed_job or Resque.
http://www.jstorimer.com/ruby/2010/01/30/delayed-paperclip.html
MIT License
402 stars 155 forks source link

Image Copied To temp Directory On Reprocess #115

Closed Undistraction closed 10 years ago

Undistraction commented 10 years ago

I'm using Delayed Paperclip alongside direct uploads to S3. My model is called Photo and its attachment is image.

Images are uploaded to S3 using JavaScript from the Photo form. The file is stored in the location that Paperclip expects the original image to be located, and the file details are saved to hidden fields. When the form is submitted, these attributes are written to the Photo model:

image_file_name
image_file_size
image_content_type

Because writing these attributes alone doesn't seem to be enough to trigger Delayed Paperclip to process the image, after Photo.save I call Photo.image.reprocess! which does get DelayedPaperclip to create a new Sidekiq job which successfully processes the image.

The problem, is that when I call Photo.save in the PhotosController, the file is copied to a temp directory from S3, then back to S3. This happens outside of the job and is blocking:

[paperclip] copying image_assets/grab-original.tiff to local file /var/folders/bv/x495g9g10m7119680c9ssqmr0000gn/T/94943834d26bcb8b471f4eeb2a7f899d20141125-3895-1oqom7l
[AWS S3 200 2.601589 0 retries] get_object(:bucket_name=>"example-com-development",:key=>"image_assets/grab-original.tiff")

[paperclip] saving image_assets/grab-original.tiff
[AWS S3 200 2.47114 0 retries] put_object(:acl=>:public_read,:bucket_name=>"example-com-development",:cache_control=>"max-age=29030400",:content_length=>534472,:content_type=>"image/tiff",:data=>Paperclip::AttachmentAdapter: grab.tiff,:key=>"image_assets/grab-original.tiff")
Undistraction commented 10 years ago

Looks like I was approaching this in the wrong way.

I needed to do the following from my controller:

@media_item = @gallery.media_items.new(media_item_params)

# Ensure we are flagged as processing
@media_item.photo.prepare_enqueueing_for(:image)

@media_item.save

# Add Job
@media_item.photo.enqueue_delayed_processing
respond_with(:admin, @galleryable, @media_item)