paperclip 4.3.7 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) 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 +86 -31
  10. data/README.md +220 -146
  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/paperclip.rb +12 -11
  24. data/lib/paperclip/attachment.rb +25 -14
  25. data/lib/paperclip/attachment_registry.rb +2 -1
  26. data/lib/paperclip/callbacks.rb +8 -6
  27. data/lib/paperclip/content_type_detector.rb +3 -2
  28. data/lib/paperclip/errors.rb +3 -1
  29. data/lib/paperclip/glue.rb +1 -1
  30. data/lib/paperclip/has_attached_file.rb +7 -1
  31. data/lib/paperclip/helpers.rb +14 -10
  32. data/lib/paperclip/interpolations.rb +1 -1
  33. data/lib/paperclip/io_adapters/abstract_adapter.rb +25 -3
  34. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  35. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  36. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  37. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  38. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  39. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  40. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  41. data/lib/paperclip/io_adapters/registry.rb +6 -2
  42. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  43. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  44. data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
  45. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  46. data/lib/paperclip/processor.rb +5 -4
  47. data/lib/paperclip/schema.rb +2 -8
  48. data/lib/paperclip/storage/filesystem.rb +13 -2
  49. data/lib/paperclip/storage/fog.rb +18 -12
  50. data/lib/paperclip/storage/s3.rb +85 -57
  51. data/lib/paperclip/thumbnail.rb +14 -4
  52. data/lib/paperclip/url_generator.rb +16 -13
  53. data/lib/paperclip/validators.rb +1 -1
  54. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  55. data/lib/paperclip/version.rb +3 -1
  56. data/lib/tasks/paperclip.rake +18 -4
  57. data/paperclip.gemspec +12 -9
  58. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  59. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  60. data/spec/paperclip/attachment_spec.rb +72 -18
  61. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  62. data/spec/paperclip/integration_spec.rb +4 -3
  63. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +45 -22
  64. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  65. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  66. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  67. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +18 -1
  68. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  69. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  70. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  71. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  72. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +48 -3
  73. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  74. data/spec/paperclip/paperclip_spec.rb +3 -33
  75. data/spec/paperclip/storage/fog_spec.rb +26 -0
  76. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  77. data/spec/paperclip/storage/s3_spec.rb +357 -190
  78. data/spec/paperclip/tempfile_spec.rb +35 -0
  79. data/spec/paperclip/thumbnail_spec.rb +35 -32
  80. data/spec/paperclip/url_generator_spec.rb +54 -43
  81. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  82. data/spec/paperclip/validators_spec.rb +5 -5
  83. data/spec/spec_helper.rb +6 -2
  84. data/spec/support/assertions.rb +12 -1
  85. data/spec/support/conditional_filter_helper.rb +5 -0
  86. data/spec/support/mock_attachment.rb +2 -0
  87. data/spec/support/mock_url_generator_builder.rb +2 -2
  88. data/spec/support/model_reconstruction.rb +9 -1
  89. data/spec/support/reporting.rb +11 -0
  90. metadata +61 -158
  91. data/cucumber/paperclip_steps.rb +0 -6
  92. data/gemfiles/4.1.gemfile +0 -19
  93. data/lib/paperclip/deprecations.rb +0 -42
  94. data/lib/paperclip/locales/de.yml +0 -18
  95. data/lib/paperclip/locales/es.yml +0 -18
  96. data/lib/paperclip/locales/ja.yml +0 -18
  97. data/lib/paperclip/locales/pt-BR.yml +0 -18
  98. data/lib/paperclip/locales/zh-CN.yml +0 -18
  99. data/lib/paperclip/locales/zh-HK.yml +0 -18
  100. data/lib/paperclip/locales/zh-TW.yml +0 -18
  101. data/spec/paperclip/deprecations_spec.rb +0 -65
  102. data/spec/support/deprecations.rb +0 -9
  103. data/spec/support/rails_helpers.rb +0 -7
@@ -1,11 +1,17 @@
1
- require 'open-uri'
1
+ require "open-uri"
2
2
 
3
3
  module Paperclip
4
4
  class UriAdapter < AbstractAdapter
5
5
  attr_writer :content_type
6
6
 
