paperclip 4.3.7 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.hound.yml +5 -16
  4. data/.travis.yml +15 -12
  5. data/Appraisals +4 -8
  6. data/CONTRIBUTING.md +16 -5
  7. data/Gemfile +3 -8
  8. data/LICENSE +1 -1
  9. data/NEWS +105 -31
  10. data/README.md +239 -153
  11. data/Rakefile +1 -1
  12. data/UPGRADING +12 -9
  13. data/features/basic_integration.feature +3 -2
  14. data/features/migration.feature +0 -24
  15. data/features/step_definitions/attachment_steps.rb +6 -6
  16. data/features/step_definitions/rails_steps.rb +29 -28
  17. data/features/step_definitions/s3_steps.rb +2 -2
  18. data/features/support/env.rb +1 -0
  19. data/features/support/paths.rb +1 -1
  20. data/features/support/rails.rb +0 -24
  21. data/gemfiles/4.2.gemfile +3 -5
  22. data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
  23. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  24. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  25. data/lib/paperclip.rb +13 -12
  26. data/lib/paperclip/attachment.rb +25 -14
  27. data/lib/paperclip/attachment_registry.rb +2 -1
  28. data/lib/paperclip/callbacks.rb +8 -6
  29. data/lib/paperclip/content_type_detector.rb +3 -2
  30. data/lib/paperclip/errors.rb +3 -1
  31. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  32. data/lib/paperclip/geometry_detector_factory.rb +2 -2
  33. data/lib/paperclip/glue.rb +1 -1
  34. data/lib/paperclip/has_attached_file.rb +7 -1
  35. data/lib/paperclip/helpers.rb +15 -11
  36. data/lib/paperclip/interpolations.rb +1 -1
  37. data/lib/paperclip/io_adapters/abstract_adapter.rb +29 -3
  38. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  39. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  40. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  41. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  42. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  43. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  44. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  45. data/lib/paperclip/io_adapters/registry.rb +6 -2
  46. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  47. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  48. data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
  49. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  50. data/lib/paperclip/media_type_spoof_detector.rb +3 -2
  51. data/lib/paperclip/processor.rb +5 -4
  52. data/lib/paperclip/schema.rb +2 -8
  53. data/lib/paperclip/storage/filesystem.rb +13 -2
  54. data/lib/paperclip/storage/fog.rb +20 -13
  55. data/lib/paperclip/storage/s3.rb +87 -58
  56. data/lib/paperclip/thumbnail.rb +18 -8
  57. data/lib/paperclip/url_generator.rb +17 -13
  58. data/lib/paperclip/validators.rb +1 -1
  59. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  60. data/lib/paperclip/version.rb +3 -1
  61. data/lib/tasks/paperclip.rake +18 -4
  62. data/paperclip.gemspec +13 -10
  63. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  64. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  65. data/spec/paperclip/attachment_spec.rb +72 -18
  66. data/spec/paperclip/content_type_detector_spec.rb +1 -1
  67. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
  68. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  69. data/spec/paperclip/integration_spec.rb +4 -3
  70. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +76 -22
  71. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  72. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  73. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  74. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +18 -1
  75. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  76. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  77. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  78. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  79. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +48 -3
  80. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  81. data/spec/paperclip/media_type_spoof_detector_spec.rb +15 -0
  82. data/spec/paperclip/paperclip_spec.rb +15 -45
  83. data/spec/paperclip/processor_spec.rb +4 -4
  84. data/spec/paperclip/storage/fog_spec.rb +31 -0
  85. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  86. data/spec/paperclip/storage/s3_spec.rb +357 -190
  87. data/spec/paperclip/tempfile_spec.rb +35 -0
  88. data/spec/paperclip/thumbnail_spec.rb +38 -35
  89. data/spec/paperclip/url_generator_spec.rb +54 -43
  90. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  91. data/spec/paperclip/validators_spec.rb +5 -5
  92. data/spec/spec_helper.rb +6 -2
  93. data/spec/support/assertions.rb +12 -1
  94. data/spec/support/conditional_filter_helper.rb +5 -0
  95. data/spec/support/mock_attachment.rb +2 -0
  96. data/spec/support/mock_url_generator_builder.rb +2 -2
  97. data/spec/support/model_reconstruction.rb +9 -1
  98. data/spec/support/reporting.rb +11 -0
  99. metadata +66 -58
  100. data/cucumber/paperclip_steps.rb +0 -6
  101. data/gemfiles/4.1.gemfile +0 -19
  102. data/lib/paperclip/deprecations.rb +0 -42
  103. data/lib/paperclip/locales/de.yml +0 -18
  104. data/lib/paperclip/locales/es.yml +0 -18
  105. data/lib/paperclip/locales/ja.yml +0 -18
  106. data/lib/paperclip/locales/pt-BR.yml +0 -18
  107. data/lib/paperclip/locales/zh-CN.yml +0 -18
  108. data/lib/paperclip/locales/zh-HK.yml +0 -18
  109. data/lib/paperclip/locales/zh-TW.yml +0 -18
  110. data/spec/paperclip/deprecations_spec.rb +0 -65
  111. data/spec/support/deprecations.rb +0 -9
  112. data/spec/support/rails_helpers.rb +0 -7
