kt-paperclip 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.gitignore +19 -0
  5. data/.hound.yml +1050 -0
  6. data/.rubocop.yml +1 -0
  7. data/.travis.yml +47 -0
  8. data/Appraisals +24 -0
  9. data/CONTRIBUTING.md +86 -0
  10. data/Gemfile +18 -0
  11. data/LICENSE +24 -0
  12. data/NEWS +515 -0
  13. data/README.md +1053 -0
  14. data/RELEASING.md +17 -0
  15. data/Rakefile +52 -0
  16. data/UPGRADING +17 -0
  17. data/features/basic_integration.feature +85 -0
  18. data/features/migration.feature +29 -0
  19. data/features/rake_tasks.feature +62 -0
  20. data/features/step_definitions/attachment_steps.rb +110 -0
  21. data/features/step_definitions/html_steps.rb +15 -0
  22. data/features/step_definitions/rails_steps.rb +257 -0
  23. data/features/step_definitions/s3_steps.rb +14 -0
  24. data/features/step_definitions/web_steps.rb +106 -0
  25. data/features/support/env.rb +12 -0
  26. data/features/support/fakeweb.rb +11 -0
  27. data/features/support/file_helpers.rb +34 -0
  28. data/features/support/fixtures/boot_config.txt +15 -0
  29. data/features/support/fixtures/gemfile.txt +5 -0
  30. data/features/support/fixtures/preinitializer.txt +20 -0
  31. data/features/support/paths.rb +28 -0
  32. data/features/support/rails.rb +39 -0
  33. data/features/support/selectors.rb +19 -0
  34. data/gemfiles/4.2.gemfile +20 -0
  35. data/gemfiles/5.0.gemfile +20 -0
  36. data/gemfiles/5.1.gemfile +20 -0
  37. data/gemfiles/5.2.gemfile +20 -0
  38. data/gemfiles/6.0.gemfile +20 -0
  39. data/lib/generators/paperclip/USAGE +8 -0
  40. data/lib/generators/paperclip/paperclip_generator.rb +36 -0
  41. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
  42. data/lib/paperclip.rb +215 -0
  43. data/lib/paperclip/attachment.rb +617 -0
  44. data/lib/paperclip/attachment_registry.rb +60 -0
  45. data/lib/paperclip/callbacks.rb +42 -0
  46. data/lib/paperclip/content_type_detector.rb +80 -0
  47. data/lib/paperclip/errors.rb +34 -0
  48. data/lib/paperclip/file_command_content_type_detector.rb +28 -0
  49. data/lib/paperclip/filename_cleaner.rb +15 -0
  50. data/lib/paperclip/geometry.rb +157 -0
  51. data/lib/paperclip/geometry_detector_factory.rb +45 -0
  52. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  53. data/lib/paperclip/glue.rb +17 -0
  54. data/lib/paperclip/has_attached_file.rb +116 -0
  55. data/lib/paperclip/helpers.rb +60 -0
  56. data/lib/paperclip/interpolations.rb +201 -0
  57. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  58. data/lib/paperclip/io_adapters/abstract_adapter.rb +75 -0
  59. data/lib/paperclip/io_adapters/attachment_adapter.rb +47 -0
  60. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  61. data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
  62. data/lib/paperclip/io_adapters/file_adapter.rb +26 -0
  63. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  64. data/lib/paperclip/io_adapters/identity_adapter.rb +17 -0
  65. data/lib/paperclip/io_adapters/nil_adapter.rb +37 -0
  66. data/lib/paperclip/io_adapters/registry.rb +36 -0
  67. data/lib/paperclip/io_adapters/stringio_adapter.rb +36 -0
  68. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +44 -0
  69. data/lib/paperclip/io_adapters/uri_adapter.rb +68 -0
  70. data/lib/paperclip/locales/en.yml +18 -0
  71. data/lib/paperclip/logger.rb +21 -0
  72. data/lib/paperclip/matchers.rb +64 -0
  73. data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
  74. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +101 -0
  75. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
  76. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +97 -0
  77. data/lib/paperclip/media_type_spoof_detector.rb +90 -0
  78. data/lib/paperclip/missing_attachment_styles.rb +84 -0
  79. data/lib/paperclip/processor.rb +56 -0
  80. data/lib/paperclip/processor_helpers.rb +52 -0
  81. data/lib/paperclip/rails_environment.rb +21 -0
  82. data/lib/paperclip/railtie.rb +31 -0
  83. data/lib/paperclip/schema.rb +81 -0
  84. data/lib/paperclip/storage.rb +3 -0
  85. data/lib/paperclip/storage/filesystem.rb +99 -0
  86. data/lib/paperclip/storage/fog.rb +252 -0
  87. data/lib/paperclip/storage/s3.rb +461 -0
  88. data/lib/paperclip/style.rb +106 -0
  89. data/lib/paperclip/tempfile.rb +42 -0
  90. data/lib/paperclip/tempfile_factory.rb +22 -0
  91. data/lib/paperclip/thumbnail.rb +131 -0
  92. data/lib/paperclip/url_generator.rb +76 -0
  93. data/lib/paperclip/validators.rb +73 -0
  94. data/lib/paperclip/validators/attachment_content_type_validator.rb +88 -0
  95. data/lib/paperclip/validators/attachment_file_name_validator.rb +75 -0
  96. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +28 -0
  97. data/lib/paperclip/validators/attachment_presence_validator.rb +28 -0
  98. data/lib/paperclip/validators/attachment_size_validator.rb +109 -0
  99. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +29 -0
  100. data/lib/paperclip/version.rb +3 -0
  101. data/lib/tasks/paperclip.rake +140 -0
  102. data/paperclip.gemspec +50 -0
  103. data/shoulda_macros/paperclip.rb +134 -0
  104. data/spec/database.yml +4 -0
  105. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  106. data/spec/paperclip/attachment_processing_spec.rb +79 -0
  107. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  108. data/spec/paperclip/attachment_spec.rb +1590 -0
  109. data/spec/paperclip/content_type_detector_spec.rb +47 -0
  110. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  111. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  112. data/spec/paperclip/geometry_detector_spec.rb +38 -0
  113. data/spec/paperclip/geometry_parser_spec.rb +73 -0
  114. data/spec/paperclip/geometry_spec.rb +255 -0
  115. data/spec/paperclip/glue_spec.rb +42 -0
  116. data/spec/paperclip/has_attached_file_spec.rb +78 -0
  117. data/spec/paperclip/integration_spec.rb +702 -0
  118. data/spec/paperclip/interpolations_spec.rb +270 -0
  119. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  120. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +140 -0
  121. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +88 -0
  122. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  123. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  124. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +137 -0
  125. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  126. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
  127. data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
  128. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  129. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  130. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +221 -0
  131. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  132. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +108 -0
  133. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  134. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  135. data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
  136. data/spec/paperclip/meta_class_spec.rb +30 -0
  137. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +88 -0
  138. data/spec/paperclip/paperclip_spec.rb +196 -0
  139. data/spec/paperclip/plural_cache_spec.rb +37 -0
  140. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  141. data/spec/paperclip/processor_spec.rb +26 -0
  142. data/spec/paperclip/rails_environment_spec.rb +30 -0
  143. data/spec/paperclip/rake_spec.rb +103 -0
  144. data/spec/paperclip/schema_spec.rb +252 -0
  145. data/spec/paperclip/storage/filesystem_spec.rb +79 -0
  146. data/spec/paperclip/storage/fog_spec.rb +560 -0
  147. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  148. data/spec/paperclip/storage/s3_spec.rb +1695 -0
  149. data/spec/paperclip/style_spec.rb +251 -0
  150. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  151. data/spec/paperclip/tempfile_spec.rb +35 -0
  152. data/spec/paperclip/thumbnail_spec.rb +504 -0
  153. data/spec/paperclip/url_generator_spec.rb +221 -0
  154. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  155. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +159 -0
  156. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
  157. data/spec/paperclip/validators/attachment_size_validator_spec.rb +235 -0
  158. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +48 -0
  159. data/spec/paperclip/validators_spec.rb +164 -0
  160. data/spec/spec_helper.rb +45 -0
  161. data/spec/support/assertions.rb +84 -0
  162. data/spec/support/fake_model.rb +24 -0
  163. data/spec/support/fake_rails.rb +12 -0
  164. data/spec/support/fixtures/12k.png +0 -0
  165. data/spec/support/fixtures/50x50.png +0 -0
  166. data/spec/support/fixtures/5k.png +0 -0
  167. data/spec/support/fixtures/animated +0 -0
  168. data/spec/support/fixtures/animated.gif +0 -0
  169. data/spec/support/fixtures/animated.unknown +0 -0
  170. data/spec/support/fixtures/bad.png +1 -0
  171. data/spec/support/fixtures/empty.html +1 -0
  172. data/spec/support/fixtures/empty.xlsx +0 -0
  173. data/spec/support/fixtures/fog.yml +8 -0
  174. data/spec/support/fixtures/rotated.jpg +0 -0
  175. data/spec/support/fixtures/s3.yml +8 -0
  176. data/spec/support/fixtures/spaced file.jpg +0 -0
  177. data/spec/support/fixtures/spaced file.png +0 -0
  178. data/spec/support/fixtures/text.txt +1 -0
  179. data/spec/support/fixtures/twopage.pdf +0 -0
  180. data/spec/support/fixtures/uppercase.PNG +0 -0
  181. data/spec/support/matchers/accept.rb +5 -0
  182. data/spec/support/matchers/exist.rb +5 -0
  183. data/spec/support/matchers/have_column.rb +23 -0
  184. data/spec/support/mock_attachment.rb +24 -0
  185. data/spec/support/mock_interpolator.rb +24 -0
  186. data/spec/support/mock_url_generator_builder.rb +26 -0
  187. data/spec/support/model_reconstruction.rb +72 -0
  188. data/spec/support/reporting.rb +11 -0
  189. data/spec/support/test_data.rb +13 -0
  190. data/spec/support/version_helper.rb +9 -0
  191. metadata +586 -0
