kt-paperclip 4.4.0 → 5.4.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/attachment.rb +25 -14
  26. data/lib/paperclip/attachment_registry.rb +2 -1
  27. data/lib/paperclip/callbacks.rb +8 -6
  28. data/lib/paperclip/content_type_detector.rb +3 -2
  29. data/lib/paperclip/errors.rb +3 -1
  30. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  31. data/lib/paperclip/geometry_detector_factory.rb +2 -2
  32. data/lib/paperclip/glue.rb +1 -1
  33. data/lib/paperclip/has_attached_file.rb +7 -1
  34. data/lib/paperclip/helpers.rb +15 -11
  35. data/lib/paperclip/interpolations.rb +1 -1
  36. data/lib/paperclip/io_adapters/abstract_adapter.rb +29 -3
  37. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  38. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  39. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  40. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  41. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  42. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  43. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  44. data/lib/paperclip/io_adapters/registry.rb +6 -2
  45. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  46. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  47. data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
  48. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  49. data/lib/paperclip/media_type_spoof_detector.rb +3 -2
  50. data/lib/paperclip/processor.rb +5 -4
  51. data/lib/paperclip/schema.rb +4 -10
  52. data/lib/paperclip/storage/filesystem.rb +13 -2
  53. data/lib/paperclip/storage/fog.rb +19 -13
  54. data/lib/paperclip/storage/s3.rb +87 -58
  55. data/lib/paperclip/thumbnail.rb +18 -8
  56. data/lib/paperclip/url_generator.rb +18 -14
  57. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  58. data/lib/paperclip/validators.rb +1 -1
  59. data/lib/paperclip/version.rb +3 -1
  60. data/lib/paperclip.rb +13 -12
  61. data/lib/tasks/paperclip.rake +18 -4
  62. data/paperclip.gemspec +13 -11
  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 +26 -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 +53 -42
  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 +10 -2
  98. data/spec/support/reporting.rb +11 -0
  99. metadata +69 -75
  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
@@ -48,7 +48,7 @@ module Paperclip
48
48
  end unless defined?(Fog)
49
49
 
50
50
  base.instance_eval do
51
- unless @options[:url].to_s.match(/\A:fog.*url\Z/)
51
+ unless @options[:url].to_s.match(/\A:fog.*url\z/)
52
52
  @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
53
53
  @options[:url] = ':fog_public_url'
54
54
  end
@@ -58,7 +58,7 @@ module Paperclip
58
58
  end
59
59
  end
60
60
 
61
- AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\Z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\Z/
61
+ AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\z/
62
62
 
63
63
  def exists?(style = default_style)
64
64
  if original_filename
@@ -86,11 +86,14 @@ module Paperclip
86
86
  end
87
87
 
88
88
  def fog_public(style = default_style)
89
- if @options.has_key?(:fog_public)
90
- if @options[:fog_public].respond_to?(:has_key?) && @options[:fog_public].has_key?(style)
91
- @options[:fog_public][style]
89
+ if @options.key?(:fog_public)
90
+ value = @options[:fog_public]
91
+ if value.respond_to?(:key?) && value.key?(style)
92
+ value[style]
93
+ elsif value.respond_to?(:call)
94
+ value.call(self)
92
95
  else
93
- @options[:fog_public]
96
+ value
94
97
  end
95
98
  else
96
99
  true
@@ -171,6 +174,7 @@ module Paperclip
171
174
  log("copying #{path(style)} to local file #{local_dest_path}")
172
175
  ::File.open(local_dest_path, 'wb') do |local_file|
173
176
  file = directory.files.get(path(style))
177
+ return false unless file
174
178
  local_file.write(file.body)
175
179
  end
176
180
  rescue ::Fog::Errors::Error => e
@@ -181,7 +185,7 @@ module Paperclip
181
185
  private
182
186
 
183
187
  def convert_time(time)
184
- if time.is_a?(Fixnum)
188
+ if time.is_a?(Integer)
185
189
  time = Time.now + time
186
190
  end
187
191
  time
@@ -196,10 +200,10 @@ module Paperclip
196
200
  end
