paperclip-v2_7-patched-ruby-1_8_6 2.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.travis.yml +14 -0
  4. data/Appraisals +20 -0
  5. data/CONTRIBUTING.md +38 -0
  6. data/Gemfile +5 -0
  7. data/LICENSE +26 -0
  8. data/NEWS +69 -0
  9. data/README.md +444 -0
  10. data/Rakefile +41 -0
  11. data/cucumber/paperclip_steps.rb +6 -0
  12. data/features/basic_integration.feature +48 -0
  13. data/features/rake_tasks.feature +68 -0
  14. data/features/step_definitions/attachment_steps.rb +65 -0
  15. data/features/step_definitions/html_steps.rb +15 -0
  16. data/features/step_definitions/rails_steps.rb +193 -0
  17. data/features/step_definitions/s3_steps.rb +14 -0
  18. data/features/step_definitions/web_steps.rb +209 -0
  19. data/features/support/env.rb +8 -0
  20. data/features/support/fakeweb.rb +3 -0
  21. data/features/support/fixtures/.boot_config.rb.swo +0 -0
  22. data/features/support/fixtures/boot_config.txt +15 -0
  23. data/features/support/fixtures/gemfile.txt +5 -0
  24. data/features/support/fixtures/preinitializer.txt +20 -0
  25. data/features/support/paths.rb +28 -0
  26. data/features/support/rails.rb +46 -0
  27. data/features/support/selectors.rb +19 -0
  28. data/gemfiles/rails2.gemfile +9 -0
  29. data/gemfiles/rails3.gemfile +9 -0
  30. data/gemfiles/rails3_1.gemfile +9 -0
  31. data/gemfiles/rails3_2.gemfile +9 -0
  32. data/generators/paperclip/USAGE +5 -0
  33. data/generators/paperclip/paperclip_generator.rb +27 -0
  34. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  35. data/init.rb +4 -0
  36. data/lib/generators/paperclip/USAGE +8 -0
  37. data/lib/generators/paperclip/paperclip_generator.rb +33 -0
  38. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  39. data/lib/paperclip.rb +493 -0
  40. data/lib/paperclip/attachment.rb +491 -0
  41. data/lib/paperclip/attachment_options.rb +10 -0
  42. data/lib/paperclip/callback_compatibility.rb +61 -0
  43. data/lib/paperclip/geometry.rb +120 -0
  44. data/lib/paperclip/interpolations.rb +174 -0
  45. data/lib/paperclip/iostream.rb +45 -0
  46. data/lib/paperclip/matchers.rb +64 -0
  47. data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
  48. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
  49. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
  50. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
  51. data/lib/paperclip/missing_attachment_styles.rb +87 -0
  52. data/lib/paperclip/processor.rb +58 -0
  53. data/lib/paperclip/railtie.rb +35 -0
  54. data/lib/paperclip/schema.rb +39 -0
  55. data/lib/paperclip/storage.rb +3 -0
  56. data/lib/paperclip/storage/filesystem.rb +81 -0
  57. data/lib/paperclip/storage/fog.rb +191 -0
  58. data/lib/paperclip/storage/s3.rb +351 -0
  59. data/lib/paperclip/style.rb +103 -0
  60. data/lib/paperclip/thumbnail.rb +105 -0
  61. data/lib/paperclip/upfile.rb +64 -0
  62. data/lib/paperclip/url_generator.rb +64 -0
  63. data/lib/paperclip/version.rb +3 -0
  64. data/lib/tasks/paperclip.rake +101 -0
  65. data/paperclip.gemspec +41 -0
  66. data/rails/init.rb +2 -0
  67. data/shoulda_macros/paperclip.rb +124 -0
  68. data/test/attachment_options_test.rb +40 -0
  69. data/test/attachment_test.rb +1211 -0
  70. data/test/database.yml +4 -0
  71. data/test/fixtures/12k.png +0 -0
  72. data/test/fixtures/50x50.png +0 -0
  73. data/test/fixtures/5k.png +0 -0
  74. data/test/fixtures/animated.gif +0 -0
  75. data/test/fixtures/bad.png +1 -0
  76. data/test/fixtures/fog.yml +8 -0
  77. data/test/fixtures/s3.yml +8 -0
  78. data/test/fixtures/spaced file.png +0 -0
  79. data/test/fixtures/text.txt +1 -0
  80. data/test/fixtures/twopage.pdf +0 -0
  81. data/test/fixtures/uppercase.PNG +0 -0
  82. data/test/geometry_test.rb +206 -0
  83. data/test/helper.rb +181 -0
  84. data/test/integration_test.rb +652 -0
  85. data/test/interpolations_test.rb +219 -0
  86. data/test/iostream_test.rb +71 -0
  87. data/test/matchers/have_attached_file_matcher_test.rb +24 -0
  88. data/test/matchers/validate_attachment_content_type_matcher_test.rb +110 -0
  89. data/test/matchers/validate_attachment_presence_matcher_test.rb +47 -0
  90. data/test/matchers/validate_attachment_size_matcher_test.rb +72 -0
  91. data/test/paperclip_missing_attachment_styles_test.rb +96 -0
  92. data/test/paperclip_test.rb +409 -0
  93. data/test/processor_test.rb +10 -0
  94. data/test/schema_test.rb +98 -0
  95. data/test/storage/filesystem_test.rb +62 -0
  96. data/test/storage/fog_test.rb +280 -0
  97. data/test/storage/s3_live_test.rb +138 -0
  98. data/test/storage/s3_test.rb +1093 -0
  99. data/test/style_test.rb +215 -0
  100. data/test/support/mock_attachment.rb +22 -0
  101. data/test/support/mock_interpolator.rb +24 -0
  102. data/test/support/mock_model.rb +2 -0
  103. data/test/support/mock_url_generator_builder.rb +27 -0
  104. data/test/thumbnail_test.rb +396 -0
  105. data/test/upfile_test.rb +53 -0
  106. data/test/url_generator_test.rb +187 -0
  107. metadata +374 -0
@@ -0,0 +1,39 @@
1
+ module Paperclip
2
+ # Provides two helpers that can be used in migrations.
3
+ #
4
+ # In order to use this module, the target class should implement a
5
+ # +column+ method that takes the column name and type, both as symbols,
6
+ # as well as a +remove_column+ method that takes a table and column name,
7
+ # also both symbols.
8
+ module Schema
9
+ @@columns = {:file_name => :string,
10
+ :content_type => :string,
11
+ :file_size => :integer,
12
+ :updated_at => :datetime}
13
+
14
+ def has_attached_file(attachment_name)
15
+ with_columns_for(attachment_name) do |column_name, column_type|
16
+ column(column_name, column_type)
17
+ end
18
+ end
19
+
20
+ def drop_attached_file(table_name, attachment_name)
21
+ with_columns_for(attachment_name) do |column_name, column_type|
22
+ remove_column(table_name, column_name)
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def with_columns_for(attachment_name)
29
+ @@columns.each do |suffix, column_type|
30
+ column_name = full_column_name(attachment_name, suffix)
31
+ yield column_name, column_type
32
+ end
33
+ end
34
+
35
+ def full_column_name(attachment_name, column_name)
36
+ "#{attachment_name}_#{column_name}".to_sym
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ require "paperclip/storage/filesystem"
2
+ require "paperclip/storage/fog"
3
+ require "paperclip/storage/s3"
@@ -0,0 +1,81 @@
1
+ module Paperclip
2
+ module Storage
3
+ # The default place to store attachments is in the filesystem. Files on the local
4
+ # filesystem can be very easily served by Apache without requiring a hit to your app.
5
+ # They also can be processed more easily after they've been saved, as they're just
6
+ # normal files. There is one Filesystem-specific option for has_attached_file.
7
+ # * +path+: The location of the repository of attachments on disk. This can (and, in
8
+ # almost all cases, should) be coordinated with the value of the +url+ option to
9
+ # allow files to be saved into a place where Apache can serve them without
10
+ # hitting your app. Defaults to
11
+ # ":rails_root/public/:attachment/:id/:style/:basename.:extension"
12
+ # By default this places the files in the app's public directory which can be served
13
+ # directly. If you are using capistrano for deployment, a good idea would be to
14
+ # make a symlink to the capistrano-created system directory from inside your app's
15
+ # public directory.
16
+ # See Paperclip::Attachment#interpolate for more information on variable interpolaton.
17
+ # :path => "/var/app/attachments/:class/:id/:style/:basename.:extension"
18
+ module Filesystem
19
+ def self.extended base
20
+ end
21
+
22
+ def exists?(style_name = default_style)
23
+ if original_filename
24
+ File.exist?(path(style_name))
25
+ else
26
+ false
27
+ end
28
+ end
29
+
30
+ # Returns representation of the data of the file assigned to the given
31
+ # style, in the format most representative of the current storage.
32
+ def to_file style_name = default_style
33
+ if @queued_for_write[style_name]
34
+ @queued_for_write[style_name].rewind
35
+ @queued_for_write[style_name]
36
+ elsif exists?(style_name)
37
+ File.new(path(style_name), 'rb')
38
+ end
39
+ end
40
+
41
+ def flush_writes #:nodoc:
42
+ @queued_for_write.each do |style_name, file|
43
+ file.close
44
+ FileUtils.mkdir_p(File.dirname(path(style_name)))
45
+ log("saving #{path(style_name)}")
46
+ FileUtils.cp(file.path, path(style_name))
47
+ FileUtils.chmod(0666&~File.umask, path(style_name))
48
+ end
49
+
50
+ after_flush_writes # allows attachment to clean up temp files
51
+
52
+ @queued_for_write = {}
53
+ end
54
+
55
+ def flush_deletes #:nodoc:
56
+ @queued_for_delete.each do |path|
57
+ begin
58
+ log("deleting #{path}")
59
+ FileUtils.rm(path) if File.exist?(path)
60
+ rescue Errno::ENOENT => e
61
+ # ignore file-not-found, let everything else pass
62
+ end
63
+ begin
64
+ while(true)
65
+ path = File.dirname(path)
66
+ FileUtils.rmdir(path)
67
+ break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
68
+ end
69
+ rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
70
+ # Stop trying to remove parent directories
71
+ rescue SystemCallError => e
72
+ log("There was an unexpected error while deleting directories: #{e.class}")
73
+ # Ignore it
74
+ end
75
+ end
76
+ @queued_for_delete = []
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,191 @@
1
+ module Paperclip
2
+ module Storage
3
+ # fog is a modern and versatile cloud computing library for Ruby.
4
+ # Among others, it supports Amazon S3 to store your files. In
5
+ # contrast to the outdated AWS-S3 gem it is actively maintained and
6
+ # supports multiple locations.
7
+ # Amazon's S3 file hosting service is a scalable, easy place to
8
+ # store files for distribution. You can find out more about it at
9
+ # http://aws.amazon.com/s3 There are a few fog-specific options for
10
+ # has_attached_file, which will be explained using S3 as an example:
11
+ # * +fog_credentials+: Takes a Hash with your credentials. For S3,
12
+ # you can use the following format:
13
+ # aws_access_key_id: '<your aws_access_key_id>'
14
+ # aws_secret_access_key: '<your aws_secret_access_key>'
15
+ # provider: 'AWS'
16
+ # region: 'eu-west-1'
17
+ # * +fog_directory+: This is the name of the S3 bucket that will
18
+ # store your files. Remember that the bucket must be unique across
19
+ # all of Amazon S3. If the bucket does not exist, Paperclip will
20
+ # attempt to create it.
21
+ # * +path+: This is the key under the bucket in which the file will
22
+ # be stored. The URL will be constructed from the bucket and the
23
+ # path. This is what you will want to interpolate. Keys should be
24
+ # unique, like filenames, and despite the fact that S3 (strictly
25
+ # speaking) does not support directories, you can still use a / to
26
+ # separate parts of your file name.
27
+ # * +fog_public+: (optional, defaults to true) Should the uploaded
28
+ # files be public or not? (true/false)
29
+ # * +fog_host+: (optional) The fully-qualified domain name (FQDN)
30
+ # that is the alias to the S3 domain of your bucket, e.g.
31
+ # 'http://images.example.com'. This can also be used in
32
+ # conjunction with Cloudfront (http://aws.amazon.com/cloudfront)
33
+
34
+ module Fog
35
+ def self.extended base
36
+ begin
37
+ require 'fog'
38
+ rescue LoadError => e
39
+ e.message << " (You may need to install the fog gem)"
40
+ raise e
41
+ end unless defined?(Fog)
42
+
43
+ base.instance_eval do
44
+ unless @options[:url].to_s.match(/^:fog.*url$/)
45
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url])
46
+ @options[:url] = ':fog_public_url'
47
+ end
48
+ Paperclip.interpolates(:fog_public_url) do |attachment, style|
49
+ attachment.public_url(style)
50
+ end unless Paperclip::Interpolations.respond_to? :fog_public_url
51
+ end
52
+ end
53
+
54
+ AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
55
+
56
+ def exists?(style = default_style)
57
+ if original_filename
58
+ !!directory.files.head(path(style))
59
+ else
60
+ false
61
+ end
62
+ end
63
+
64
+ def fog_credentials
65
+ @fog_credentials ||= parse_credentials(@options[:fog_credentials])
66
+ end
67
+
68
+ def fog_file
69
+ @fog_file ||= @options[:fog_file] || {}
70
+ end
71
+
72
+ def fog_public
73
+ return @fog_public if defined?(@fog_public)
74
+ @fog_public = defined?(@options[:fog_public]) ? @options[:fog_public] : true
75
+ end
76
+
77
+ def flush_writes
78
+ for style, file in @queued_for_write do
79
+ log("saving #{path(style)}")
80
+ retried = false
81
+ begin
82
+ directory.files.create(fog_file.merge(
83
+ :body => file,
84
+ :key => path(style),
85
+ :public => fog_public,
86
+ :content_type => file.content_type.to_s.strip
87
+ ))
88
+ rescue Excon::Errors::NotFound
89
+ raise if retried
90
+ retried = true
91
+ directory.save
92
+ retry
93
+ end
94
+ end
95
+
96
+ after_flush_writes # allows attachment to clean up temp files
97
+
98
+ @queued_for_write = {}
99
+ end
100
+
101
+ def flush_deletes
102
+ for path in @queued_for_delete do
103
+ log("deleting #{path}")
104
+ directory.files.new(:key => path).destroy
105
+ end
106
+ @queued_for_delete = []
107
+ end
108
+
109
+ # Returns representation of the data of the file assigned to the given
110
+ # style, in the format most representative of the current storage.
111
+ def to_file(style = default_style)
112
+ if @queued_for_write[style]
113
+ @queued_for_write[style].rewind
114
+ @queued_for_write[style]
115
+ else
116
+ body = directory.files.get(path(style)).body
117
+ filename = path(style)
118
+ extname = File.extname(filename)
119
+ basename = File.basename(filename, extname)
120
+ file = Tempfile.new([basename, extname])
121
+ file.binmode
122
+ file.write(body)
123
+ file.rewind
124
+ file
125
+ end
126
+ end
127
+
128
+ def public_url(style = default_style)
129
+ if @options[:fog_host]
130
+ host = if @options[:fog_host].respond_to?(:call)
131
+ @options[:fog_host].call(self)
132
+ else
133
+ (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
134
+ end
135
+
136
+ "#{host}/#{path(style)}"
137
+ else
138
+ if fog_credentials[:provider] == 'AWS'
139
+ if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
140
+ "https://#{@options[:fog_directory]}.s3.amazonaws.com/#{path(style)}"
141
+ else
142
+ # directory is not a valid subdomain, so use path style for access
143
+ "https://s3.amazonaws.com/#{@options[:fog_directory]}/#{path(style)}"
144
+ end
145
+ else
146
+ directory.files.new(:key => path(style)).public_url
147
+ end
148
+ end
149
+ end
150
+
151
+ def parse_credentials(creds)
152
+ creds = find_credentials(creds).stringify_keys
153
+ env = Object.const_defined?(:Rails) ? Rails.env : nil
154
+ (creds[env] || creds).symbolize_keys
155
+ end
156
+
157
+ private
158
+
159
+ def find_credentials(creds)
160
+ case creds
161
+ when File
162
+ YAML::load(ERB.new(File.read(creds.path)).result)
163
+ when String, Pathname
164
+ YAML::load(ERB.new(File.read(creds)).result)
165
+ when Hash
166
+ creds
167
+ else
168
+ if creds.respond_to?(:call)
169
+ creds.call(self)
170
+ else
171
+ raise ArgumentError, "Credentials are not a path, file, hash or proc."
172
+ end
173
+ end
174
+ end
175
+
176
+ def connection
177
+ @connection ||= ::Fog::Storage.new(fog_credentials)
178
+ end
179
+
180
+ def directory
181
+ dir = if @options[:fog_directory].respond_to?(:call)
182
+ @options[:fog_directory].call(self)
183
+ else
184
+ @options[:fog_directory]
185
+ end
186
+
187
+ @directory ||= connection.directories.new(:key => dir)
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,351 @@
1
+ module Paperclip
2
+ module Storage
3
+ # Amazon's S3 file hosting service is a scalable, easy place to store files for
4
+ # distribution. You can find out more about it at http://aws.amazon.com/s3
5
+ #
6
+ # To use Paperclip with S3, include the +aws-sdk+ gem in your Gemfile:
7
+ # gem 'aws-sdk'
8
+ # There are a few S3-specific options for has_attached_file:
9
+ # * +s3_credentials+: Takes a path, a File, or a Hash. The path (or File) must point
10
+ # to a YAML file containing the +access_key_id+ and +secret_access_key+ that Amazon
11
+ # gives you. You can 'environment-space' this just like you do to your
12
+ # database.yml file, so different environments can use different accounts:
13
+ # development:
14
+ # access_key_id: 123...
15
+ # secret_access_key: 123...
16
+ # test:
17
+ # access_key_id: abc...
18
+ # secret_access_key: abc...
19
+ # production:
20
+ # access_key_id: 456...
21
+ # secret_access_key: 456...
22
+ # This is not required, however, and the file may simply look like this:
23
+ # access_key_id: 456...
24
+ # secret_access_key: 456...
25
+ # In which case, those access keys will be used in all environments. You can also
26
+ # put your bucket name in this file, instead of adding it to the code directly.
27
+ # This is useful when you want the same account but a different bucket for
28
+ # development versus production.
29
+ # * +s3_permissions+: This is a String that should be one of the "canned" access
30
+ # policies that S3 provides (more information can be found here:
31
+ # http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAccessPolicy.html)
32
+ # The default for Paperclip is :public_read.
33
+ #
34
+ # You can set permission on a per style bases by doing the following:
35
+ # :s3_permissions => {
36
+ # :original => :private
37
+ # }
38
+ # Or globaly:
39
+ # :s3_permissions => :private
40
+ #
41
+ # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
42
+ # 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
43
+ # default), and 'https' when your :s3_permissions are anything else.
44
+ # * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as
45
+ # {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at
46
+ # runtime. Paperclip will call that Proc with attachment as the only argument.
47
+ # * +bucket+: This is the name of the S3 bucket that will store your files. Remember
48
+ # that the bucket must be unique across all of Amazon S3. If the bucket does not exist
49
+ # Paperclip will attempt to create it. The bucket name will not be interpolated.
50
+ # You can define the bucket as a Proc if you want to determine it's name at runtime.
51
+ # Paperclip will call that Proc with attachment as the only argument.
52
+ # * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
53
+ # S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
54
+ # link in the +url+ entry for more information about S3 domains and buckets.
55
+ # * +url+: There are four options for the S3 url. You can choose to have the bucket's name
56
+ # placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
57
+ # You can also specify a CNAME (which requires the CNAME to be specified as
58
+ # :s3_alias_url. You can read more about CNAMEs and S3 at
59
+ # http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
60
+ # Normally, this won't matter in the slightest and you can leave the default (which is
61
+ # path-style, or :s3_path_url). But in some cases paths don't work and you need to use
62
+ # the domain-style (:s3_domain_url). Anything else here will be treated like path-style.
63
+ # NOTE: If you use a CNAME for use with CloudFront, you can NOT specify https as your
64
+ # :s3_protocol; This is *not supported* by S3/CloudFront. Finally, when using the host
65
+ # alias, the :bucket parameter is ignored, as the hostname is used as the bucket name
66
+ # by S3. The fourth option for the S3 url is :asset_host, which uses Rails' built-in
67
+ # asset_host settings. NOTE: To get the full url from a paperclip'd object, use the
68
+ # image_path helper; this is what image_tag uses to generate the url for an img tag.
69
+ # * +path+: This is the key under the bucket in which the file will be stored. The
70
+ # URL will be constructed from the bucket and the path. This is what you will want
71
+ # to interpolate. Keys should be unique, like filenames, and despite the fact that
72
+ # S3 (strictly speaking) does not support directories, you can still use a / to
73
+ # separate parts of your file name.
74
+ # * +s3_host_name+: If you are using your bucket in Tokyo region etc, write host_name.
75
+ # * +s3_metadata+: These key/value pairs will be stored with the
76
+ # object. This option works by prefixing each key with
77
+ # "x-amz-meta-" before sending it as a header on the object
78
+ # upload request.
79
+ # * +s3_storage_class+: If this option is set to
80
+ # <tt>:reduced_redundancy</tt>, the object will be stored using Reduced
81
+ # Redundancy Storage. RRS enables customers to reduce their
82
+ # costs by storing non-critical, reproducible data at lower
83
+ # levels of redundancy than Amazon S3's standard storage.
84
+ module S3
85
+ def self.extended base
86
+ begin
87
+ require 'aws-sdk'
88
+ rescue LoadError => e
89
+ e.message << " (You may need to install the aws-sdk gem)"
90
+ raise e
91
+ end unless defined?(AWS::Core)
92
+
93
+ base.instance_eval do
94
+ @s3_options = @options[:s3_options] || {}
95
+ @s3_permissions = set_permissions(@options[:s3_permissions])
96
+ @s3_protocol = @options[:s3_protocol] ||
97
+ Proc.new do |style, attachment|
98
+ permission = (@s3_permissions[style.to_sym] || @s3_permissions[:default])
99
+ permission = permission.call(attachment, style) if permission.is_a?(Proc)
100
+ (permission == :public_read) ? 'http' : 'https'
101
+ end
102
+ @s3_metadata = @options[:s3_metadata] || {}
103
+ @s3_headers = @options[:s3_headers] || {}
104
+ @s3_headers = @s3_headers.call(instance) if @s3_headers.is_a?(Proc)
105
+ @s3_headers = (@s3_headers).inject({}) do |headers,(name,value)|
106
+ case name.to_s
107
+ when /^x-amz-meta-(.*)/i
108
+ @s3_metadata[$1.downcase] = value
109
+ else
110
+ name = name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym
111
+ headers[name] = value
112
+ end
113
+ headers
114
+ end
115
+
116
+ @s3_headers[:storage_class] = @options[:s3_storage_class] if @options[:s3_storage_class]
117
+
118
+ @s3_server_side_encryption = @options[:s3_server_side_encryption]
119
+
120
+ unless @options[:url].to_s.match(/^:s3.*url$/) || @options[:url] == ":asset_host"
121
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/^:rails_root\/public\/system/, '')
122
+ @options[:url] = ":s3_path_url"
123
+ end
124
+ @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
125
+
126
+ @http_proxy = @options[:http_proxy] || nil
127
+ end
128
+ Paperclip.interpolates(:s3_alias_url) do |attachment, style|
129
+ "#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
130
+ end unless Paperclip::Interpolations.respond_to? :s3_alias_url
131
+ Paperclip.interpolates(:s3_path_url) do |attachment, style|
132
+ "#{attachment.s3_protocol(style)}://#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
133
+ end unless Paperclip::Interpolations.respond_to? :s3_path_url
134
+ Paperclip.interpolates(:s3_domain_url) do |attachment, style|
135
+ "#{attachment.s3_protocol(style)}://#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
136
+ end unless Paperclip::Interpolations.respond_to? :s3_domain_url
137
+ Paperclip.interpolates(:asset_host) do |attachment, style|
138
+ "#{attachment.path(style).gsub(%r{^/}, "")}"
139
+ end unless Paperclip::Interpolations.respond_to? :asset_host
140
+ end
141
+
142
+ def expiring_url(time = 3600, style_name = default_style)
143
+ if path
144
+ s3_object(style_name).url_for(:read, :expires => time, :secure => use_secure_protocol?(style_name)).to_s
145
+ end
146
+ end
147
+
148
+ def s3_credentials
149
+ @s3_credentials ||= parse_credentials(@options[:s3_credentials])
150
+ end
151
+
152
+ def s3_host_name
153
+ host_name = @options[:s3_host_name]
154
+ host_name = host_name.call(self) if host_name.is_a?(Proc)
155
+
156
+ host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
157
+ end
158
+
159
+ def s3_host_alias
160
+ @s3_host_alias = @options[:s3_host_alias]
161
+ @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
162
+ @s3_host_alias
163
+ end
164
+
165
+ def bucket_name
166
+ @bucket = @options[:bucket] || s3_credentials[:bucket]
167
+ @bucket = @bucket.call(self) if @bucket.is_a?(Proc)
168
+ @bucket or raise ArgumentError, "missing required :bucket option"
169
+ end
170
+
171
+ def s3_interface
172
+ @s3_interface ||= begin
173
+ config = { :s3_endpoint => s3_host_name }
174
+
175
+ if using_http_proxy?
176
+
177
+ proxy_opts = { :host => http_proxy_host }
178
+ proxy_opts[:port] = http_proxy_port if http_proxy_port
179
+ if http_proxy_user
180
+ userinfo = http_proxy_user.to_s
181
+ userinfo += ":#{http_proxy_password}" if http_proxy_password
182
+ proxy_opts[:userinfo] = userinfo
183
+ end
184
+ config[:proxy_uri] = URI::HTTP.build(proxy_opts)
185
+ end
186
+
187
+ [:access_key_id, :secret_access_key].each do |opt|
188
+ config[opt] = s3_credentials[opt] if s3_credentials[opt]
189
+ end
190
+
191
+ AWS::S3.new(config.merge(@s3_options))
192
+ end
193
+ end
194
+
195
+ def s3_bucket
196
+ @s3_bucket ||= s3_interface.buckets[bucket_name]
197
+ end
198
+
199
+ def s3_object style_name = default_style
200
+ s3_bucket.objects[path(style_name).sub(%r{^/},'')]
201
+ end
202
+
203
+ def using_http_proxy?
204
+ !!@http_proxy
205
+ end
206
+
207
+ def http_proxy_host
208
+ using_http_proxy? ? @http_proxy[:host] : nil
209
+ end
210
+
211
+ def http_proxy_port
212
+ using_http_proxy? ? @http_proxy[:port] : nil
213
+ end
214
+
215
+ def http_proxy_user
216
+ using_http_proxy? ? @http_proxy[:user] : nil
217
+ end
218
+
219
+ def http_proxy_password
220
+ using_http_proxy? ? @http_proxy[:password] : nil
221
+ end
222
+
223
+ def set_permissions permissions
224
+ if permissions.is_a?(Hash)
225
+ permissions[:default] = permissions[:default] || :public_read
226
+ else
227
+ permissions = { :default => permissions || :public_read }
228
+ end
229
+ permissions
230
+ end
231
+
232
+ def parse_credentials creds
233
+ creds = find_credentials(creds).stringify_keys
234
+ env = Object.const_defined?(:Rails) ? Rails.env : nil
235
+ (creds[env] || creds).symbolize_keys
236
+ end
237
+
238
+ def exists?(style = default_style)
239
+ if original_filename
240
+ s3_object(style).exists?
241
+ else
242
+ false
243
+ end
244
+ rescue AWS::Errors::Base => e
245
+ false
246
+ end
247
+
248
+ def s3_permissions(style = default_style)
249
+ s3_permissions = @s3_permissions[style] || @s3_permissions[:default]
250
+ s3_permissions = s3_permissions.call(self, style) if s3_permissions.is_a?(Proc)
251
+ s3_permissions
252
+ end
253
+
254
+ def s3_protocol(style = default_style)
255
+ if @s3_protocol.is_a?(Proc)
256
+ @s3_protocol.call(style, self)
257
+ else
258
+ @s3_protocol
259
+ end
260
+ end
261
+
262
+ # Returns representation of the data of the file assigned to the given
263
+ # style, in the format most representative of the current storage.
264
+ def to_file style = default_style
265
+ if @queued_for_write[style]
266
+ @queued_for_write[style].rewind
267
+ return @queued_for_write[style]
268
+ end
269
+ filename = path(style)
270
+ extname = File.extname(filename)
271
+ basename = File.basename(filename, extname)
272
+ file = Tempfile.new([basename, extname])
273
+ file.binmode
274
+ file.write(s3_object(style).read)
275
+ file.rewind
276
+ return file
277
+ end
278
+
279
+ def create_bucket
280
+ s3_interface.buckets.create(bucket_name)
281
+ end
282
+
283
+ def flush_writes #:nodoc:
284
+ @queued_for_write.each do |style, file|
285
+ begin
286
+ log("saving #{path(style)}")
287
+ acl = @s3_permissions[style] || @s3_permissions[:default]
288
+ acl = acl.call(self, style) if acl.respond_to?(:call)
289
+ write_options = {
290
+ :content_type => file.content_type.to_s.strip,
291
+ :acl => acl
292
+ }
293
+ write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
294
+ unless @s3_server_side_encryption.blank?
295
+ write_options[:server_side_encryption] = @s3_server_side_encryption
296
+ end
297
+ write_options.merge!(@s3_headers)
298
+ write_options.merge!(:file => file.path)
299
+ s3_object(style).write(write_options)
300
+ rescue AWS::S3::Errors::NoSuchBucket => e
301
+ create_bucket
302
+ retry
303
+ end
304
+ end
305
+
306
+ after_flush_writes # allows attachment to clean up temp files
307
+
308
+ @queued_for_write = {}
309
+ end
310
+
311
+ def flush_deletes #:nodoc:
312
+ @queued_for_delete.each do |path|
313
+ begin
314
+ log("deleting #{path}")
315
+ s3_bucket.objects[path.sub(%r{^/},'')].delete
316
+ rescue AWS::Errors::Base => e
317
+ # Ignore this.
318
+ end
319
+ end
320
+ @queued_for_delete = []
321
+ end
322
+
323
+ def find_credentials creds
324
+ case creds
325
+ when File
326
+ YAML::load(ERB.new(File.read(creds.path)).result)
327
+ when String, Pathname
328
+ YAML::load(ERB.new(File.read(creds)).result)
329
+ when Hash
330
+ creds
331
+ else
332
+ raise ArgumentError, "Credentials are not a path, file, or hash."
333
+ end
334
+ end
335
+ private :find_credentials
336
+
337
+ def establish_connection!
338
+ @connection ||= AWS::S3::Base.establish_connection!( @s3_options.merge(
339
+ :access_key_id => s3_credentials[:access_key_id],
340
+ :secret_access_key => s3_credentials[:secret_access_key]
341
+ ))
342
+ end
343
+ private :establish_connection!
344
+
345
+ def use_secure_protocol?(style_name)
346
+ s3_protocol(style_name) == "https"
347
+ end
348
+ private :use_secure_protocol?
349
+ end
350
+ end
351
+ end