Commit 8be77a91 by Jon Berke Committed by Jon Yurek

Code cleanup

parent de11bd94
...@@ -45,6 +45,7 @@ module Paperclip ...@@ -45,6 +45,7 @@ module Paperclip
# * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as # * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as
# {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at # {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at
# runtime. Paperclip will call that Proc with attachment as the only argument. # runtime. Paperclip will call that Proc with attachment as the only argument.
# Can be defined bot globaly and within a style-specific hash.
# * +bucket+: This is the name of the S3 bucket that will store your files. Remember # * +bucket+: This is the name of the S3 bucket that will store your files. Remember
# that the bucket must be unique across all of Amazon S3. If the bucket does not exist # that the bucket must be unique across all of Amazon S3. If the bucket does not exist
# Paperclip will attempt to create it. The bucket name will not be interpolated. # Paperclip will attempt to create it. The bucket name will not be interpolated.
...@@ -83,7 +84,7 @@ module Paperclip ...@@ -83,7 +84,7 @@ module Paperclip
# * +s3_metadata+: These key/value pairs will be stored with the # * +s3_metadata+: These key/value pairs will be stored with the
# object. This option works by prefixing each key with # object. This option works by prefixing each key with
# "x-amz-meta-" before sending it as a header on the object # "x-amz-meta-" before sending it as a header on the object
# upload request. # upload request. Can be defined bot globaly and within a style-specific hash.
# * +s3_storage_class+: If this option is set to # * +s3_storage_class+: If this option is set to
# <tt>:reduced_redundancy</tt>, the object will be stored using Reduced # <tt>:reduced_redundancy</tt>, the object will be stored using Reduced
# Redundancy Storage. RRS enables customers to reduce their # Redundancy Storage. RRS enables customers to reduce their
...@@ -123,9 +124,8 @@ module Paperclip ...@@ -123,9 +124,8 @@ module Paperclip
(permission == :public_read) ? 'http' : 'https' (permission == :public_read) ? 'http' : 'https'
end end
@s3_metadata = @options[:s3_metadata] || {} @s3_metadata = @options[:s3_metadata] || {}
@s3_headers = {}
@s3_headers, new_s3_metadata = convert_headers_to_s3_format(@options[:s3_headers]) merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
@s3_metadata.merge!(new_s3_metadata)
@s3_headers[:storage_class] = @options[:s3_storage_class] if @options[:s3_storage_class] @s3_headers[:storage_class] = @options[:s3_storage_class] if @options[:s3_storage_class]
...@@ -307,15 +307,10 @@ module Paperclip ...@@ -307,15 +307,10 @@ module Paperclip
write_options[:server_side_encryption] = @s3_server_side_encryption write_options[:server_side_encryption] = @s3_server_side_encryption
end end
if @options[:styles][style].is_a?(Hash) && @options[:styles][style].has_key?(:s3_headers) style_specific_options = @options[:styles][style]
style_specific_s3_headers, style_specific_s3_meta = convert_headers_to_s3_format(@options[:styles][style][:s3_headers]) if style_specific_options.is_a?(Hash)
@s3_headers.merge!(style_specific_s3_headers) merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options.has_key?(:s3_headers)
@s3_metadata.merge!(style_specific_s3_meta) # Merge with meta values found in :s3_headers @s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options.has_key?(:s3_metadata)
end
if @options[:styles][style].is_a?(Hash) && @options[:styles][style].has_key?(:s3_metadata)
# Merge with style-specific :s3_metadata
@s3_metadata.merge!(@options[:styles][style][:s3_metadata])
end end
write_options[:metadata] = @s3_metadata unless @s3_metadata.empty? write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
...@@ -377,25 +372,16 @@ module Paperclip ...@@ -377,25 +372,16 @@ module Paperclip
s3_protocol(style_name) == "https" s3_protocol(style_name) == "https"
end end
# Converts http-style headers to the format needed by the aws-sdk's S3Object#write method def merge_s3_headers(http_headers, s3_headers, s3_metadata)
# Returns two hashes - s3_headers and s3_metadata http_headers = http_headers.call(instance) if http_headers.respond_to?(:call)
# If amazon meta data headers (x-amz-meta-*) are found in http_headers, they are passed back in the second return value http_headers.inject({}) do |headers,(name,value)|
def convert_headers_to_s3_format(http_headers)
s3_headers = http_headers || {}
s3_metadata = {}
s3_headers = s3_headers.call(instance) if s3_headers.respond_to?(:call)
s3_headers = (s3_headers).inject({}) do |headers,(name,value)|
case name.to_s case name.to_s
when /^x-amz-meta-(.*)/i when /^x-amz-meta-(.*)/i
s3_metadata[$1.downcase] = value s3_metadata[$1.downcase] = value
else else
name = name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym s3_headers[name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym] = value
headers[name] = value
end end
headers
end end
return s3_headers, s3_metadata
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment