paperclip 4.3.7 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.hound.yml +5 -16
  5. data/.travis.yml +15 -12
  6. data/Appraisals +4 -8
  7. data/CONTRIBUTING.md +16 -5
  8. data/Gemfile +3 -8
  9. data/LICENSE +1 -1
  10. data/MIGRATING-ES.md +317 -0
  11. data/MIGRATING.md +375 -0
  12. data/NEWS +126 -31
  13. data/README.md +264 -156
  14. data/Rakefile +1 -1
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +3 -2
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +14 -14
  19. data/features/step_definitions/rails_steps.rb +29 -28
  20. data/features/step_definitions/s3_steps.rb +2 -2
  21. data/features/support/env.rb +1 -0
  22. data/features/support/paths.rb +1 -1
  23. data/features/support/rails.rb +0 -24
  24. data/gemfiles/4.2.gemfile +3 -5
  25. data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
  26. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  27. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  28. data/lib/paperclip.rb +14 -12
  29. data/lib/paperclip/attachment.rb +44 -20
  30. data/lib/paperclip/attachment_registry.rb +2 -1
  31. data/lib/paperclip/callbacks.rb +8 -6
  32. data/lib/paperclip/content_type_detector.rb +3 -2
  33. data/lib/paperclip/errors.rb +3 -1
  34. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  35. data/lib/paperclip/filename_cleaner.rb +0 -1
  36. data/lib/paperclip/geometry_detector_factory.rb +3 -3
  37. data/lib/paperclip/glue.rb +1 -1
  38. data/lib/paperclip/has_attached_file.rb +7 -1
  39. data/lib/paperclip/helpers.rb +15 -11
  40. data/lib/paperclip/interpolations.rb +7 -2
  41. data/lib/paperclip/io_adapters/abstract_adapter.rb +31 -4
  42. data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
  43. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  44. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  45. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  46. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  47. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  48. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  49. data/lib/paperclip/io_adapters/registry.rb +6 -2
  50. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  51. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  52. data/lib/paperclip/io_adapters/uri_adapter.rb +22 -17
  53. data/lib/paperclip/logger.rb +1 -1
  54. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  55. data/lib/paperclip/media_type_spoof_detector.rb +11 -7
  56. data/lib/paperclip/processor.rb +15 -6
  57. data/lib/paperclip/schema.rb +3 -9
  58. data/lib/paperclip/storage/filesystem.rb +13 -2
  59. data/lib/paperclip/storage/fog.rb +21 -14
  60. data/lib/paperclip/storage/s3.rb +81 -61
  61. data/lib/paperclip/style.rb +0 -1
  62. data/lib/paperclip/thumbnail.rb +22 -9
  63. data/lib/paperclip/url_generator.rb +17 -13
  64. data/lib/paperclip/validators.rb +1 -1
  65. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  66. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
  67. data/lib/paperclip/version.rb +3 -1
  68. data/lib/tasks/paperclip.rake +18 -4
  69. data/paperclip.gemspec +13 -10
  70. data/spec/paperclip/attachment_processing_spec.rb +2 -5
  71. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  72. data/spec/paperclip/attachment_spec.rb +89 -20
  73. data/spec/paperclip/content_type_detector_spec.rb +1 -1
  74. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
  75. data/spec/paperclip/filename_cleaner_spec.rb +0 -1
  76. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  77. data/spec/paperclip/integration_spec.rb +42 -5
  78. data/spec/paperclip/interpolations_spec.rb +9 -0
  79. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +104 -22
  80. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  81. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  82. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  83. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +47 -13
  84. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  85. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  86. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  87. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  88. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +104 -11
  89. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  90. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
  91. data/spec/paperclip/media_type_spoof_detector_spec.rb +41 -0
  92. data/spec/paperclip/paperclip_spec.rb +15 -45
  93. data/spec/paperclip/processor_spec.rb +4 -4
  94. data/spec/paperclip/schema_spec.rb +46 -46
  95. data/spec/paperclip/storage/fog_spec.rb +31 -0
  96. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  97. data/spec/paperclip/storage/s3_spec.rb +359 -192
  98. data/spec/paperclip/style_spec.rb +0 -1
  99. data/spec/paperclip/tempfile_spec.rb +35 -0
  100. data/spec/paperclip/thumbnail_spec.rb +43 -38
  101. data/spec/paperclip/url_generator_spec.rb +54 -44
  102. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  103. data/spec/paperclip/validators_spec.rb +5 -5
  104. data/spec/spec_helper.rb +5 -2
  105. data/spec/support/assertions.rb +12 -1
  106. data/spec/support/mock_attachment.rb +2 -0
  107. data/spec/support/mock_url_generator_builder.rb +2 -2
  108. data/spec/support/model_reconstruction.rb +11 -3
  109. data/spec/support/reporting.rb +11 -0
  110. metadata +64 -61
  111. data/cucumber/paperclip_steps.rb +0 -6
  112. data/gemfiles/4.1.gemfile +0 -19
  113. data/lib/paperclip/deprecations.rb +0 -42
  114. data/lib/paperclip/locales/de.yml +0 -18
  115. data/lib/paperclip/locales/es.yml +0 -18
  116. data/lib/paperclip/locales/ja.yml +0 -18
  117. data/lib/paperclip/locales/pt-BR.yml +0 -18
  118. data/lib/paperclip/locales/zh-CN.yml +0 -18
  119. data/lib/paperclip/locales/zh-HK.yml +0 -18
  120. data/lib/paperclip/locales/zh-TW.yml +0 -18
  121. data/spec/paperclip/deprecations_spec.rb +0 -65
  122. data/spec/support/deprecations.rb +0 -9
  123. data/spec/support/rails_helpers.rb +0 -7
@@ -5,17 +5,14 @@ module Paperclip
5
5
  module Schema
6
6
  COLUMNS = {:file_name => :string,
7
7
  :content_type => :string,
8
- :file_size => :integer,
8
+ :file_size => :bigint,
9
9
  :updated_at => :datetime}
10
10
 
11
11
  def self.included(base)
12
12
  ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
13
13
  ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
14
14
  ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
15
-
16
- if defined?(ActiveRecord::Migration::CommandRecorder) # Rails 3.1+
17
- ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
18
- end
15
+ ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
19
16
  end
20
17
 
21
18
  module Statements
@@ -35,11 +32,8 @@ module Paperclip
35
32
  def remove_attachment(table_name, *attachment_names)
36
33
  raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty?
37
34
 
38
- options = attachment_names.extract_options!
39
-
40
35
  attachment_names.each do |attachment_name|
41
- COLUMNS.each_pair do |column_name, column_type|
42
- column_options = options.merge(options[column_name.to_sym] || {})
36
+ COLUMNS.keys.each do |column_name|
43
37
  remove_column(table_name, "#{attachment_name}_#{column_name}")
44
38
  end
45
39
  end
@@ -37,7 +37,7 @@ module Paperclip
37
37
  @queued_for_write.each do |style_name, file|
38
38
  FileUtils.mkdir_p(File.dirname(path(style_name)))
39
39
  begin
40
- FileUtils.mv(file.path, path(style_name))
40
+ move_file(file.path, path(style_name))
41
41
  rescue SystemCallError
42
42
  File.open(path(style_name), "wb") do |new_file|
43
43
  while chunk = file.read(16 * 1024)
@@ -46,7 +46,7 @@ module Paperclip
46
46
  end
47
47
  end
48
48
  unless @options[:override_file_permissions] == false
49
- resolved_chmod = (@options[:override_file_permissions] &~ 0111) || (0666 &~ File.umask)
49
+ resolved_chmod = (@options[:override_file_permissions] & ~0111) || (0666 & ~File.umask)
50
50
  FileUtils.chmod( resolved_chmod, path(style_name) )
51
51
  end
52
52
  file.rewind
@@ -84,6 +84,17 @@ module Paperclip
84
84
  def copy_to_local_file(style, local_dest_path)
85
85
  FileUtils.cp(path(style), local_dest_path)
86
86
  end
87
+
88
+ private
89
+
90
+ def move_file(src, dest)
91
+ # Support hardlinked files
92
+ if File.identical?(src, dest)
93
+ File.unlink(src)
94
+ else
95
+ FileUtils.mv(src, dest)
96
+ end
97
+ end
87
98
  end
88
99
 
89
100
  end
@@ -19,7 +19,7 @@ module Paperclip
19
19
  # store your files. Remember that the bucket must be unique across
20
20
  # all of Amazon S3. If the bucket does not exist, Paperclip will
21
21
  # attempt to create it.
22
- # * +fog_file*: This can be hash or lambda returning hash. The
22
+ # * +fog_file+: This can be hash or lambda returning hash. The
23
23
  # value is used as base properties for new uploaded file.
24
24
  # * +path+: This is the key under the bucket in which the file will
25
25
  # be stored. The URL will be constructed from the bucket and the
@@ -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
@@ -114,6 +117,7 @@ module Paperclip
114
117
  raise if retried
115
118
  retried = true
116
119
  directory.save
120
+ file.rewind
117
121
  retry
118
122
  ensure
119
123
  file.rewind
@@ -170,6 +174,7 @@ module Paperclip
170
174
  log("copying #{path(style)} to local file #{local_dest_path}")
171
175
  ::File.open(local_dest_path, 'wb') do |local_file|
172
176
  file = directory.files.get(path(style))
177
+ return false unless file
173
178
  local_file.write(file.body)
174
179
  end
175
180
  rescue ::Fog::Errors::Error => e
@@ -180,7 +185,7 @@ module Paperclip
180
185
  private
181
186
 
182
187
  def convert_time(time)
183
- if time.is_a?(Fixnum)
188
+ if time.is_a?(Integer)
184
189
  time = Time.now + time
185
190
  end
186
191
  time
@@ -195,10 +200,10 @@ module Paperclip
195
200
  end
196
201
 
197
202
  def host_name_for_directory
198
- if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
199
- "#{@options[:fog_directory]}.s3.amazonaws.com"
203
+ if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
204
+ "#{directory_name}.s3.amazonaws.com"
200
205
  else
201
- "s3.amazonaws.com/#{@options[:fog_directory]}"
206
+ "s3.amazonaws.com/#{directory_name}"
202
207
  end
203
208
  end
204
209
 
@@ -224,13 +229,15 @@ module Paperclip
224
229
  end
225
230
 
226
231
  def directory
227
- 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)
228
237
  @options[:fog_directory].call(self)
229
238
  else
230
239
  @options[:fog_directory]
231
240
  end
232
-
233
- @directory ||= connection.directories.new(:key => dir)
234
241
  end
235
242
 
236
243
  def scheme
@@ -3,8 +3,8 @@ module Paperclip
3
3
  # Amazon's S3 file hosting service is a scalable, easy place to store files for
4
4
  # distribution. You can find out more about it at http://aws.amazon.com/s3
5
5
  #
6
- # To use Paperclip with S3, include the +aws-sdk+ gem in your Gemfile:
7
- # gem 'aws-sdk', '~> 1.6'
6
+ # To use Paperclip with S3, include the +aws-sdk-s3+ gem in your Gemfile:
7
+ # gem 'aws-sdk-s3'
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,64 +94,52 @@ 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-s3, 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-s3</tt> gem for the full list.
112
122
 
113
123
  module S3
114
124
  def self.extended base
115
125
  begin
116
- require 'aws-sdk'
126
+ require "aws-sdk-s3"
117
127
  rescue LoadError => e
118
- e.message << " (You may need to install the aws-sdk gem)"
128
+ e.message << " (You may need to install the aws-sdk-s3 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
135
130
  end
136
131
 
137
132
  base.instance_eval do
138
133
  @s3_options = @options[:s3_options] || {}
139
134
  @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
135
+ @s3_protocol = @options[:s3_protocol] || "".freeze
146
136
  @s3_metadata = @options[:s3_metadata] || {}
147
137
  @s3_headers = {}
148
138
  merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
149
139
 
150
140
  @s3_storage_class = set_storage_class(@options[:s3_storage_class])
151
141
 
152
- @s3_server_side_encryption = :aes256
142
+ @s3_server_side_encryption = "AES256"
153
143
  if @options[:s3_server_side_encryption].blank?
154
144
  @s3_server_side_encryption = false
155
145
  end
@@ -157,17 +147,25 @@ module Paperclip
157
147
  @s3_server_side_encryption = @options[:s3_server_side_encryption]
158
148
  end
159
149
 
160
- unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host".freeze
150
+ unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
161
151
  @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
162
152
  @options[:url] = ":s3_path_url".freeze
163
153
  end
164
154
  @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
165
155
 
166
156
  @http_proxy = @options[:http_proxy] || nil
157
+
158
+ @use_accelerate_endpoint = @options[:use_accelerate_endpoint]
167
159
  end
168
160
 
169
161
  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)}"
162
+ protocol = attachment.s3_protocol(style, true)
163
+ host = attachment.s3_host_alias
164
+ path = attachment.path(style).
165
+ split("/")[attachment.s3_prefixes_in_alias..-1].
166
+ join("/").
167
+ sub(%r{\A/}, "".freeze)
168
+ "#{protocol}//#{host}/#{path}"
171
169
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
172
170
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
173
171
  "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
@@ -182,8 +180,11 @@ module Paperclip
182
180
 
183
181
  def expiring_url(time = 3600, style_name = default_style)
184
182
  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
183
+ base_options = { expires_in: time }
184
+ s3_object(style_name).presigned_url(
185
+ :get,
186
+ base_options.merge(s3_url_options),
187
+ ).to_s
187
188
  else
188
189
  url(style_name)
189
190
  end
@@ -200,12 +201,23 @@ module Paperclip
200
201
  host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
201
202
  end
202
203
 
204
+ def s3_region
205
+ region = @options[:s3_region]
206
+ region = region.call(self) if region.is_a?(Proc)
207
+
208
+ region || s3_credentials[:s3_region]
209
+ end
210
+
203
211
  def s3_host_alias
204
212
  @s3_host_alias = @options[:s3_host_alias]
205
213
  @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.respond_to?(:call)
206
214
  @s3_host_alias
207
215
  end
208
216
 
217
+ def s3_prefixes_in_alias
218
+ @s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
219
+ end
220
+
209
221
  def s3_url_options
210
222
  s3_url_options = @options[:s3_url_options] || {}
211
223
  s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
@@ -220,7 +232,7 @@ module Paperclip
220
232
 
221
233
  def s3_interface
222
234
  @s3_interface ||= begin
223
- config = { :s3_endpoint => s3_host_name }
235
+ config = { region: s3_region }
224
236
 
225
237
  if using_http_proxy?
226
238
 
@@ -234,7 +246,9 @@ module Paperclip
234
246
  config[:proxy_uri] = URI::HTTP.build(proxy_opts)
235
247
  end
236
248
 
237
- [:access_key_id, :secret_access_key, :credential_provider].each do |opt|
249
+ config[:use_accelerate_endpoint] = use_accelerate_endpoint?
250
+
251
+ [:access_key_id, :secret_access_key, :credential_provider, :credentials].each do |opt|
238
252
  config[opt] = s3_credentials[opt] if s3_credentials[opt]
239
253
  end
240
254
 
@@ -244,15 +258,23 @@ module Paperclip
244
258
 
245
259
  def obtain_s3_instance_for(options)
246
260
  instances = (Thread.current[:paperclip_s3_instances] ||= {})
247
- instances[options] ||= AWS::S3.new(options)
261
+ instances[options] ||= ::Aws::S3::Resource.new(options)
248
262
  end
249
263
 
250
264
  def s3_bucket
