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

Already on GitHub? Sign in to your account

Paperclip 2.6.0/aws-sdk try to delete styles that don't exist, causing exception #743

Closed
uberllama opened this Issue Feb 16, 2012 · 4 comments

Comments

Projects
None yet
2 participants

Using Rails 3.2.1 and Ruby 1.9.3.

I was previously running paperclip 2.4.1 with aws-s3 with no issues when deleting a file. Standard asset model, and controller actions for creating and destroying the record.

asset.rb

has_attached_file :upload,
    :styles => {
      :tiny => {:geometry => '80x80>', :source_file_options => '-auto-orient'},
      :thumb => {:geometry => '480>', :source_file_options => '-auto-orient'},
      :large => {:geometry => '1024x1024>', :source_file_options => '-auto-orient'}
    }

before_post_process :conditional_resize_images

# Resize uploaded asset (i.e. create styles) only if it is an image
def conditional_resize_images
    return false unless upload_content_type =~ Regex::IMAGE_CONTENT_REGEX
end

assets_controller#create

[paperclip] Saving attachments.
[paperclip] saving attachments/123/original/textfile.rtf
[AWS S3 200 1.153745] put_object(:acl=>:authenticated_read,:bucket_name=>"mydevbucket",:content_disposition=>"attachment",:content_type=>"application/rtf",:data=>#<File:/var/folders/+x/+xGad....rtf>,:key=>"attachments/123/original/textfile.rtf")

assets_controller#destroy


Started DELETE "/attachments/123" for 127.0.0.1 at 2012-02-16 09:57:05 -0700
Processing by AssetsController#destroy as JS
  Parameters: {"id"=>"1020"}
  Asset Load (0.2ms)  SELECT `assets`.* FROM `assets` WHERE `assets`.`id` = 123 LIMIT 1
   (0.1ms)  BEGIN
[paperclip] Scheduling attachments for deletion.
[AWS S3 200 0.091734] head_object(:bucket_name=>"mydevbucket",:key=>"attachments/123/original/textfile.rtf")

[AWS S3 403 0.093428] head_object(:bucket_name=>"mydevbucket",:key=>"attachments/123/tiny/textfile.rtf") AWS::Errors::Base: AWS::Errors::Base

   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 194ms

AWS::Errors::Base (AWS::Errors::Base):
  app/controllers/assets_controller.rb:71:in `destroy'
  app/middleware/flash_session_cookie_middleware.rb:18:in `call'


  Rendered /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.2ms)
  Rendered /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
  Rendered /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.4ms)

What you'll see here is that the delete request is trying to delete attachments/123/tiny/textfile.rtf, which doesn't exist. Tiny is a style that gets applied only to image uploads, along with a few other styles (thumb, etc), as you can see in the asset model.

Enlightenment appreciated.

I have confirmed that deletion works correctly if I remove my style declarations. So I'm not sure what changed between 2.4.1 and 2.6.0 but paperclip is clearly trying to delete styles that don't exist for an attachment. Was it perhaps doing this before but ignoring missing styles rather than raising an exception?

Here's a better stack trace run from a test:

test_delete_a_draft_asset(AssetsControllerTest):
AWS::Errors::Base: AWS::Errors::Base
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/aws-sdk-1.3.4/lib/aws/core/client.rb:261:in `return_or_raise'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/aws-sdk-1.3.4/lib/aws/core/client.rb:321:in `client_request'
    (eval):3:in `head_object'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/aws-sdk-1.3.4/lib/aws/s3/s3_object.rb:94:in `head'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/aws-sdk-1.3.4/lib/aws/s3/s3_object.rb:71:in `exists?'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip/storage/s3.rb:237:in `exists?'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip/attachment.rb:460:in `block in queue_existing_for_delete'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip/attachment.rb:459:in `map'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip/attachment.rb:459:in `queue_existing_for_delete'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip.rb:482:in `block in prepare_for_destroy'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip.rb:461:in `block in each_attachment'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip.rb:460:in `each'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip.rb:460:in `each_attachment'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/paperclip-2.6.0/lib/paperclip.rb:481:in `prepare_for_destroy'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:407:in `_run__4496500028692525756__destroy__3403128668297463690__callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_destroy_callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:254:in `destroy'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/transactions.rb:236:in `block in destroy'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/transactions.rb:208:in `transaction'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/transactions.rb:236:in `destroy'
    /Users/yuval/Documents/webdev/mysite/app/controllers/assets_controller.rb:71:in `destroy'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:167:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:469:in `_run__4335382314239699235__process_action__1210692511636296819__callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:17:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `block in instrument'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `instrument'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:121:in `process'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/abstract_controller/rendering.rb:45:in `process'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:464:in `process'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:49:in `process'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:395:in `delete'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:406:in `xml_http_request'
    /Users/yuval/Documents/webdev/mysite/test/functional/assets_controller_test.rb:622:in `block (2 levels) in <class:AssetsControllerTest>'
    /Users/yuval/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/testing/assertions.rb:55:in `assert_difference'

I see an :async option in aws-sdk aws-sdk-1.3.4/lib/aws/core/client.rb:260 . Wondering if this is the issue (i.e. async not turned on by paperclip). I'm new to the aws-sdk integration so unsure, but this was the first thing I noticed. This is a definite issue.

Contributor

sikachu commented Feb 17, 2012

I think so, I think aws-sdk actually decides to raise the error when the file is not found. I'll work on the fix to ignore the error when delete.

Thanks sikachu! Greatly appreciated.

I wonder if there needs to be more smarts with Paperclip in general for handling contextual generation of styles. Half the questions I see out there are regarding how to resize only if an attachment is an image, then there's this issue and some prior issues I had with validation steps occurring out of order with post processing. Inevitably, extra plumbing is required for the end user. Just a thought.

@sikachu sikachu closed this in 56012bf Feb 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment