lib/paperclip-aws.rb in paperclip-aws-1.3.1 vs lib/paperclip-aws.rb in paperclip-aws-1.3.2
- old
+ new
@@ -1,8 +1,9 @@
# coding: UTF-8
require 'paperclip'
+require 'uri'
module Paperclip
module Storage
module Aws
def self.extended base
@@ -14,16 +15,18 @@
end unless defined?(AWS)
base.instance_eval do
@s3_credentials = parse_credentials(@options.s3_credentials)
@s3_permissions = set_permissions(@options.s3_permissions)
+
@s3_protocol = @options.s3_protocol ||
Proc.new do |style, attachment|
permission = (@s3_permissions[style.to_sym] || @s3_permissions[:default])
permission = permission.call(attachment, style) if permission.is_a?(Proc)
(permission == :public_read) ? 'http' : 'https'
end
+
@s3_headers = @options.s3_headers || {}
@s3_bucket = @options.bucket
@s3_bucket = @s3_bucket.call(self) if @s3_bucket.is_a?(Proc)
@@ -46,11 +49,11 @@
:s3_endpoint => @s3_endpoint
)
end
end
-
+
def url(style=default_style, options={})
if self.original_filename.nil?
default_url = @default_url.is_a?(Proc) ? @default_url.call(self) : @default_url
return interpolate(default_url, style)
end
@@ -58,19 +61,21 @@
if options[:expires].present? || options[:action].present?
options.reverse_merge!({
:expires => 60*60,
:action => :read
})
- secure = ( self.choose_protocol(options) == 'https' )
- @s3.buckets[@s3_bucket].objects[path(style).gsub(%r{^/}, "")].url_for(options[:action], { :secure => secure, :expires => options[:expires] }).to_s
+ secure = ( self.choose_protocol(style, options) == 'https' )
+ url = @s3.buckets[@s3_bucket].objects[path(style).gsub(%r{^/}, "")].url_for(options[:action], { :secure => secure, :expires => options[:expires] }).to_s
else
if @s3_host_alias.present?
- "#{choose_protocol(options)}://#{@s3_host_alias}/#{path(style).gsub(%r{^/}, "")}"
+ url = "#{choose_protocol(style, options)}://#{@s3_host_alias}/#{path(style).gsub(%r{^/}, "")}"
else
- "#{choose_protocol(options)}://#{@s3_endpoint}/#{@s3_bucket}/#{path(style).gsub(%r{^/}, "")}"
+ url = "#{choose_protocol(style, options)}://#{@s3_endpoint}/#{@s3_bucket}/#{path(style).gsub(%r{^/}, "")}"
end
end
+
+ return URI.escape(url)
end
def bucket_name
@s3_bucket
end
@@ -99,15 +104,15 @@
rescue AWS::S3::Errors::NoSuchKey
return false
end
end
- def choose_protocol(options={})
+ def choose_protocol(style, options={})
if options[:protocol].present?
return options[:protocol].to_s
else
- return @s3_protocol
+ return @s3_protocol.is_a?(Proc) ? @s3_protocol.call(style, self) : @s3_protocol
end
end
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
@@ -129,10 +134,10 @@
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
-
+
@s3.buckets[@s3_bucket].objects[path(style)].write(
file,
:acl => @s3_permissions[:style.to_sym] || @s3_permissions[:default],
:storage_class => @s3_storage_class.to_sym,
:content_type => file.content_type,