@@ -0,0 +1,461 @@
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-s3+ gem in your Gemfile:
7
+ # gem 'aws-sdk-s3'
8
+ # There are a few S3-specific options for has_attached_file:
9
+ # * +s3_credentials+: Takes a path, a File, a Hash or a Proc. 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
+ # When using a Proc it provides a single parameter which is the attachment itself. A
30
+ # method #instance is available on the attachment which will take you back to your
31
+ # code. eg.
32
+ # class User
33
+ # has_attached_file :download,
34
+ # :storage => :s3,
35
+ # :s3_credentials => Proc.new{|a| a.instance.s3_credentials }
36
+ #
37
+ # def s3_credentials
38
+ # {:bucket => "xxx", :access_key_id => "xxx", :secret_access_key => "xxx"}
39
+ # end
40
+ # end
41
+ # * +s3_permissions+: This is a String that should be one of the "canned" access
42
+ # policies that S3 provides (more information can be found here:
43
+ # http://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html)
44
+ # The default for Paperclip is public-read.
45
+ #
46
+ # You can set permission on a per style bases by doing the following:
47
+ # :s3_permissions => {
48
+ # :original => "private"
49
+ # }
50
+ # Or globally:
51
+ # :s3_permissions => "private"
52
+ #
53
+ # * +s3_protocol+: The protocol for the URLs generated to your S3 assets.
54
+ # Can be either 'http', 'https', or an empty string to generate
55
+ # protocol-relative URLs. Defaults to empty string.
56
+ # * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as
57
+ # {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at
58
+ # runtime. Paperclip will call that Proc with attachment as the only argument.
59
+ # Can be defined both globally and within a style-specific hash.
60
+ # * +bucket+: This is the name of the S3 bucket that will store your files. Remember
61
+ # that the bucket must be unique across all of Amazon S3. If the bucket does not exist
62
+ # Paperclip will attempt to create it. The bucket name will not be interpolated.
63
+ # You can define the bucket as a Proc if you want to determine its name at runtime.
64
+ # Paperclip will call that Proc with attachment as the only argument.
65
+ # * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
66
+ # S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
67
+ # link in the +url+ entry for more information about S3 domains and buckets.
68
+ # * +s3_prefixes_in_alias+: The number of prefixes that is prepended by
69
+ # s3_host_alias. This will remove the prefixes from the path in
70
+ # :s3_alias_url url interpolation
71
+ # * +url+: There are four options for the S3 url. You can choose to have the bucket's name
72
+ # placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
73
+ # You can also specify a CNAME (which requires the CNAME to be specified as
74
+ # :s3_alias_url. You can read more about CNAMEs and S3 at
75
+ # http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
76
+ # Normally, this won't matter in the slightest and you can leave the default (which is
77
+ # path-style, or :s3_path_url). But in some cases paths don't work and you need to use
78
+ # the domain-style (:s3_domain_url). Anything else here will be treated like path-style.
79
+ #
80
+ # Notes:
81
+ # * The value of this option is a string, not a symbol.
82
+ # <b>right:</b> <tt>":s3_domain_url"</tt>
83
+ # <b>wrong:</b> <tt>:s3_domain_url</tt>
84
+ # * If you use a CNAME for use with CloudFront, you can NOT specify https as your
85
+ # :s3_protocol;
86
+ # This is *not supported* by S3/CloudFront. Finally, when using the host
87
+ # alias, the :bucket parameter is ignored, as the hostname is used as the bucket name
88
+ # by S3. The fourth option for the S3 url is :asset_host, which uses Rails' built-in
89
+ # asset_host settings.
90
+ # * To get the full url from a paperclip'd object, use the
91
+ # image_path helper; this is what image_tag uses to generate the url for an img tag.
92
+ # * +path+: This is the key under the bucket in which the file will be stored. The
93
+ # URL will be constructed from the bucket and the path. This is what you will want
94
+ # to interpolate. Keys should be unique, like filenames, and despite the fact that
95
+ # S3 (strictly speaking) does not support directories, you can still use a / to
96
+ # separate parts of your file name.
97
+ # * +s3_host_name+: If you are using your bucket in Tokyo region
98
+ # etc, write host_name (e.g., 's3-ap-northeast-1.amazonaws.com').
99
+ # * +s3_region+: For aws-sdk-s3, s3_region is required.
100
+ # * +s3_metadata+: These key/value pairs will be stored with the
101
+ # object. This option works by prefixing each key with
102
+ # "x-amz-meta-" before sending it as a header on the object
103
+ # upload request. Can be defined both globally and within a style-specific hash.
104
+ # * +s3_storage_class+: If this option is set to
105
+ # <tt>:REDUCED_REDUNDANCY</tt>, the object will be stored using Reduced
106
+ # Redundancy Storage. RRS enables customers to reduce their
107
+ # costs by storing non-critical, reproducible data at lower
108
+ # levels of redundancy than Amazon S3's standard storage.
109
+ # * +use_accelerate_endpoint+: Use accelerate endpoint
110
+ # http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html
111
+ #
112
+ # You can set storage class on a per style bases by doing the following:
113
+ # :s3_storage_class => {
114
+ # :thumb => :REDUCED_REDUNDANCY
115
+ # }
116
+ #
117
+ # Or globally:
118
+ # :s3_storage_class => :REDUCED_REDUNDANCY
119
+ #
120
+ # Other storage classes, such as <tt>:STANDARD_IA</tt>, are also available—see the
121
+ # documentation for the <tt>aws-sdk-s3</tt> gem for the full list.
122
+
123
+ module S3
124
+ def self.extended(base)
125
+ begin
126
+ require "aws-sdk-s3"
127
+ rescue LoadError => e
128
+ e.message << " (You may need to install the aws-sdk-s3 gem)"
129
+ raise e
130
+ end
131
+
132
+ base.instance_eval do
133
+ @s3_options = @options[:s3_options] || {}
134
+ @s3_permissions = set_permissions(@options[:s3_permissions])
135
+ @s3_protocol = @options[:s3_protocol] || ""
136
+ @s3_metadata = @options[:s3_metadata] || {}
137
+ @s3_headers = {}
138
+ merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
139
+
140
+ @s3_storage_class = set_storage_class(@options[:s3_storage_class])
141
+
142
+ @s3_server_side_encryption = "AES256"
143
+ @s3_server_side_encryption = false if @options[:s3_server_side_encryption].blank?
144
+ @s3_server_side_encryption = @options[:s3_server_side_encryption] if @s3_server_side_encryption
145
+
146
+ unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host"
147
+ @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "")
148
+ @options[:url] = ":s3_path_url"
149
+ end
150
+ @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
151
+
152
+ @http_proxy = @options[:http_proxy] || nil
153
+
154
+ @use_accelerate_endpoint = @options[:use_accelerate_endpoint]
155
+ end
156
+
157
+ unless Paperclip::Interpolations.respond_to? :s3_alias_url
158
+ Paperclip.interpolates(:s3_alias_url) do |attachment, style|
159
+ protocol = attachment.s3_protocol(style, true)
160
+ host = attachment.s3_host_alias
161
+ path = attachment.path(style).
162
+ split("/")[attachment.s3_prefixes_in_alias..-1].
163
+ join("/").
164
+ sub(%r{\A/}, "")
165
+ "#{protocol}//#{host}/#{path}"
166
+ end
167
+ end
168
+ unless Paperclip::Interpolations.respond_to? :s3_path_url
169
+ Paperclip.interpolates(:s3_path_url) do |attachment, style|
170
+ "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, '')}"
171
+ end
172
+ end
173
+ unless Paperclip::Interpolations.respond_to? :s3_domain_url
174
+ Paperclip.interpolates(:s3_domain_url) do |attachment, style|
175
+ "#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).sub(%r{\A/}, '')}"
176
+ end
177
+ end
178
+ unless Paperclip::Interpolations.respond_to? :asset_host
179
+ Paperclip.interpolates(:asset_host) do |attachment, style|
180
+ attachment.path(style).sub(%r{\A/}, "").to_s
181
+ end
182
+ end
183
+ end
184
+
185
+ def expiring_url(time = 3600, style_name = default_style)
186
+ if path(style_name)
187
+ base_options = { expires_in: time }
188
+ s3_object(style_name).presigned_url(
189
+ :get,
190
+ base_options.merge(s3_url_options)
191
+ ).to_s
192
+ else
193
+ url(style_name)
194
+ end
195
+ end
196
+
197
+ def s3_credentials
198
+ @s3_credentials ||= parse_credentials(@options[:s3_credentials])
199
+ end
200
+
201
+ def s3_host_name
202
+ host_name = @options[:s3_host_name]
203
+ host_name = host_name.call(self) if host_name.is_a?(Proc)
204
+
205
+ host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
206
+ end
207
+
208
+ def s3_region
209
+ region = @options[:s3_region]
210
+ region = region.call(self) if region.is_a?(Proc)
211
+
212
+ region || s3_credentials[:s3_region]
213
+ end
214
+
215
+ def s3_host_alias
216
+ @s3_host_alias = @options[:s3_host_alias]
217
+ @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.respond_to?(:call)
218
+ @s3_host_alias
219
+ end
220
+
221
+ def s3_prefixes_in_alias
222
+ @s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
223
+ end
224
+
225
+ def s3_url_options
226
+ s3_url_options = @options[:s3_url_options] || {}
227
+ s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
228
+ s3_url_options
229
+ end
230
+
231
+ def bucket_name
232
+ @bucket = @options[:bucket] || s3_credentials[:bucket]
233
+ @bucket = @bucket.call(self) if @bucket.respond_to?(:call)
234
+ @bucket || raise(ArgumentError, "missing required :bucket option")
235
+ end
236
+
237
+ def s3_interface
238
+ @s3_interface ||= begin
239
+ config = { region: s3_region }
240
+
241
+ if using_http_proxy?
242
+
243
+ proxy_opts = { host: http_proxy_host }
244
+ proxy_opts[:port] = http_proxy_port if http_proxy_port
245
+ if http_proxy_user
246
+ userinfo = http_proxy_user.to_s
247
+ userinfo += ":#{http_proxy_password}" if http_proxy_password
248
+ proxy_opts[:userinfo] = userinfo
249
+ end
250
+ config[:proxy_uri] = URI::HTTP.build(proxy_opts)
251
+ end
252
+
253
+ config[:use_accelerate_endpoint] = use_accelerate_endpoint?
254
+
255
+ [:access_key_id, :secret_access_key, :credential_provider, :credentials].each do |opt|
256
+ config[opt] = s3_credentials[opt] if s3_credentials[opt]
257
+ end
258
+
259
+ obtain_s3_instance_for(config.merge(@s3_options))
260
+ end
261
+ end
262
+
263
+ def obtain_s3_instance_for(options)
264
+ instances = (Thread.current[:paperclip_s3_instances] ||= {})
265
+ instances[options] ||= ::Aws::S3::Resource.new(options)
266
+ end
267
+
268
+ def s3_bucket
269
+ @s3_bucket ||= s3_interface.bucket(bucket_name)
270
+ end
271
+
272
+ def style_name_as_path(style_name)
273
+ path(style_name).sub(%r{\A/}, "")
274
+ end
275
+
276
+ def s3_object(style_name = default_style)
277
+ s3_bucket.object style_name_as_path(style_name)
278
+ end
279
+
280
+ def use_accelerate_endpoint?
281
+ !!@use_accelerate_endpoint
282
+ end
283
+
284
+ def using_http_proxy?
285
+ !!@http_proxy
286
+ end
287
+
288
+ def http_proxy_host
289
+ using_http_proxy? ? @http_proxy[:host] : nil
290
+ end
291
+
292
+ def http_proxy_port
293
+ using_http_proxy? ? @http_proxy[:port] : nil
294
+ end
295
+
296
+ def http_proxy_user
297
+ using_http_proxy? ? @http_proxy[:user] : nil
298
+ end
299
+
300
+ def http_proxy_password
301
+ using_http_proxy? ? @http_proxy[:password] : nil
302
+ end
303
+
304
+ def set_permissions(permissions)
305
+ permissions = { default: permissions } unless permissions.respond_to?(:merge)
306
+ permissions.merge default: (permissions[:default] || :"public-read")
307
+ end
308
+
309
+ def set_storage_class(storage_class)
310
+ storage_class = { default: storage_class } unless storage_class.respond_to?(:merge)
311
+ storage_class
312
+ end
313
+
314
+ def parse_credentials(creds)
315
+ creds = creds.respond_to?(:call) ? creds.call(self) : creds
316
+ creds = find_credentials(creds).stringify_keys
317
+ (creds[RailsEnvironment.get] || creds).symbolize_keys
318
+ end
319
+
320
+ def exists?(style = default_style)
321
+ if original_filename
322
+ s3_object(style).exists?
323
+ else
324
+ false
325
+ end
326
+ rescue Aws::Errors::ServiceError => e
327
+ false
328
+ end
329
+
330
+ def s3_permissions(style = default_style)
331
+ s3_permissions = @s3_permissions[style] || @s3_permissions[:default]
332
+ s3_permissions = s3_permissions.call(self, style) if s3_permissions.respond_to?(:call)
333
+ s3_permissions
334
+ end
335
+
336
+ def s3_storage_class(style = default_style)
337
+ @s3_storage_class[style] || @s3_storage_class[:default]
338
+ end
339
+
340
+ def s3_protocol(style = default_style, with_colon = false)
341
+ protocol = @s3_protocol
342
+ protocol = protocol.call(style, self) if protocol.respond_to?(:call)
343
+
344
+ if with_colon && !protocol.empty?
345
+ "#{protocol}:"
346
+ else
347
+ protocol.to_s
348
+ end
349
+ end
350
+
351
+ def create_bucket
352
+ s3_interface.bucket(bucket_name).create
353
+ end
354
+
355
+ def flush_writes #:nodoc:
356
+ @queued_for_write.each do |style, file|
357
+ retries = 0
358
+ begin
359
+ log("saving #{path(style)}")
360
+ write_options = {
361
+ content_type: file.content_type,
362
+ acl: s3_permissions(style)
363
+ }
364
+
365
+ # add storage class for this style if defined
366
+ storage_class = s3_storage_class(style)
367
+ write_options.merge!(storage_class: storage_class) if storage_class
368
+
369
+ write_options[:server_side_encryption] = @s3_server_side_encryption if @s3_server_side_encryption
370
+
371
+ style_specific_options = styles[style]
372
+
373
+ if style_specific_options
374
+ if style_specific_options[:s3_headers]
375
+ merge_s3_headers(style_specific_options[:s3_headers], @s3_headers, @s3_metadata)
376
+ end
377
+ @s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
378
+ end
379
+
380
+ write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
381
+ write_options.merge!(@s3_headers)
382
+
383
+ s3_object(style).upload_file(file.path, write_options)
384
+ rescue ::Aws::S3::Errors::NoSuchBucket
385
+ create_bucket
386
+ retry
387
+ rescue ::Aws::S3::Errors::SlowDown
388
+ retries += 1
389
+ if retries <= 5
390
+ sleep((2**retries) * 0.5)
391
+ retry
392
+ else
393
+ raise
394
+ end
395
+ ensure
396
+ file.rewind
397
+ end
398
+ end
399
+
400
+ after_flush_writes # allows attachment to clean up temp files
401
+
402
+ @queued_for_write = {}
403
+ end
404
+
405
+ def flush_deletes #:nodoc:
406
+ @queued_for_delete.each do |path|
407
+ begin
408
+ log("deleting #{path}")
409
+ s3_bucket.object(path.sub(%r{\A/}, "")).delete
410
+ rescue Aws::Errors::ServiceError => e
411
+ # Ignore this.
412
+ end
413
+ end
414
+ @queued_for_delete = []
415
+ end
416
+
417
+ def copy_to_local_file(style, local_dest_path)
418
+ log("copying #{path(style)} to local file #{local_dest_path}")
419
+ s3_object(style).download_file(local_dest_path)
420
+ rescue Aws::Errors::ServiceError => e
421
+ warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
422
+ false
423
+ end
424
+
425
+ private
426
+
427
+ def find_credentials(creds)
428
+ case creds
429
+ when File
430
+ YAML::safe_load(ERB.new(File.read(creds.path)).result)
431
+ when String, Pathname
432
+ YAML::safe_load(ERB.new(File.read(creds)).result)
433
+ when Hash
434
+ creds
435
+ when NilClass
436
+ {}
437
+ else
438
+ raise ArgumentError, "Credentials given are not a path, file, proc, or hash."
439
+ end
440
+ end
441
+
442
+ def use_secure_protocol?(style_name)
443
+ s3_protocol(style_name) == "https"
444
+ end
445
+
446
+ def merge_s3_headers(http_headers, s3_headers, s3_metadata)
447
+ return if http_headers.nil?
448
+
449
+ http_headers = http_headers.call(instance) if http_headers.respond_to?(:call)
450
+ http_headers.inject({}) do |_headers, (name, value)|
451
+ case name.to_s
452
+ when /\Ax-amz-meta-(.*)/i
453
+ s3_metadata[$1.downcase] = value
454
+ else
455
+ s3_headers[name.to_s.downcase.sub(/\Ax-amz-/, "").tr("-", "_").to_sym] = value
456
+ end
457
+ end
458
+ end
459
+ end
460
+ end
461
+ end