7
- def initialize(target)
8
- @target = target
7
+ def self.register
8
+ Paperclip.io_adapters.register self do |target|
9
+ target.is_a?(URI)
10
+ end
11
+ end
12
+
13
+ def initialize(target, options = {})
14
+ super
9
15
  @content = download_content
10
16
  cache_current_values
11
17
  @tempfile = copy_to_tempfile(@content)
@@ -17,8 +23,7 @@ module Paperclip
17
23
  self.content_type = content_type_from_content || "text/html"
18
24
 
19
25
  self.original_filename = filename_from_content_disposition ||
20
- filename_from_path ||
21
- default_filename
26
+ filename_from_path || default_filename
22
27
  @size = @content.size
23
28
  end
24
29
 
@@ -29,9 +34,9 @@ module Paperclip
29
34
  end
30
35
 
31
36
  def filename_from_content_disposition
32
- if @content.meta.has_key?("content-disposition")
33
- @content.meta["content-disposition"].
34
- match(/filename="([^"]*)"/)[1]
37
+ if @content.meta.key?("content-disposition")
38
+ matches = @content.meta["content-disposition"].match(/filename="([^"]*)"/)
39
+ matches[1] if matches
35
40
  end
36
41
  end
37
42
 
@@ -44,11 +49,13 @@ module Paperclip
44
49
  end
45
50
 
46
51
  def download_content
47
- open(@target)
52
+ options = { read_timeout: Paperclip.options[:read_timeout] }.compact
53
+
54
+ open(@target, **options)
48
55
  end
49
56
 
50
57
  def copy_to_tempfile(src)
51
- while data = src.read(16*1024)
58
+ while data = src.read(16 * 1024)
52
59
  destination.write(data)
53
60
  end
54
61
  src.close
@@ -57,7 +64,3 @@ module Paperclip
57
64
  end
58
65
  end
59
66
  end
60
-
61
- Paperclip.io_adapters.register Paperclip::UriAdapter do |target|
62
- target.kind_of?(URI)
63
- end
@@ -40,9 +40,9 @@ module Paperclip
40
40
 
41
41
  def failure_message
42
42
  "#{expected_attachment}\n".tap do |message|
43
- message << accepted_types_and_failures
43
+ message << accepted_types_and_failures.to_s
44
44
  message << "\n\n" if @allowed_types.present? && @rejected_types.present?
45
- message << rejected_types_and_failures
45
+ message << rejected_types_and_failures.to_s
46
46
  end
47
47
  end
48
48
 
@@ -55,7 +55,7 @@ module Paperclip
55
55
  def accepted_types_and_failures
56
56
  if @allowed_types.present?
57
57
  "Accept content types: #{@allowed_types.join(", ")}\n".tap do |message|
58
- if @missing_allowed_types.any?
58
+ if @missing_allowed_types.present?
59
59
  message << " #{@missing_allowed_types.join(", ")} were rejected."
60
60
  else
61
61
  message << " All were accepted successfully."
@@ -66,7 +66,7 @@ module Paperclip
66
66
  def rejected_types_and_failures
67
67
  if @rejected_types.present?
68
68
  "Reject content types: #{@rejected_types.join(", ")}\n".tap do |message|
69
- if @missing_rejected_types.any?
69
+ if @missing_rejected_types.present?
70
70
  message << " #{@missing_rejected_types.join(", ")} were accepted."
71
71
  else
72
72
  message << " All were rejected successfully."
@@ -7,13 +7,14 @@ module Paperclip
7
7
  # Processors are required to be defined inside the Paperclip module and
8
8
  # are also required to be a subclass of Paperclip::Processor. There is
9
9
  # only one method you *must* implement to properly be a subclass:
10
- # #make, but #initialize may also be of use. Both methods accept 3
10
+ # #make, but #initialize may also be of use. #initialize accepts 3
11
11
  # arguments: the file that will be operated on (which is an instance of
12
12
  # File), a hash of options that were defined in has_attached_file's
13
- # style hash, and the Paperclip::Attachment itself.
13
+ # style hash, and the Paperclip::Attachment itself. These are set as
14
+ # instance variables that can be used within `#make`.
14
15
  #
