technoweenie/attachment_fu

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

mpetnuch opened this issue · 1 comments

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>
  •    def base_path
    
  •      File.join(attachment_options[:path_prefix], attachment_path_id)
    
  •    def base_path(thumbnail = nil)
    
  •      file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix]
    
  •      File.join(file_system_path, attachment_path_id)
     end
    
     # The full path to the file relative to the container name
     # Example: <tt>:table_name/:id/:filename</tt>
     def full_filename(thumbnail = nil)
    
  •      File.join(base_path, thumbnail_name_for(thumbnail))
    
  •      File.join(base_path(thumbnail), thumbnail_name_for(thumbnail))
     end
    
     # All public objects are accessible via a GET request to the Cloud Files servers. You can generate a
    

    diff --git a/lib/technoweenie/attachment_fu/backends/s3_backend.rb b/lib/technoweenie/attachment_fu/backends/s3_backend.rb
    index 53b0caf..1593fc6 100644
    --- a/lib/technoweenie/attachment_fu/backends/s3_backend.rb
    +++ b/lib/technoweenie/attachment_fu/backends/s3_backend.rb
    @@ -252,14 +252,15 @@ module Technoweenie # :nodoc:

     # The pseudo hierarchy containing the file relative to the bucket name
     # Example: <tt>:table_name/:id</tt>
    
  •    def base_path
    
  •      File.join(attachment_options[:path_prefix], attachment_path_id)
    
  •    def base_path(thumbnail = nil)
    
  •      file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix]
    
  •      File.join(file_system_path, attachment_path_id)
     end
    
     # The full path to the file relative to the bucket name
     # Example: <tt>:table_name/:id/:filename</tt>
     def full_filename(thumbnail = nil)
    
  •      File.join(base_path, thumbnail_name_for(thumbnail))
    
  •      File.join(base_path(thumbnail), thumbnail_name_for(thumbnail))
     end
    
     # All public objects are accessible via a GET request to the S3 servers. You can generate a
    

    1.7.2

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