thoughtbot/paperclip — Issue #634: Content Type and S3
Author: russ1985
Created: 2011-10-26 20:43:42+00:00
State: open

I don't know if anyone has come across this but it the lastest version the content type is not being set correctly in Amazon S3. This worked in prior rails 2 versions, we are moving to rails 3 and I am using this to set the content type...

before_post_process :set_content_type

def set_content_type
klass = @type.constantize
self.data.instance_write(:content_type, content_type)
end

I have classes specified for the different file types StyleSheet, Template, Javascript etc. But setting a content_type of text/css resulted in text/plain on S3. I added this patch to paperclip that has seemed to fix it...

module Paperclip
module Storage
module S3
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => content_type,
:access => (@s3_permissions[style] || @s3_permissions[:default]),
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
retry
rescue AWS::S3::ResponseError => e
raise
end
end

after_flush_writes # allows attachment to clean up temp files

@queued_for_write = {}
end
end
end
end

do you have any recommendations?

thanks