paperclip 4.2.2 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +17 -15
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +19 -8
- data/Gemfile +5 -9
- data/LICENSE +1 -1
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +184 -31
- data/README.md +371 -201
- data/RELEASING.md +17 -0
- data/Rakefile +2 -2
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +10 -6
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +41 -35
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +39 -38
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +1 -0
- data/features/support/file_helpers.rb +2 -2
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +0 -24
- data/gemfiles/4.2.gemfile +6 -8
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -1
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
- data/lib/paperclip/attachment.rb +51 -26
- data/lib/paperclip/attachment_registry.rb +3 -2
- data/lib/paperclip/callbacks.rb +8 -6
- data/lib/paperclip/content_type_detector.rb +27 -11
- data/lib/paperclip/errors.rb +3 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +0 -1
- data/lib/paperclip/geometry_detector_factory.rb +3 -3
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +9 -2
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +24 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/media_type_spoof_detector.rb +13 -9
- data/lib/paperclip/processor.rb +15 -6
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +4 -10
- data/lib/paperclip/storage/filesystem.rb +13 -2
- data/lib/paperclip/storage/fog.rb +33 -20
- data/lib/paperclip/storage/s3.rb +89 -70
- data/lib/paperclip/style.rb +0 -1
- data/lib/paperclip/thumbnail.rb +24 -12
- data/lib/paperclip/url_generator.rb +17 -13
- data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
- data/lib/paperclip/validators.rb +1 -1
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +27 -13
- data/lib/tasks/paperclip.rake +33 -3
- data/paperclip.gemspec +18 -15
- data/spec/paperclip/attachment_definitions_spec.rb +1 -1
- data/spec/paperclip/attachment_processing_spec.rb +2 -5
- data/spec/paperclip/attachment_registry_spec.rb +84 -13
- data/spec/paperclip/attachment_spec.rb +147 -41
- data/spec/paperclip/content_type_detector_spec.rb +9 -2
- data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -2
- data/spec/paperclip/filename_cleaner_spec.rb +0 -1
- data/spec/paperclip/geometry_spec.rb +1 -1
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +24 -8
- data/spec/paperclip/integration_spec.rb +42 -5
- data/spec/paperclip/interpolations_spec.rb +21 -9
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +106 -23
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +6 -3
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +51 -14
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +5 -1
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +126 -8
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
- data/spec/paperclip/media_type_spoof_detector_spec.rb +75 -11
- data/spec/paperclip/paperclip_spec.rb +15 -40
- data/spec/paperclip/plural_cache_spec.rb +17 -16
- data/spec/paperclip/processor_spec.rb +4 -4
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/spec/paperclip/schema_spec.rb +46 -46
- data/spec/paperclip/storage/fog_spec.rb +63 -3
- data/spec/paperclip/storage/s3_live_spec.rb +20 -14
- data/spec/paperclip/storage/s3_spec.rb +400 -215
- data/spec/paperclip/style_spec.rb +0 -1
- data/spec/paperclip/tempfile_factory_spec.rb +4 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/spec/paperclip/thumbnail_spec.rb +59 -38
- data/spec/paperclip/url_generator_spec.rb +55 -45
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
- data/spec/paperclip/validators_spec.rb +5 -5
- data/spec/spec_helper.rb +7 -1
- data/spec/support/assertions.rb +12 -1
- data/spec/support/fake_model.rb +4 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/matchers/have_column.rb +11 -2
- data/spec/support/mock_attachment.rb +2 -0
- data/spec/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +11 -3
- data/spec/support/reporting.rb +11 -0
- metadata +110 -63
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.2.gemfile +0 -19
- data/gemfiles/4.0.gemfile +0 -19
- data/gemfiles/4.1.gemfile +0 -19
- data/lib/paperclip/locales/de.yml +0 -18
- data/lib/paperclip/locales/es.yml +0 -18
- data/lib/paperclip/locales/ja.yml +0 -18
- data/lib/paperclip/locales/pt-BR.yml +0 -18
- data/lib/paperclip/locales/zh-CN.yml +0 -18
- data/lib/paperclip/locales/zh-HK.yml +0 -18
- data/lib/paperclip/locales/zh-TW.yml +0 -18
- data/spec/support/mock_model.rb +0 -2
- data/spec/support/rails_helpers.rb +0 -7
data/lib/paperclip/processor.rb
CHANGED
@@ -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.
|
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
|
-
#
|
16
|
-
#
|
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.
|
@@ -36,13 +37,21 @@ module Paperclip
|
|
36
37
|
# The convert method runs the convert binary with the provided arguments.
|
37
38
|
# See Paperclip.run for the available options.
|
38
39
|
def convert(arguments = "", local_options = {})
|
39
|
-
Paperclip.run(
|
40
|
+
Paperclip.run(
|
41
|
+
Paperclip.options[:is_windows] ? "magick convert" : "convert",
|
42
|
+
arguments,
|
43
|
+
local_options,
|
44
|
+
)
|
40
45
|
end
|
41
46
|
|
42
47
|
# The identify method runs the identify binary with the provided arguments.
|
43
48
|
# See Paperclip.run for the available options.
|
44
49
|
def identify(arguments = "", local_options = {})
|
45
|
-
Paperclip.run(
|
50
|
+
Paperclip.run(
|
51
|
+
Paperclip.options[:is_windows] ? "magick identify" : "identify",
|
52
|
+
arguments,
|
53
|
+
local_options,
|
54
|
+
)
|
46
55
|
end
|
47
56
|
end
|
48
57
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Paperclip
|
2
|
+
class RailsEnvironment
|
3
|
+
def self.get
|
4
|
+
new.get
|
5
|
+
end
|
6
|
+
|
7
|
+
def get
|
8
|
+
if rails_exists? && rails_environment_exists?
|
9
|
+
Rails.env
|
10
|
+
else
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def rails_exists?
|
18
|
+
Object.const_defined?(:Rails)
|
19
|
+
end
|
20
|
+
|
21
|
+
def rails_environment_exists?
|
22
|
+
Rails.respond_to?(:env)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/paperclip/schema.rb
CHANGED
@@ -5,17 +5,14 @@ module Paperclip
|
|
5
5
|
module Schema
|
6
6
|
COLUMNS = {:file_name => :string,
|
7
7
|
:content_type => :string,
|
8
|
-
:file_size => :
|
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,12 +32,9 @@ 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.
|
42
|
-
|
43
|
-
remove_column(table_name, "#{attachment_name}_#{column_name}", column_type, column_options)
|
36
|
+
COLUMNS.keys.each do |column_name|
|
37
|
+
remove_column(table_name, "#{attachment_name}_#{column_name}")
|
44
38
|
end
|
45
39
|
end
|
46
40
|
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
|
-
|
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]
|
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
|
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
|
@@ -33,6 +33,10 @@ module Paperclip
|
|
33
33
|
# that is the alias to the S3 domain of your bucket, e.g.
|
34
34
|
# 'http://images.example.com'. This can also be used in
|
35
35
|
# conjunction with Cloudfront (http://aws.amazon.com/cloudfront)
|
36
|
+
# * +fog_options+: (optional) A hash of options that are passed
|
37
|
+
# to fog when the file is created. For example, you could set
|
38
|
+
# the multipart-chunk size to 100MB with a hash:
|
39
|
+
# { :multipart_chunk_size => 104857600 }
|
36
40
|
|
37
41
|
module Fog
|
38
42
|
def self.extended base
|
@@ -44,7 +48,7 @@ module Paperclip
|
|
44
48
|
end unless defined?(Fog)
|
45
49
|
|
46
50
|
base.instance_eval do
|
47
|
-
unless @options[:url].to_s.match(/\A:fog.*url\
|
51
|
+
unless @options[:url].to_s.match(/\A:fog.*url\z/)
|
48
52
|
@options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
|
49
53
|
@options[:url] = ':fog_public_url'
|
50
54
|
end
|
@@ -54,7 +58,7 @@ module Paperclip
|
|
54
58
|
end
|
55
59
|
end
|
56
60
|
|
57
|
-
AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\
|
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/
|
58
62
|
|
59
63
|
def exists?(style = default_style)
|
60
64
|
if original_filename
|
@@ -82,11 +86,14 @@ module Paperclip
|
|
82
86
|
end
|
83
87
|
|
84
88
|
def fog_public(style = default_style)
|
85
|
-
if @options.
|
86
|
-
|
87
|
-
|
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)
|
88
95
|
else
|
89
|
-
|
96
|
+
value
|
90
97
|
end
|
91
98
|
else
|
92
99
|
true
|
@@ -98,16 +105,19 @@ module Paperclip
|
|
98
105
|
log("saving #{path(style)}")
|
99
106
|
retried = false
|
100
107
|
begin
|
101
|
-
|
108
|
+
attributes = fog_file.merge(
|
102
109
|
:body => file,
|
103
110
|
:key => path(style),
|
104
111
|
:public => fog_public(style),
|
105
112
|
:content_type => file.content_type
|
106
|
-
)
|
113
|
+
)
|
114
|
+
attributes.merge!(@options[:fog_options]) if @options[:fog_options]
|
115
|
+
directory.files.create(attributes)
|
107
116
|
rescue Excon::Errors::NotFound
|
108
117
|
raise if retried
|
109
118
|
retried = true
|
110
119
|
directory.save
|
120
|
+
file.rewind
|
111
121
|
retry
|
112
122
|
ensure
|
113
123
|
file.rewind
|
@@ -141,8 +151,9 @@ module Paperclip
|
|
141
151
|
|
142
152
|
def expiring_url(time = (Time.now + 3600), style_name = default_style)
|
143
153
|
time = convert_time(time)
|
144
|
-
|
145
|
-
|
154
|
+
http_url_method = "get_#{scheme}_url"
|
155
|
+
if path(style_name) && directory.files.respond_to?(http_url_method)
|
156
|
+
expiring_url = directory.files.public_send(http_url_method, path(style_name), time)
|
146
157
|
|
147
158
|
if @options[:fog_host]
|
148
159
|
expiring_url.gsub!(/#{host_name_for_directory}/, dynamic_fog_host_for_style(style_name))
|
@@ -156,14 +167,14 @@ module Paperclip
|
|
156
167
|
|
157
168
|
def parse_credentials(creds)
|
158
169
|
creds = find_credentials(creds).stringify_keys
|
159
|
-
|
160
|
-
(creds[env] || creds).symbolize_keys
|
170
|
+
(creds[RailsEnvironment.get] || creds).symbolize_keys
|
161
171
|
end
|
162
172
|
|
163
173
|
def copy_to_local_file(style, local_dest_path)
|
164
174
|
log("copying #{path(style)} to local file #{local_dest_path}")
|
165
175
|
::File.open(local_dest_path, 'wb') do |local_file|
|
166
176
|
file = directory.files.get(path(style))
|
177
|
+
return false unless file
|
167
178
|
local_file.write(file.body)
|
168
179
|
end
|
169
180
|
rescue ::Fog::Errors::Error => e
|
@@ -174,7 +185,7 @@ module Paperclip
|
|
174
185
|
private
|
175
186
|
|
176
187
|
def convert_time(time)
|
177
|
-
if time.is_a?(
|
188
|
+
if time.is_a?(Integer)
|
178
189
|
time = Time.now + time
|
179
190
|
end
|
180
191
|
time
|
@@ -189,10 +200,10 @@ module Paperclip
|
|
189
200
|
end
|
190
201
|
|
191
202
|
def host_name_for_directory
|
192
|
-
if
|
193
|
-
"#{
|
203
|
+
if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
|
204
|
+
"#{directory_name}.s3.amazonaws.com"
|
194
205
|
else
|
195
|
-
"s3.amazonaws.com/#{
|
206
|
+
"s3.amazonaws.com/#{directory_name}"
|
196
207
|
end
|
197
208
|
end
|
198
209
|
|
@@ -218,13 +229,15 @@ module Paperclip
|
|
218
229
|
end
|
219
230
|
|
220
231
|
def directory
|
221
|
-
|
232
|
+
@directory ||= connection.directories.new(key: directory_name)
|
233
|
+
end
|
234
|
+
|
235
|
+
def directory_name
|
236
|
+
if @options[:fog_directory].respond_to?(:call)
|
222
237
|
@options[:fog_directory].call(self)
|
223
238
|
else
|
224
239
|
@options[:fog_directory]
|
225
240
|
end
|
226
|
-
|
227
|
-
@directory ||= connection.directories.new(:key => dir)
|
228
241
|
end
|
229
242
|
|
230
243
|
def scheme
|
data/lib/paperclip/storage/s3.rb
CHANGED
@@ -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'
|
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
|
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 =>
|
48
|
+
# :original => "private"
|
49
49
|
# }
|
50
50
|
# Or globally:
|
51
|
-
# :s3_permissions =>
|
51
|
+
# :s3_permissions => "private"
|
52
52
|
#
|
53
|
-
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets.
|
54
|
-
# 'http', 'https', or an empty string to generate
|
55
|
-
#
|
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
|
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
|
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>:
|
102
|
-
# Redundancy Storage.
|
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 => :
|
114
|
+
# :thumb => :REDUCED_REDUNDANCY
|
109
115
|
# }
|
116
|
+
#
|
110
117
|
# Or globally:
|
111
|
-
# :s3_storage_class => :
|
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
|
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' : 'https'
|
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 =
|
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,33 +147,44 @@ 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\
|
161
|
-
@options[:path] = path_option.gsub(/:url/, @options[:url]).
|
162
|
-
@options[:url] = ":s3_path_url"
|
150
|
+
unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
|
151
|
+
@options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
|
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
|
-
|
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
|
-
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).
|
171
|
+
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
|
174
172
|
end unless Paperclip::Interpolations.respond_to? :s3_path_url
|
175
173
|
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
|
176
|
-
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).
|
174
|
+
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
|
177
175
|
end unless Paperclip::Interpolations.respond_to? :s3_domain_url
|
178
176
|
Paperclip.interpolates(:asset_host) do |attachment, style|
|
179
|
-
"#{attachment.path(style).
|
177
|
+
"#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
|
180
178
|
end unless Paperclip::Interpolations.respond_to? :asset_host
|
181
179
|
end
|
182
180
|
|
183
181
|
def expiring_url(time = 3600, style_name = default_style)
|
184
182
|
if path(style_name)
|
185
|
-
base_options = { :
|
186
|
-
s3_object(style_name).
|
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
|
@@ -197,7 +198,14 @@ module Paperclip
|
|
197
198
|
host_name = @options[:s3_host_name]
|
198
199
|
host_name = host_name.call(self) if host_name.is_a?(Proc)
|
199
200
|
|
200
|
-
host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
|
201
|
+
host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
|
202
|
+
end
|
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]
|
201
209
|
end
|
202
210
|
|
203
211
|
def s3_host_alias
|
@@ -206,6 +214,10 @@ module Paperclip
|
|
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 = { :
|
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
|
-
[:
|
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] ||=
|
261
|
+
instances[options] ||= ::Aws::S3::Resource.new(options)
|
248
262
|
end
|
249
263
|
|
250
264
|
def s3_bucket
|
251
|
-
@s3_bucket ||= s3_interface.
|
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.
|
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] || :
|
302
|
+
permissions.merge :default => (permissions[:default] || :"public-read")
|
281
303
|
end
|
282
304
|
|
283
305
|
def set_storage_class(storage_class)
|
@@ -286,10 +308,9 @@ module Paperclip
|
|
286
308
|
end
|
287
309
|
|
288
310
|
def parse_credentials creds
|
289
|
-
creds = creds.respond_to?(
|
311
|
+
creds = creds.respond_to?(:call) ? creds.call(self) : creds
|
290
312
|
creds = find_credentials(creds).stringify_keys
|
291
|
-
|
292
|
-
(creds[env] || creds).symbolize_keys
|
313
|
+
(creds[RailsEnvironment.get] || creds).symbolize_keys
|
293
314
|
end
|
294
315
|
|
295
316
|
def exists?(style = default_style)
|
@@ -298,7 +319,7 @@ module Paperclip
|
|
298
319
|
else
|
299
320
|
false
|
300
321
|
end
|
301
|
-
rescue
|
322
|
+
rescue Aws::Errors::ServiceError => e
|
302
323
|
false
|
303
324
|
end
|
304
325
|
|
@@ -324,7 +345,7 @@ module Paperclip
|
|
324
345
|
end
|
325
346
|
|
326
347
|
def create_bucket
|
327
|
-
s3_interface.
|
348
|
+
s3_interface.bucket(bucket_name).create
|
328
349
|
end
|
329
350
|
|
330
351
|
def flush_writes #:nodoc:
|
@@ -332,11 +353,9 @@ module Paperclip
|
|
332
353
|
retries = 0
|
333
354
|
begin
|
334
355
|
log("saving #{path(style)}")
|
335
|
-
acl = @s3_permissions[style] || @s3_permissions[:default]
|
336
|
-
acl = acl.call(self, style) if acl.respond_to?(:call)
|
337
356
|
write_options = {
|
338
357
|
:content_type => file.content_type,
|
339
|
-
:acl =>
|
358
|
+
:acl => s3_permissions(style)
|
340
359
|
}
|
341
360
|
|
342
361
|
# add storage class for this style if defined
|
@@ -357,11 +376,11 @@ module Paperclip
|
|
357
376
|
write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
|
358
377
|
write_options.merge!(@s3_headers)
|
359
378
|
|
360
|
-
s3_object(style).
|
361
|
-
rescue
|
379
|
+
s3_object(style).upload_file(file.path, write_options)
|
380
|
+
rescue ::Aws::S3::Errors::NoSuchBucket
|
362
381
|
create_bucket
|
363
382
|
retry
|
364
|
-
rescue
|
383
|
+
rescue ::Aws::S3::Errors::SlowDown
|
365
384
|
retries += 1
|
366
385
|
if retries <= 5
|
367
386
|
sleep((2 ** retries) * 0.5)
|
@@ -383,8 +402,8 @@ module Paperclip
|
|
383
402
|
@queued_for_delete.each do |path|
|
384
403
|
begin
|
385
404
|
log("deleting #{path}")
|
386
|
-
s3_bucket.
|
387
|
-
rescue
|
405
|
+
s3_bucket.object(path.sub(%r{\A/}, "")).delete
|
406
|
+
rescue Aws::Errors::ServiceError => e
|
388
407
|
# Ignore this.
|
389
408
|
end
|
390
409
|
end
|
@@ -394,11 +413,11 @@ module Paperclip
|
|
394
413
|
def copy_to_local_file(style, local_dest_path)
|
395
414
|
log("copying #{path(style)} to local file #{local_dest_path}")
|
396
415
|
::File.open(local_dest_path, 'wb') do |local_file|
|
397
|
-
s3_object(style).
|
416
|
+
s3_object(style).get do |chunk|
|
398
417
|
local_file.write(chunk)
|
399
418
|
end
|
400
419
|
end
|
401
|
-
rescue
|
420
|
+
rescue Aws::Errors::ServiceError => e
|
402
421
|
warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
|
403
422
|
false
|
404
423
|
end
|
data/lib/paperclip/style.rb
CHANGED