Commit 8be77a91 by Jon Berke Committed by Jon Yurek

Code cleanup

parent de11bd94
......@@ -45,6 +45,7 @@ module Paperclip
# * +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
# 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
# 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.
......@@ -83,7 +84,7 @@ module Paperclip
# * +s3_metadata+: These key/value pairs will be stored with the
# object. This option works by prefixing each key with
# "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
# <tt>:reduced_redundancy</tt>, the object will be stored using Reduced
# Redundancy Storage. RRS enables customers to reduce their
......@@ -123,9 +124,8 @@ module Paperclip
(permission == :public_read) ? 'http' : 'https'
end
@s3_metadata = @options[:s3_metadata] || {}
@s3_headers, new_s3_metadata = convert_headers_to_s3_format(@options[:s3_headers])
@s3_metadata.merge!(new_s3_metadata)
@s3_headers = {}
merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
@s3_headers[:storage_class] = @options[:s3_storage_class] if @options[:s3_storage_class]
......@@ -307,15 +307,10 @@ module Paperclip
write_options[:server_side_encryption] = @s3_server_side_encryption
end
if @options[:styles][style].is_a?(Hash) && @options[:styles][style].has_key?(:s3_headers)
style_specific_s3_headers, style_specific_s3_meta = convert_headers_to_s3_format(@options[:styles][style][:s3_headers])
@s3_headers.merge!(style_specific_s3_headers)
@s3_metadata.merge!(style_specific_s3_meta) # Merge with meta values found in :s3_headers
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])
style_specific_options = @options[:styles][style]
if style_specific_options.is_a?(Hash)
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_options[:s3_metadata]) if style_specific_options.has_key?(:s3_metadata)
end
write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
......@@ -377,25 +372,16 @@ module Paperclip
s3_protocol(style_name) == "https"
end
# Converts http-style headers to the format needed by the aws-sdk's S3Object#write method
# Returns two hashes - s3_headers and s3_metadata
# If amazon meta data headers (x-amz-meta-*) are found in http_headers, they are passed back in the second return 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)|
def merge_s3_headers(http_headers, s3_headers, s3_metadata)
http_headers = http_headers.call(instance) if http_headers.respond_to?(:call)
http_headers.inject({}) do |headers,(name,value)|
case name.to_s
when /^x-amz-meta-(.*)/i
s3_metadata[$1.downcase] = value
else
name = name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym
headers[name] = value
s3_headers[name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym] = value
end
headers
end
return s3_headers, s3_metadata
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