@@ -4,7 +4,7 @@ module Paperclip
4
4
  # distribution. You can find out more about it at http://aws.amazon.com/s3
5
5
  #
6
6
  # To use Paperclip with S3, include the +aws-sdk+ gem in your Gemfile:
7
- # gem 'aws-sdk', '~> 1.6'
7
+ # gem 'aws-sdk'
8
8
  # There are a few S3-specific options for has_attached_file:
9
9
  # * +s3_credentials+: Takes a path, a File, a Hash or a Proc. The path (or File) must point
10
10
  # to a YAML file containing the +access_key_id+ and +secret_access_key+ that Amazon
@@ -41,19 +41,18 @@ module Paperclip
41
41
  # * +s3_permissions+: This is a String that should be one of the "canned" access
42
42
  # policies that S3 provides (more information can be found here:
43
43
  # http://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html)
44
- # The default for Paperclip is :public_read.
44
+ # The default for Paperclip is public-read.
45
45
  #
46
46
  # You can set permission on a per style bases by doing the following:
47
47
  # :s3_permissions => {
48
- # :original => :private
48
+ # :original => "private"
49
49
  # }
50
50
  # Or globally:
51
- # :s3_permissions => :private
51
+ # :s3_permissions => "private"
52
52
  #
53
- # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
54
- # 'http', 'https', or an empty string to generate protocol-relative URLs. Defaults to 'http'
55
- # when your :s3_permissions are :public_read (the default), and 'https' when your
56
- # :s3_permissions are anything else.
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.
57
56
  # * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as
58
57
  # {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at
59
58
  # runtime. Paperclip will call that Proc with attachment as the only argument.
@@ -61,11 +60,14 @@ module Paperclip
61
60
  # * +bucket+: This is the name of the S3 bucket that will store your files. Remember
62
61
  # that the bucket must be unique across all of Amazon S3. If the bucket does not exist
63
62
  # Paperclip will attempt to create it. The bucket name will not be interpolated.
64
- # You can define the bucket as a Proc if you want to determine it's name at runtime.
63
+ # You can define the bucket as a Proc if you want to determine its name at runtime.
65
64
  # Paperclip will call that Proc with attachment as the only argument.
66
65
  # * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
67
66
  # S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
68
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
69
71
  # * +url+: There are four options for the S3 url. You can choose to have the bucket's name
70
72
  # placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
71
73
  # You can also specify a CNAME (which requires the CNAME to be specified as
@@ -92,23 +94,31 @@ module Paperclip
92
94
  # to interpolate. Keys should be unique, like filenames, and despite the fact that
93
95
  # S3 (strictly speaking) does not support directories, you can still use a / to
94
96
  # separate parts of your file name.
95
- # * +s3_host_name+: If you are using your bucket in Tokyo region etc, write host_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 v2, s3_region is required.
96
100
  # * +s3_metadata+: These key/value pairs will be stored with the
97
101
  # object. This option works by prefixing each key with
98
102
  # "x-amz-meta-" before sending it as a header on the object