15
- # All #make needs to return is an instance of File (Tempfile is
16
- # acceptable) which contains the results of the processing.
16
+ # #make must return an instance of File (Tempfile is acceptable) which
17
+ # contains the results of the processing.
17
18
  #
18
19
  # See Paperclip.run for more information about using command-line
19
20
  # utilities from within Processors.
@@ -12,10 +12,7 @@ module Paperclip
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
@@ -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
@@ -170,6 +173,7 @@ module Paperclip
170
173
  log("copying #{path(style)} to local file #{local_dest_path}")
171
174
  ::File.open(local_dest_path, 'wb') do |local_file|
172
175
  file = directory.files.get(path(style))
176
+ return false unless file
173
177
  local_file.write(file.body)
174
178
  end
175
179
  rescue ::Fog::Errors::Error => e
@@ -195,10 +199,10 @@ module Paperclip
195
199
  end
196
200
 
197
201
  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"
202
+ if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
203
+ "#{directory_name}.s3.amazonaws.com"
200
204
  else
201
- "s3.amazonaws.com/#{@options[:fog_directory]}"
205
+ "s3.amazonaws.com/#{directory_name}"
202
206
  end
203
207
  end
204
208
 
@@ -224,13 +228,15 @@ module Paperclip
224
228
  end
225
229
 
226
230
  def directory
227
- dir = if @options[:fog_directory].respond_to?(:call)
231
+ @directory ||= connection.directories.new(key: directory_name)
232
+ end
233
+
234
+ def directory_name
235
+ if @options[:fog_directory].respond_to?(:call)
228
236
  @options[:fog_directory].call(self)
229
237
  else
230
238
  @options[:fog_directory]
231
239
  end
232
-
233
- @directory ||= connection.directories.new(:key => dir)
234
240
  end
235
241
 
236
242
  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
@@ -93,22 +95,29 @@ module Paperclip
93
95
  # S3 (strictly speaking) does not support directories, you can still use a / to
94
96
  # separate parts of your file name.
95
97
  # * +s3_host_name+: If you are using your bucket in Tokyo region etc, write host_name.
98
+ # * +s3_region+: For aws-sdk v2, s3_region is required.
96
99
  # * +s3_metadata+: These key/value pairs will be stored with the
97
100
  # object. This option works by prefixing each key with
98
101
  # "x-amz-meta-" before sending it as a header on the object
99
102
  # upload request. Can be defined both globally and within a style-specific hash.
100
103
  # * +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
104
+ # <tt>:REDUCED_REDUNDANCY</tt>, the object will be stored using Reduced
105
+ # Redundancy Storage. RRS enables customers to reduce their
103
106
  # costs by storing non-critical, reproducible data at lower
104
107
  # levels of redundancy than Amazon S3's standard storage.
108
+ # * +use_accelerate_endpoint+: Use accelerate endpoint
109
+ # http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html
105
110
  #
106
111
  # You can set storage class on a per style bases by doing the following:
107
112
  # :s3_storage_class => {
108
- # :thumb => :reduced_reduncancy
113
+ # :thumb => :REDUCED_REDUNDANCY
109
114
  # }
115
+ #
110
116
  # Or globally:
111
- # :s3_storage_class => :reduced_redundancy
117
+ # :s3_storage_class => :REDUCED_REDUNDANCY
118
+ #
119
+ # Other storage classes, such as <tt>:STANDARD_IA</tt>, are also available—see the
120
+ # documentation for the <tt>aws-sdk</tt> gem for the full list.
112
121
 
113
122
  module S3
114
123
  def self.extended base
@@ -117,39 +126,23 @@ module Paperclip
117
126
  rescue LoadError => e
118
127
  e.message << " (You may need to install the aws-sdk gem)"
119
128
  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
129
+ end
130
+ if Gem::Version.new(Aws::VERSION) >= Gem::Version.new(2) &&
131
+ Gem::Version.new(Aws::VERSION) <= Gem::Version.new("2.0.33")
132
+ raise LoadError, "paperclip does not support aws-sdk versions 2.0.0 - 2.0.33. Please upgrade aws-sdk to a newer version."
135
133
  end
136
134
 
137
135
  base.instance_eval do
138
136
  @s3_options = @options[:s3_options] || {}
139
137
  @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
138
+ @s3_protocol = @options[:s3_protocol] || "".freeze
146
139
  @s3_metadata = @options[:s3_metadata] || {}
147
140
  @s3_headers = {}
148
141
  merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
149
142
 
150
143
  @s3_storage_class = set_storage_class(@options[:s3_storage_class])
151
144
 
152
- @s3_server_side_encryption = :aes256
145
+ @s3_server_side_encryption = "AES256"
153
146
  if @options[:s3_server_side_encryption].blank?
154
147
  @s3_server_side_encryption = false
155
148
  end
@@ -157,17 +150,30 @@ module Paperclip
157
150
  @s3_server_side_encryption = @options[:s3_server_side_encryption]
158
151
  end
159
152
 
160
- unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host".freeze
153
+ unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
161
154
  @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
162
155
  @options[:url] = ":s3_path_url".freeze
163
156
  end
164
157
  @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
165
158
 
166
159
  @http_proxy = @options[:http_proxy] || nil
160
+
161
+ if @options.has_key?(:use_accelerate_endpoint) &&
162
+ Gem::Version.new(Aws::VERSION) < Gem::Version.new("2.3.0")
163
+ raise LoadError, ":use_accelerate_endpoint is only available from aws-sdk version 2.3.0. Please upgrade aws-sdk to a newer version."
164
+ end
165
+
166
+ @use_accelerate_endpoint = @options[:use_accelerate_endpoint]
167
167
  end
168
168
 
169
169
  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)}"
170
+ protocol = attachment.s3_protocol(style, true)
171
+ host = attachment.s3_host_alias
172
+ path = attachment.path(style).
173
+ split("/")[attachment.s3_prefixes_in_alias..-1].
174
+ join("/").
175
+ sub(%r{\A/}, "".freeze)
176
+ "#{protocol}//#{host}/#{path}"
171
177
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
172
178
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
173
179
  "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
@@ -182,8 +188,11 @@ module Paperclip
182
188
 
183
189
  def expiring_url(time = 3600, style_name = default_style)
184
190
  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
191
+ base_options = { expires_in: time }
192
+ s3_object(style_name).presigned_url(
193
+ :get,
194
+ base_options.merge(s3_url_options),
195
+ ).to_s
187
196
  else
188
197
  url(style_name)
189
198
  end
@@ -200,12 +209,23 @@ module Paperclip
200
209
  host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
201
210
  end
202
211
 
212
+ def s3_region
213
+ region = @options[:s3_region]
214
+ region = region.call(self) if region.is_a?(Proc)
215
+
216
+ region || s3_credentials[:s3_region]
217
+ end
218
+
203
219
  def s3_host_alias
204
220
  @s3_host_alias = @options[:s3_host_alias]
205
221
  @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.respond_to?(:call)
206
222
  @s3_host_alias
207
223
  end
208
224
 
225
+ def s3_prefixes_in_alias
226
+ @s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
227
+ end
228
+
209
229
  def s3_url_options
210
230
  s3_url_options = @options[:s3_url_options] || {}
211
231
  s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
@@ -220,7 +240,7 @@ module Paperclip
220
240
 
221
241
  def s3_interface
222
242
  @s3_interface ||= begin
223
- config = { :s3_endpoint => s3_host_name }
243
+ config = { region: s3_region }
224
244
 
225
245
  if using_http_proxy?
226
246
 
@@ -234,7 +254,9 @@ module Paperclip
234
254
  config[:proxy_uri] = URI::HTTP.build(proxy_opts)
235
255
  end
236
256
 
237
- [:access_key_id, :secret_access_key, :credential_provider].each do |opt|
257
+ config[:use_accelerate_endpoint] = use_accelerate_endpoint?
258
+
259
+ [:access_key_id, :secret_access_key, :credential_provider, :credentials].each do |opt|
238
260
  config[opt] = s3_credentials[opt] if s3_credentials[opt]
239
261
  end
240
262
 
@@ -244,15 +266,23 @@ module Paperclip
244
266
 
245
267
  def obtain_s3_instance_for(options)
246
268
  instances = (Thread.current[:paperclip_s3_instances] ||= {})
