Commit 96c800e4 by Andrew Timberlake Committed by Jon Yurek

Added ability to set arbitrary S3 headers. Included option to remove timestamp…

Added ability to set arbitrary S3 headers. Included option to remove timestamp form url method to improve cache control
parent f79a822f
...@@ -97,9 +97,10 @@ module Paperclip ...@@ -97,9 +97,10 @@ module Paperclip
# and can point to an action in your app, if you need fine grained security. # and can point to an action in your app, if you need fine grained security.
# This is not recommended if you don't need the security, however, for # This is not recommended if you don't need the security, however, for
# performance reasons. # performance reasons.
def url style = default_style # set include_updated_timestamp to false if you want to stop the attachment update time appended to the url
def url style = default_style, include_updated_timestamp = true
url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style) url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url include_updated_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
end end
# Returns the path of the attachment as defined by the :path option. If the # Returns the path of the attachment as defined by the :path option. If the
......
...@@ -91,6 +91,7 @@ module Paperclip ...@@ -91,6 +91,7 @@ module Paperclip
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
# 'http' or 'https'. Defaults to 'http' when your :s3_permissions are 'public-read' (the # 'http' or 'https'. Defaults to 'http' when your :s3_permissions are 'public-read' (the
# default), and 'https' when your :s3_permissions are anything else. # default), and 'https' when your :s3_permissions are anything else.
# * +s3_headers+: A hash of headers such as {'Expires' => 1.year.from_now.httpdate}
# * +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.
...@@ -113,6 +114,7 @@ module Paperclip ...@@ -113,6 +114,7 @@ module Paperclip
@s3_options = @options[:s3_options] || {} @s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read' @s3_permissions = @options[:s3_permissions] || 'public-read'
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https') @s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/) @url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/)
end end
base.class.interpolations[:s3_path_url] = lambda do |attachment, style| base.class.interpolations[:s3_path_url] = lambda do |attachment, style|
...@@ -165,7 +167,7 @@ module Paperclip ...@@ -165,7 +167,7 @@ module Paperclip
logger.info("[paperclip] -> #{path(style)}") logger.info("[paperclip] -> #{path(style)}")
key = s3_bucket.key(path(style)) key = s3_bucket.key(path(style))
key.data = file key.data = file
key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}) key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers))
rescue RightAws::AwsError => e rescue RightAws::AwsError => e
raise raise
end end
......
...@@ -295,6 +295,10 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -295,6 +295,10 @@ class AttachmentTest < Test::Unit::TestCase
should "make sure the updated_at mtime is in the url if it is defined" do should "make sure the updated_at mtime is in the url if it is defined" do
assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah) assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah)
end end
should "make sure the updated_at mtime is NOT in the url if false is passed to the url method" do
assert_no_match %r{#{Time.now.to_i}$}, @attachment.url(:blah, false)
end
context "with the updated_at field removed" do context "with the updated_at field removed" do
setup do setup do
......
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