99
103
  # upload request. Can be defined both globally and within a style-specific hash.
100
104
  # * +s3_storage_class+: If this option is set to
101
- # <tt>:reduced_redundancy</tt>, the object will be stored using Reduced
102
- # Redundancy Storage. RRS enables customers to reduce their
105
+ # <tt>:REDUCED_REDUNDANCY</tt>, the object will be stored using Reduced
106
+ # Redundancy Storage. RRS enables customers to reduce their
103
107
  # costs by storing non-critical, reproducible data at lower
104
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
105
111
  #
106
112
  # You can set storage class on a per style bases by doing the following:
107
113
  # :s3_storage_class => {
108
- # :thumb => :reduced_reduncancy
114
+ # :thumb => :REDUCED_REDUNDANCY
109
115
  # }
116
+ #
110
117
  # Or globally:
111
- # :s3_storage_class => :reduced_redundancy
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</tt> gem for the full list.
112
122
 
113
123
  module S3
114
124
  def self.extended base
@@ -117,39 +127,23 @@ module Paperclip
117
127
  rescue LoadError => e
118
128
  e.message << " (You may need to install the aws-sdk gem)"
119
129
  raise e
120
- end unless defined?(AWS::Core)
121
-
122
- # Overriding log formatter to make sure it return a UTF-8 string
123
- if defined?(AWS::Core::LogFormatter)
124
- AWS::Core::LogFormatter.class_eval do
125
- def summarize_hash(hash)
126
- hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
127
- end
128
- end
129
- elsif defined?(AWS::Core::ClientLogging)
130
- AWS::Core::ClientLogging.class_eval do
131
- def sanitize_hash(hash)
132
- hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
133
- end
134
- end
130
+ end
131
+ if Gem::Version.new(Aws::VERSION) >= Gem::Version.new(2) &&
132
+ Gem::Version.new(Aws::VERSION) <= Gem::Version.new("2.0.33")
133
+ raise LoadError, "paperclip does not support aws-sdk versions 2.0.0 - 2.0.33. Please upgrade aws-sdk to a newer version."
135
134
  end
136
135
 
137
136
  base.instance_eval do
138
137
  @s3_options = @options[:s3_options] || {}
139
138
  @s3_permissions = set_permissions(@options[:s3_permissions])
140
- @s3_protocol = @options[:s3_protocol] ||
141
- Proc.new do |style, attachment|
142
- permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
143
- permission = permission.call(attachment, style) if permission.respond_to?(:call)
144
- (permission == :public_read) ? 'http'.freeze : 'https'.freeze
145
- end
139
+ @s3_protocol = @options[:s3_protocol] || "".freeze
146
140
  @s3_metadata = @options[:s3_metadata] || {}
147
141
  @s3_headers = {}
148
142
  merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
149
143
 
150
144
  @s3_storage_class = set_storage_class(@options[:s3_storage_class])
151
145
 
152
- @s3_server_side_encryption = :aes256
146
+ @s3_server_side_encryption = "AES256"
153
147
  if @options[:s3_server_side_encryption].blank?
154
148
  @s3_server_side_encryption = false
155
149
  end
@@ -157,17 +151,30 @@ module Paperclip
157
151
  @s3_server_side_encryption = @options[:s3_server_side_encryption]
158
152
  end
159
153
 
160
- unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host".freeze
154
+ unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
161
155
  @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
162
156
  @options[:url] = ":s3_path_url".freeze
163
157
  end
164
158
  @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
165
159
 
166
160
  @http_proxy = @options[:http_proxy] || nil
161
+
162
+ if @options.has_key?(:use_accelerate_endpoint) &&
163
+ Gem::Version.new(Aws::VERSION) < Gem::Version.new("2.3.0")
164
+ raise LoadError, ":use_accelerate_endpoint is only available from aws-sdk version 2.3.0. Please upgrade aws-sdk to a newer version."
165
+ end
166
+
167
+ @use_accelerate_endpoint = @options[:use_accelerate_endpoint]
167
168
  end
168
169
 
169
170
  Paperclip.interpolates(:s3_alias_url) do |attachment, style|