197
201
 
198
202
  def host_name_for_directory
199
- if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
200
- "#{@options[:fog_directory]}.s3.amazonaws.com"
203
+ if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
204
+ "#{directory_name}.s3.amazonaws.com"
201
205
  else
202
- "s3.amazonaws.com/#{@options[:fog_directory]}"
206
+ "s3.amazonaws.com/#{directory_name}"
203
207
  end
204
208
  end
205
209
 
@@ -225,13 +229,15 @@ module Paperclip
225
229
  end
226
230
 
227
231
  def directory
228
- dir = if @options[:fog_directory].respond_to?(:call)
232
+ @directory ||= connection.directories.new(key: directory_name)
233
+ end
234
+
235
+ def directory_name
236
+ if @options[:fog_directory].respond_to?(:call)
229
237
  @options[:fog_directory].call(self)
230
238
  else
231
239
  @options[:fog_directory]
232
240
  end
233
-
234
- @directory ||= connection.directories.new(:key => dir)
235
241
  end
236
242
 
237
243
  def scheme
@@ -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,4 +1,5 @@
1
1
  require 'uri'
2
+ require 'active_support/core_ext/module/delegation'
2
3
 
3
4
  module Paperclip
4
5
  class UrlGenerator
@@ -9,29 +10,32 @@ module Paperclip
9
10
  delegate :escape, :unescape, to: :encoder
10
11
  end
11
12
 
12
- def initialize(attachment, attachment_options)
13
+ def initialize(attachment)
13
14
  @attachment = attachment
14
- @attachment_options = attachment_options
15
15
  end
16
16
 
17
17
  def for(style_name, options)
18
- timestamp_as_needed(
19
- escape_url_as_needed(
20
- @attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
21
- options
22
- ), options)
18
+ interpolated = attachment_options[:interpolator].interpolate(
19
+ most_appropriate_url, @attachment, style_name
20
+ )
21
+
22
+ escaped = escape_url_as_needed(interpolated, options)
23
+ timestamp_as_needed(escaped, options)
23
24
  end
24
25
 
25
26
  private
26
27
 
28
+ attr_reader :attachment
29
+ delegate :options, to: :attachment, prefix: true
30
+
27
31
  # This method is all over the place.
28
32
  def default_url
29
- if @attachment_options[:default_url].respond_to?(:call)
30
- @attachment_options[:default_url].call(@attachment)
31
- elsif @attachment_options[:default_url].is_a?(Symbol)
32
- @attachment.instance.send(@attachment_options[:default_url])
33
+ if attachment_options[:default_url].respond_to?(:call)
34
+ attachment_options[:default_url].call(@attachment)
35
+ elsif attachment_options[:default_url].is_a?(Symbol)
36
+ @attachment.instance.send(attachment_options[:default_url])
33
37
  else
34
- @attachment_options[:default_url]
38
+ attachment_options[:default_url]
35
39
  end
36
40
  end
37
41
 
@@ -39,7 +43,7 @@ module Paperclip
39
43
  if @attachment.original_filename.nil?
40
44
  default_url
41
45
  else
42
- @attachment_options[:url]
46
+ attachment_options[:url]
43
47
  end
44
48
  end
45
49
 
@@ -68,7 +72,7 @@ module Paperclip
68
72
  if url.respond_to?(:escape)
69
73
  url.escape
70
74
  else
71
- self.class.escape(url).gsub(escape_regex) { |m| "%#{m.ord.to_s(16).upcase}" }
75
+ self.class.escape(url).gsub(escape_regex){|m| "%#{m.ord.to_s(16).upcase}" }
72
76
  end
73
77
  end
74
78
 
@@ -71,13 +71,7 @@ module Paperclip
71
71
  end
72
72
 
73
73
  def human_size(size)
74
- if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
75
- ActiveSupport::NumberHelper.number_to_human_size(size)
76
- else
77
- storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
78
- unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => size.to_i, :raise => true)
79
- storage_units_format.gsub(/%n/, size.to_i.to_s).gsub(/%u/, unit).html_safe
80
- end
74
+ ActiveSupport::NumberHelper.number_to_human_size(size)
81
75
  end