251
- @s3_bucket ||= s3_interface.buckets[bucket_name]
265
+ @s3_bucket ||= s3_interface.bucket(bucket_name)
266
+ end
267
+
268
+ def style_name_as_path(style_name)
269
+ path(style_name).sub(%r{\A/},'')
252
270
  end
253
271
 
254
272
  def s3_object style_name = default_style
255
- s3_bucket.objects[path(style_name).sub(%r{\A/},'')]
273
+ s3_bucket.object style_name_as_path(style_name)
274
+ end
275
+
276
+ def use_accelerate_endpoint?
277
+ !!@use_accelerate_endpoint
256
278
  end
257
279
 
258
280
  def using_http_proxy?
@@ -277,7 +299,7 @@ module Paperclip
277
299
 
278
300
  def set_permissions permissions
279
301
  permissions = { :default => permissions } unless permissions.respond_to?(:merge)
280
- permissions.merge :default => (permissions[:default] || :public_read)
302
+ permissions.merge :default => (permissions[:default] || :"public-read")
281
303
  end
282
304
 
283
305
  def set_storage_class(storage_class)
@@ -297,7 +319,7 @@ module Paperclip
297
319
  else
298
320
  false
299
321
  end
300
- rescue AWS::Errors::Base => e
322
+ rescue Aws::Errors::ServiceError => e
301
323
  false
302
324
  end
303
325
 
@@ -323,7 +345,7 @@ module Paperclip
323
345
  end
324
346
 
325
347
  def create_bucket
326
- s3_interface.buckets.create(bucket_name)
348
+ s3_interface.bucket(bucket_name).create
327
349
  end
328
350
 
329
351
  def flush_writes #:nodoc:
@@ -331,11 +353,9 @@ module Paperclip
331
353
  retries = 0
332
354
  begin
333
355
  log("saving #{path(style)}")
334
- acl = @s3_permissions[style] || @s3_permissions[:default]
335
- acl = acl.call(self, style) if acl.respond_to?(:call)
336
356
  write_options = {
337
357
  :content_type => file.content_type,
338
- :acl => acl
358
+ :acl => s3_permissions(style)
339
359
  }
340
360
 
341
361
  # add storage class for this style if defined
@@ -356,11 +376,11 @@ module Paperclip
356
376
  write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
357
377
  write_options.merge!(@s3_headers)
358
378
 
359
- s3_object(style).write(file, write_options)
360
- rescue AWS::S3::Errors::NoSuchBucket
379
+ s3_object(style).upload_file(file.path, write_options)
380
+ rescue ::Aws::S3::Errors::NoSuchBucket
361
381
  create_bucket
362
382
  retry
363
- rescue AWS::S3::Errors::SlowDown
383
+ rescue ::Aws::S3::Errors::SlowDown
364
384
  retries += 1
365
385
  if retries <= 5
366
386
  sleep((2 ** retries) * 0.5)
@@ -382,8 +402,8 @@ module Paperclip
382
402
  @queued_for_delete.each do |path|
383
403
  begin
384
404
  log("deleting #{path}")
385
- s3_bucket.objects[path.sub(%r{\A/},'')].delete
386
- rescue AWS::Errors::Base => e
405
+ s3_bucket.object(path.sub(%r{\A/}, "")).delete
406
+ rescue Aws::Errors::ServiceError => e
387
407
  # Ignore this.
388
408
  end
389
409
  end
@@ -393,11 +413,11 @@ module Paperclip
393
413
  def copy_to_local_file(style, local_dest_path)
394
414
  log("copying #{path(style)} to local file #{local_dest_path}")
395
415
  ::File.open(local_dest_path, 'wb') do |local_file|
396
- s3_object(style).read do |chunk|
416
+ s3_object(style).get do |chunk|
397
417
  local_file.write(chunk)
398
418
  end
399
419
  end
400
- rescue AWS::Errors::Base => e
420
+ rescue Aws::Errors::ServiceError => e
401
421
  warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
402
422
  false
403
423
  end