170
- "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
171
+ protocol = attachment.s3_protocol(style, true)
172
+ host = attachment.s3_host_alias
173
+ path = attachment.path(style).
174
+ split("/")[attachment.s3_prefixes_in_alias..-1].
175
+ join("/").
176
+ sub(%r{\A/}, "".freeze)
177
+ "#{protocol}//#{host}/#{path}"
171
178
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
172
179
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
173
180
  "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
@@ -182,8 +189,11 @@ module Paperclip
182
189
 
183
190
  def expiring_url(time = 3600, style_name = default_style)
184
191
  if path(style_name)
185
- base_options = { :expires => time, :secure => use_secure_protocol?(style_name) }
186
- s3_object(style_name).url_for(:read, base_options.merge(s3_url_options)).to_s
192
+ base_options = { expires_in: time }
193
+ s3_object(style_name).presigned_url(
194
+ :get,
195
+ base_options.merge(s3_url_options),
196
+ ).to_s
187
197
  else
188
198
  url(style_name)
189
199
  end
@@ -200,12 +210,23 @@ module Paperclip
200
210
  host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
201
211
  end
202
212
 
213
+ def s3_region
214
+ region = @options[:s3_region]
215
+ region = region.call(self) if region.is_a?(Proc)
216
+
217
+ region || s3_credentials[:s3_region]
218
+ end
219
+
203
220
  def s3_host_alias
204
221
  @s3_host_alias = @options[:s3_host_alias]
205
222
  @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.respond_to?(:call)
206
223
  @s3_host_alias
207
224
  end
208
225
 
226
+ def s3_prefixes_in_alias
227
+ @s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
228
+ end
229
+
209
230
  def s3_url_options
210
231
  s3_url_options = @options[:s3_url_options] || {}
211
232
  s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
@@ -220,7 +241,7 @@ module Paperclip
220
241
 
221
242
  def s3_interface
222
243
  @s3_interface ||= begin
223
- config = { :s3_endpoint => s3_host_name }
244
+ config = { region: s3_region }
224
245
 
225
246
  if using_http_proxy?
226
247
 
@@ -234,7 +255,9 @@ module Paperclip
234
255
  config[:proxy_uri] = URI::HTTP.build(proxy_opts)
235
256
  end
236
257
 
237
- [:access_key_id, :secret_access_key, :credential_provider].each do |opt|
258
+ config[:use_accelerate_endpoint] = use_accelerate_endpoint?
259
+
260
+ [:access_key_id, :secret_access_key, :credential_provider, :credentials].each do |opt|
238
261
  config[opt] = s3_credentials[opt] if s3_credentials[opt]
239
262
  end
240
263
 
@@ -244,15 +267,23 @@ module Paperclip
244
267
 
245
268
  def obtain_s3_instance_for(options)
246
269
  instances = (Thread.current[:paperclip_s3_instances] ||= {})
247
- instances[options] ||= AWS::S3.new(options)
270
+ instances[options] ||= ::Aws::S3::Resource.new(options)
248
271
  end
249
272
 
250
273
  def s3_bucket
251
- @s3_bucket ||= s3_interface.buckets[bucket_name]
274
+ @s3_bucket ||= s3_interface.bucket(bucket_name)
275
+ end
276
+
277
+ def style_name_as_path(style_name)
278
+ path(style_name).sub(%r{\A/},'')
252
279
  end
253
280
 
254
281
  def s3_object style_name = default_style
255
- s3_bucket.objects[path(style_name).sub(%r{\A/},'')]
282
+ s3_bucket.object style_name_as_path(style_name)
283
+ end
284
+
285
+ def use_accelerate_endpoint?
286
+ !!@use_accelerate_endpoint
256
287
  end
257
288
 
258
289
  def using_http_proxy?
@@ -277,7 +308,7 @@ module Paperclip
277
308
 
278
309
  def set_permissions permissions
279
310
  permissions = { :default => permissions } unless permissions.respond_to?(:merge)
280
- permissions.merge :default => (permissions[:default] || :public_read)
311
+ permissions.merge :default => (permissions[:default] || :"public-read")
281
312
  end
282
313
 