247
- instances[options] ||= AWS::S3.new(options)
269
+ instances[options] ||= ::Aws::S3::Resource.new(options)
248
270
  end
249
271
 
250
272
  def s3_bucket
251
- @s3_bucket ||= s3_interface.buckets[bucket_name]
273
+ @s3_bucket ||= s3_interface.bucket(bucket_name)
274
+ end
275
+
276
+ def style_name_as_path(style_name)
277
+ path(style_name).sub(%r{\A/},'')
252
278
  end
253
279
 
254
280
  def s3_object style_name = default_style
255
- s3_bucket.objects[path(style_name).sub(%r{\A/},'')]
281
+ s3_bucket.object style_name_as_path(style_name)
282
+ end
283
+
284
+ def use_accelerate_endpoint?
285
+ !!@use_accelerate_endpoint
256
286
  end
257
287
 
258
288
  def using_http_proxy?
@@ -277,7 +307,7 @@ module Paperclip
277
307
 
278
308
  def set_permissions permissions
279
309
  permissions = { :default => permissions } unless permissions.respond_to?(:merge)
280
- permissions.merge :default => (permissions[:default] || :public_read)
310
+ permissions.merge :default => (permissions[:default] || :"public-read")
281
311
  end
282
312
 
283
313
  def set_storage_class(storage_class)
@@ -297,7 +327,7 @@ module Paperclip
297
327
  else
298
328
  false
299
329
  end
300
- rescue AWS::Errors::Base => e
330
+ rescue Aws::Errors::ServiceError => e
301
331
  false
302
332
  end
303
333
 
@@ -323,7 +353,7 @@ module Paperclip
323
353
  end
324
354
 
325
355
  def create_bucket
326
- s3_interface.buckets.create(bucket_name)
356
+ s3_interface.bucket(bucket_name).create
327
357
  end
328
358
 
329
359
  def flush_writes #:nodoc:
@@ -331,11 +361,9 @@ module Paperclip
331
361
  retries = 0
332
362
  begin
333
363
  log("saving #{path(style)}")
334
- acl = @s3_permissions[style] || @s3_permissions[:default]
335
- acl = acl.call(self, style) if acl.respond_to?(:call)
336
364
  write_options = {
337
365
  :content_type => file.content_type,
338
- :acl => acl
366
+ :acl => s3_permissions(style)
339
367
  }
340
368
 
341
369
  # add storage class for this style if defined
@@ -356,11 +384,11 @@ module Paperclip
356
384
  write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
357
385
  write_options.merge!(@s3_headers)
358
386
 
359
- s3_object(style).write(file, write_options)
360
- rescue AWS::S3::Errors::NoSuchBucket
387
+ s3_object(style).upload_file(file.path, write_options)
388
+ rescue ::Aws::S3::Errors::NoSuchBucket
361
389
  create_bucket
362
390
  retry
363
- rescue AWS::S3::Errors::SlowDown
391
+ rescue ::Aws::S3::Errors::SlowDown
364
392
  retries += 1
365
393
  if retries <= 5
366
394
  sleep((2 ** retries) * 0.5)
@@ -382,8 +410,8 @@ module Paperclip
382
410
  @queued_for_delete.each do |path|
383
411
  begin
384
412
  log("deleting #{path}")
385
- s3_bucket.objects[path.sub(%r{\A/},'')].delete
386
- rescue AWS::Errors::Base => e
413
+ s3_bucket.object(path.sub(%r{\A/}, "")).delete
414
+ rescue Aws::Errors::ServiceError => e
387
415
  # Ignore this.
388
416
  end
389
417
  end
@@ -393,11 +421,11 @@ module Paperclip
393
421
  def copy_to_local_file(style, local_dest_path)
394
422
  log("copying #{path(style)} to local file #{local_dest_path}")
395
423
  ::File.open(local_dest_path, 'wb') do |local_file|
396
- s3_object(style).read do |chunk|
424
+ s3_object(style).get do |chunk|
397
425
  local_file.write(chunk)
398
426
  end
399
427
  end
400
- rescue AWS::Errors::Base => e
428
+ rescue Aws::Errors::ServiceError => e
401
429
  warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
402
430
  false
403
431
  end