technoweenie / attachment_fu

Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
http://weblog.techno-weenie.net
MIT License
1.02k stars 339 forks source link

S3/Cloud Creates wrong public path when using separate thumbnail class #23

Open mpetnuch opened 14 years ago

mpetnuch commented 14 years ago

If you have a separate thumbnail class the wrong public_filename is show when using S3/Cloud.

Examples:

photo = Photo.find(328) => #<Photo id: 328, parent_id: nil, size: 2078, width: 33, height: 48, content_type: "image/gif", filename: "Hollowbody_1.gif", thumbnail: nil, caption: nil, position: 43, picturable_id: 14, picturable_type: "Topic", created_at: "2010-07-30 15:05:02", updated_at: "2010-07-30 15:05:03">

File System:

photo.public_filename => "/photos/0000/0328/Hollowbody_1.gif" photo.public_filename(:thumb) => "/thumbnails/0000/0328/Hollowbody_1_thumb.gif"

S3/Cloud:

photo.public_filename => "http://XXX.cloudfront.net/photos/328/Hollowbody_1.gif" photo.public_filename(:thumb) => "http://XXX.cloudfront.net/photos/328/Hollowbody_1_thumb.gif"

Here is a patch that fixes it:

From 130b83b534fbdb03f15a5cc06c46c65d8e1629a2 Mon Sep 17 00:00:00 2001 From: Michael Petnuch michael@petnuch.com Date: Fri, 30 Jul 2010 11:59:50 -0400 Subject: [PATCH] Correct path when using thumbnail class with S3/Cloud


.../attachment_fu/backends/cloud_file_backend.rb | 7 ++++--- .../attachment_fu/backends/s3_backend.rb | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb b/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb index 214ab27..8d043de 100644 --- a/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +++ b/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb @@ -142,14 +142,15 @@ module Technoweenie # :nodoc:

     # The pseudo hierarchy containing the file relative to the container name
     # Example: <tt>:table_name/:id</tt>
bacrossland commented 13 years ago

If you're running this under ruby 1.9.2 you have to make an additional change to the s3_backend.rb because of the way ruby 1.9.2 handles arrays to string.

Your public_filename method needs to change to look like the following:

      def public_filename(*args)
           if args.empty?
              clean_args = nil
           else
              clean_args = args.join  
           end

          if attachment_options[:cloudfront]
             cloudfront_url(clean_args)
         else
             s3_url(clean_args)
         end
      end