283
314
  def set_storage_class(storage_class)
@@ -297,7 +328,7 @@ module Paperclip
297
328
  else
298
329
  false
299
330
  end
300
- rescue AWS::Errors::Base => e
331
+ rescue Aws::Errors::ServiceError => e
301
332
  false
302
333
  end
303
334
 
@@ -323,7 +354,7 @@ module Paperclip
323
354
  end
324
355
 
325
356
  def create_bucket
326
- s3_interface.buckets.create(bucket_name)
357
+ s3_interface.bucket(bucket_name).create
327
358
  end
328
359
 
329
360
  def flush_writes #:nodoc:
@@ -331,11 +362,9 @@ module Paperclip
331
362
  retries = 0
332
363
  begin
333
364
  log("saving #{path(style)}")
334
- acl = @s3_permissions[style] || @s3_permissions[:default]
335
- acl = acl.call(self, style) if acl.respond_to?(:call)
336
365
  write_options = {
337
366
  :content_type => file.content_type,
338
- :acl => acl
367
+ :acl => s3_permissions(style)
339
368
  }
340
369
 
341
370
  # add storage class for this style if defined
@@ -356,11 +385,11 @@ module Paperclip
356
385
  write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
357
386
  write_options.merge!(@s3_headers)
358
387
 
359
- s3_object(style).write(file, write_options)
360
- rescue AWS::S3::Errors::NoSuchBucket
388
+ s3_object(style).upload_file(file.path, write_options)
389
+ rescue ::Aws::S3::Errors::NoSuchBucket
361
390
  create_bucket
362
391
  retry
363
- rescue AWS::S3::Errors::SlowDown
392
+ rescue ::Aws::S3::Errors::SlowDown
364
393
  retries += 1
365
394
  if retries <= 5
366
395
  sleep((2 ** retries) * 0.5)
@@ -382,8 +411,8 @@ module Paperclip
382
411
  @queued_for_delete.each do |path|
383
412
  begin
384
413
  log("deleting #{path}")
385
- s3_bucket.objects[path.sub(%r{\A/},'')].delete
386
- rescue AWS::Errors::Base => e
414
+ s3_bucket.object(path.sub(%r{\A/}, "")).delete
415
+ rescue Aws::Errors::ServiceError => e
387
416
  # Ignore this.
388
417
  end
389
418
  end
@@ -393,11 +422,11 @@ module Paperclip
393
422
  def copy_to_local_file(style, local_dest_path)
394
423
  log("copying #{path(style)} to local file #{local_dest_path}")
395
424
  ::File.open(local_dest_path, 'wb') do |local_file|
396
- s3_object(style).read do |chunk|
425
+ s3_object(style).get do |chunk|
397
426
  local_file.write(chunk)
398
427
  end
399
428
  end
400
- rescue AWS::Errors::Base => e
429
+ rescue Aws::Errors::ServiceError => e
401
430
  warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
402
431
  false
403
432
  end
@@ -3,10 +3,11 @@ module Paperclip
3
3
  class Thumbnail < Processor
4
4
 
5
5
  attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options,
6
- :source_file_options, :animated, :auto_orient
6
+ :source_file_options, :animated, :auto_orient, :frame_index
7
7
 
8
8
  # List of formats that we need to preserve animation
9
9
  ANIMATED_FORMATS = %w(gif)
10
+ MULTI_FRAME_FORMATS = %w(.mkv .avi .mp4 .mov .mpg .mpeg .gif)
10
11
 
11
12
  # Creates a Thumbnail object set to work on the +file+ given. It
12
13
  # will attempt to transform the image into one defined by +target_geometry+
@@ -25,6 +26,7 @@ module Paperclip
25
26
  # +whiny+ - whether to raise an error when processing fails. Defaults to true
26
27
  # +format+ - the desired filename extension
27
28
  # +animated+ - whether to merge all the layers in the image. Defaults to true
29
+ # +frame_index+ - the frame index of the source file to render as the thumbnail
28
30
  def initialize(file, options = {}, attachment = nil)
29
31
  super
30
32
 
@@ -41,12 +43,12 @@ module Paperclip
41
43
  if @auto_orient && @current_geometry.respond_to?(:auto_orient)