82
76
 
83
77
  def min_value_in_human_size(record)
@@ -36,7 +36,7 @@ module Paperclip
36
36
  options = attributes.extract_options!.dup
37
37
 
38
38
  Paperclip::Validators.constants.each do |constant|
39
- if constant.to_s =~ /\AAttachment(.+)Validator\Z/
39
+ if constant.to_s =~ /\AAttachment(.+)Validator\z/
40
40
  validator_kind = $1.underscore.to_sym
41
41
 
42
42
  if options.has_key?(validator_kind)
@@ -1,3 +1,5 @@
1
1
  module Paperclip
2
- VERSION = "4.4.0".freeze unless defined? Paperclip::VERSION
2
+ unless defined?(Paperclip::VERSION)
3
+ VERSION = "5.4.0".freeze
4
+ end
3
5
  end
data/lib/paperclip.rb CHANGED
@@ -56,7 +56,6 @@ require 'paperclip/has_attached_file'
56
56
  require 'paperclip/attachment_registry'
57
57
  require 'paperclip/filename_cleaner'
58
58
  require 'paperclip/rails_environment'
59
- require "paperclip/deprecations"
60
59
 
61
60
  begin
62
61
  # Use mime/types/columnar if available, for reduced memory usage
@@ -68,7 +67,7 @@ end
68
67
  require 'mimemagic'
69
68
  require 'mimemagic/overlay'
70
69
  require 'logger'
71
- require 'cocaine'
70
+ require 'terrapin'
72
71
 
73
72
  require 'paperclip/railtie' if defined?(Rails::Railtie)
74
73
 
@@ -91,14 +90,14 @@ module Paperclip
91
90
  # image's orientation. Defaults to true.
92
91
  def self.options
93
92
  @options ||= {
94
- :whiny => true,
95
- :image_magick_path => nil,
96
- :command_path => nil,
97
- :log => true,
98
- :log_command => true,
99
- :swallow_stderr => true,
100
- :content_type_mappings => {},
101
- :use_exif_orientation => true
93
+ command_path: nil,
94
+ content_type_mappings: {},
95
+ log: true,
96
+ log_command: true,
97
+ read_timeout: nil,
98
+ swallow_stderr: true,
99
+ use_exif_orientation: true,
100
+ whiny: true,
102
101
  }
103
102
  end
104
103
 
@@ -120,7 +119,7 @@ module Paperclip
120
119
  # called on it, the attachment will *not* be deleted until +save+ is called. See the
121
120
  # Paperclip::Attachment documentation for more specifics. There are a number of options
122
121
  # you can set to change the behavior of a Paperclip attachment:
123
- # * +url+: The full URL of where the attachment is publically accessible. This can just
122
+ # * +url+: The full URL of where the attachment is publicly accessible. This can just
124
123
  # as easily point to a directory served directly through Apache as it can to an action
125
124
  # that can control permissions. You can specify the full domain and path, but usually
126
125
  # just an absolute path is sufficient. The leading slash *must* be included manually for
@@ -129,6 +128,9 @@ module Paperclip
129
128
  # Paperclip::Attachment#interpolate for more information on variable interpolaton.
130
129
  # :url => "/:class/:attachment/:id/:style_:filename"
131
130
  # :url => "http://some.other.host/stuff/:class/:id_:extension"
131
+ # Note: When using the +s3+ storage option, the +url+ option expects
132
+ # particular values. See the Paperclip::Storage::S3#url documentation for
133
+ # specifics.
132
134
  # * +default_url+: The URL that will be returned if there is no attachment assigned.
133
135
  # This field is interpolated just as the url is. The default value is
134
136
  # "/:attachment/:style/missing.png"
@@ -192,7 +194,6 @@ module Paperclip
192
194
  # end
193
195
  # end
194
196
  def has_attached_file(name, options = {})
195
- Paperclip::Deprecations.check
196
197
  HasAttachedFile.define_on(self, name, options)
197
198
  end
198
199
  end