42
44
  @current_geometry.auto_orient
43
45
  end
44
-
45
46
  @source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
46
47
  @convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
47
48
 
48
49
  @current_format = File.extname(@file.path)
49
50
  @basename = File.basename(@file.path, @current_format)
51
+ @frame_index = multi_frame_format? ? options.fetch(:frame_index, 0) : 0
50
52
  end
51
53
 
52
54
  # Returns true if the +target_geometry+ is meant to crop.
@@ -76,10 +78,15 @@ module Paperclip
76
78
 
77
79
  parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
78
80
 
79
- success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
80
- rescue Cocaine::ExitStatusError => e
81
+ frame = animated? ? "" : "[#{@frame_index}]"
82
+ convert(
83
+ parameters,
84
+ source: "#{File.expand_path(src.path)}#{frame}",
85
+ dest: File.expand_path(dst.path),
86
+ )
87
+ rescue Terrapin::ExitStatusError => e
81
88
  raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
82
- rescue Cocaine::CommandNotFoundError => e
89
+ rescue Terrapin::CommandNotFoundError => e
83
90
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
84
91
  end
85
92
 
@@ -101,7 +108,10 @@ module Paperclip
101
108
 
102
109
  protected
103
110
 
104
- # Return true if the format is animated
111
+ def multi_frame_format?
112
+ MULTI_FRAME_FORMATS.include? @current_format
113
+ end
114
+
105
115
  def animated?
106
116
  @animated && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?) && identified_as_animated?
107
117
  end
@@ -112,9 +122,9 @@ module Paperclip
112
122
  @identified_as_animated = ANIMATED_FORMATS.include? identify("-format %m :file", :file => "#{@file.path}[0]").to_s.downcase.strip
113
123
  end
114
124
  @identified_as_animated
115
- rescue Cocaine::ExitStatusError => e
125
+ rescue Terrapin::ExitStatusError => e
116
126
  raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
117
- rescue Cocaine::CommandNotFoundError => e
127
+ rescue Terrapin::CommandNotFoundError => e
118
128
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
119
129
  end
120
130
  end
@@ -1,30 +1,34 @@
1
1
  require 'uri'
2
+ require 'active_support/core_ext/module/delegation'
2
3
 
3
4
  module Paperclip
4
5
  class UrlGenerator
5
- def initialize(attachment, attachment_options)
6
+ def initialize(attachment)
6
7
  @attachment = attachment
7
- @attachment_options = attachment_options
8
8
  end
9
9
 
10
10
  def for(style_name, options)
11
- timestamp_as_needed(
12
- escape_url_as_needed(
13
- @attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
14
- options
15
- ), options)
11
+ interpolated = attachment_options[:interpolator].interpolate(
12
+ most_appropriate_url, @attachment, style_name
13
+ )
14
+
15
+ escaped = escape_url_as_needed(interpolated, options)
16
+ timestamp_as_needed(escaped, options)
16
17
  end
17
18
 
18
19
  private
19
20
 
21
+ attr_reader :attachment
22
+ delegate :options, to: :attachment, prefix: true
23
+
20
24
  # This method is all over the place.
21
25
  def default_url
22
- if @attachment_options[:default_url].respond_to?(:call)
23
- @attachment_options[:default_url].call(@attachment)
24
- elsif @attachment_options[:default_url].is_a?(Symbol)
25
- @attachment.instance.send(@attachment_options[:default_url])
26
+ if attachment_options[:default_url].respond_to?(:call)
27
+ attachment_options[:default_url].call(@attachment)
28
+ elsif attachment_options[:default_url].is_a?(Symbol)
29
+ @attachment.instance.send(attachment_options[:default_url])
26
30
  else
27
- @attachment_options[:default_url]
31
+ attachment_options[:default_url]
28
32
  end
29
33
  end
30
34
 
@@ -32,7 +36,7 @@ module Paperclip
32
36
  if @attachment.original_filename.nil?
33
37
  default_url
34
38
  else
35
- @attachment_options[:url]
39
+ attachment_options[:url]
36
40
  end
37
41
  end
38
42