paperclip 3.5.4 → 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/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +17 -20
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +11 -3
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +262 -49
- data/README.md +496 -169
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +170 -52
- data/lib/paperclip/attachment_registry.rb +3 -2
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -22
- data/lib/paperclip/errors.rb +8 -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 +6 -4
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +31 -13
- data/lib/paperclip/io_adapters/abstract_adapter.rb +34 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +19 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- 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 +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +38 -20
- data/lib/paperclip/storage/s3.rb +124 -73
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +5 -1
- data/lib/paperclip/thumbnail.rb +34 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +11 -4
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +31 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +21 -16
- data/shoulda_macros/paperclip.rb +0 -1
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -21
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +519 -409
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +174 -129
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +254 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +158 -137
- data/spec/paperclip/url_generator_spec.rb +221 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +344 -226
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -88
- data/test/file_command_content_type_detector_test.rb +0 -27
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -84
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -232
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -74
- data/test/io_adapters/empty_string_adapter_test.rb +0 -18
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -90
- data/test/paperclip_test.rb +0 -217
- data/test/plural_cache_test.rb +0 -36
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -473
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1356
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -17
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -324
- data/test/validators_test.rb +0 -61
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -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
|
@@ -69,7 +69,7 @@ module Paperclip
|
|
69
69
|
while(true)
|
70
70
|
path = File.dirname(path)
|
71
71
|
FileUtils.rmdir(path)
|
72
|
-
break if File.
|
72
|
+
break if File.exist?(path) # Ruby 1.9.2 does not raise if the removal failed.
|
73
73
|
end
|
74
74
|
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
|
75
75
|
# Stop trying to remove parent directories
|
@@ -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
|
@@ -14,11 +14,12 @@ module Paperclip
|
|
14
14
|
# aws_secret_access_key: '<your aws_secret_access_key>'
|
15
15
|
# provider: 'AWS'
|
16
16
|
# region: 'eu-west-1'
|
17
|
+
# scheme: 'https'
|
17
18
|
# * +fog_directory+: This is the name of the S3 bucket that will
|
18
19
|
# store your files. Remember that the bucket must be unique across
|
19
20
|
# all of Amazon S3. If the bucket does not exist, Paperclip will
|
20
21
|
# attempt to create it.
|
21
|
-
# * +fog_file
|
22
|
+
# * +fog_file+: This can be hash or lambda returning hash. The
|
22
23
|
# value is used as base properties for new uploaded file.
|
23
24
|
# * +path+: This is the key under the bucket in which the file will
|
24
25
|
# be stored. The URL will be constructed from the bucket and the
|
@@ -32,6 +33,10 @@ module Paperclip
|
|
32
33
|
# that is the alias to the S3 domain of your bucket, e.g.
|
33
34
|
# 'http://images.example.com'. This can also be used in
|
34
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 }
|
35
40
|
|
36
41
|
module Fog
|
37
42
|
def self.extended base
|
@@ -43,7 +48,7 @@ module Paperclip
|
|
43
48
|
end unless defined?(Fog)
|
44
49
|
|
45
50
|
base.instance_eval do
|
46
|
-
unless @options[:url].to_s.match(/\A:fog.*url\
|
51
|
+
unless @options[:url].to_s.match(/\A:fog.*url\z/)
|
47
52
|
@options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
|
48
53
|
@options[:url] = ':fog_public_url'
|
49
54
|
end
|
@@ -53,7 +58,7 @@ module Paperclip
|
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
56
|
-
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/
|
57
62
|
|
58
63
|
def exists?(style = default_style)
|
59
64
|
if original_filename
|
@@ -81,11 +86,14 @@ module Paperclip
|
|
81
86
|
end
|
82
87
|
|
83
88
|
def fog_public(style = default_style)
|
84
|
-
if @options.
|
85
|
-
|
86
|
-
|
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)
|
87
95
|
else
|
88
|
-
|
96
|
+
value
|
89
97
|
end
|
90
98
|
else
|
91
99
|
true
|
@@ -97,16 +105,19 @@ module Paperclip
|
|
97
105
|
log("saving #{path(style)}")
|
98
106
|
retried = false
|
99
107
|
begin
|
100
|
-
|
108
|
+
attributes = fog_file.merge(
|
101
109
|
:body => file,
|
102
110
|
:key => path(style),
|
103
111
|
:public => fog_public(style),
|
104
112
|
:content_type => file.content_type
|
105
|
-
)
|
113
|
+
)
|
114
|
+
attributes.merge!(@options[:fog_options]) if @options[:fog_options]
|
115
|
+
directory.files.create(attributes)
|
106
116
|
rescue Excon::Errors::NotFound
|
107
117
|
raise if retried
|
108
118
|
retried = true
|
109
119
|
directory.save
|
120
|
+
file.rewind
|
110
121
|
retry
|
111
122
|
ensure
|
112
123
|
file.rewind
|
@@ -131,7 +142,7 @@ module Paperclip
|
|
131
142
|
"#{dynamic_fog_host_for_style(style)}/#{path(style)}"
|
132
143
|
else
|
133
144
|
if fog_credentials[:provider] == 'AWS'
|
134
|
-
"
|
145
|
+
"#{scheme}://#{host_name_for_directory}/#{path(style)}"
|
135
146
|
else
|
136
147
|
directory.files.new(:key => path(style)).public_url
|
137
148
|
end
|
@@ -140,8 +151,9 @@ module Paperclip
|
|
140
151
|
|
141
152
|
def expiring_url(time = (Time.now + 3600), style_name = default_style)
|
142
153
|
time = convert_time(time)
|
143
|
-
|
144
|
-
|
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)
|
145
157
|
|
146
158
|
if @options[:fog_host]
|
147
159
|
expiring_url.gsub!(/#{host_name_for_directory}/, dynamic_fog_host_for_style(style_name))
|
@@ -155,14 +167,14 @@ module Paperclip
|
|
155
167
|
|
156
168
|
def parse_credentials(creds)
|
157
169
|
creds = find_credentials(creds).stringify_keys
|
158
|
-
|
159
|
-
(creds[env] || creds).symbolize_keys
|
170
|
+
(creds[RailsEnvironment.get] || creds).symbolize_keys
|
160
171
|
end
|
161
172
|
|
162
173
|
def copy_to_local_file(style, local_dest_path)
|
163
174
|
log("copying #{path(style)} to local file #{local_dest_path}")
|
164
175
|
::File.open(local_dest_path, 'wb') do |local_file|
|
165
176
|
file = directory.files.get(path(style))
|
177
|
+
return false unless file
|
166
178
|
local_file.write(file.body)
|
167
179
|
end
|
168
180
|
rescue ::Fog::Errors::Error => e
|
@@ -173,7 +185,7 @@ module Paperclip
|
|
173
185
|
private
|
174
186
|
|
175
187
|
def convert_time(time)
|
176
|
-
if time.is_a?(
|
188
|
+
if time.is_a?(Integer)
|
177
189
|
time = Time.now + time
|
178
190
|
end
|
179
191
|
time
|
@@ -188,10 +200,10 @@ module Paperclip
|
|
188
200
|
end
|
189
201
|
|
190
202
|
def host_name_for_directory
|
191
|
-
if
|
192
|
-
"#{
|
203
|
+
if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
|
204
|
+
"#{directory_name}.s3.amazonaws.com"
|
193
205
|
else
|
194
|
-
"s3.amazonaws.com/#{
|
206
|
+
"s3.amazonaws.com/#{directory_name}"
|
195
207
|
end
|
196
208
|
end
|
197
209
|
|
@@ -217,13 +229,19 @@ module Paperclip
|
|
217
229
|
end
|
218
230
|
|
219
231
|
def directory
|
220
|
-
|
232
|
+
@directory ||= connection.directories.new(key: directory_name)
|
233
|
+
end
|
234
|
+
|
235
|
+
def directory_name
|
236
|
+
if @options[:fog_directory].respond_to?(:call)
|
221
237
|
@options[:fog_directory].call(self)
|
222
238
|
else
|
223
239
|
@options[:fog_directory]
|
224
240
|
end
|
241
|
+
end
|
225
242
|
|
226
|
-
|
243
|
+
def scheme
|
244
|
+
@scheme ||= fog_credentials[:scheme] || 'https'
|
227
245
|
end
|
228
246
|
end
|
229
247
|
end
|
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
|
@@ -26,7 +26,7 @@ module Paperclip
|
|
26
26
|
# put your bucket name in this file, instead of adding it to the code directly.
|
27
27
|
# This is useful when you want the same account but a different bucket for
|
28
28
|
# development versus production.
|
29
|
-
# When using a Proc it provides a single parameter which is the attachment itself. A
|
29
|
+
# When using a Proc it provides a single parameter which is the attachment itself. A
|
30
30
|
# method #instance is available on the attachment which will take you back to your
|
31
31
|
# code. eg.
|
32
32
|
# class User
|
@@ -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,90 +94,97 @@ 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
|
111
|
+
#
|
112
|
+
# You can set storage class on a per style bases by doing the following:
|
113
|
+
# :s3_storage_class => {
|
114
|
+
# :thumb => :REDUCED_REDUNDANCY
|
115
|
+
# }
|
116
|
+
#
|
117
|
+
# Or globally:
|
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.
|
122
|
+
|
105
123
|
module S3
|
106
124
|
def self.extended base
|
107
125
|
begin
|
108
|
-
require
|
126
|
+
require "aws-sdk-s3"
|
109
127
|
rescue LoadError => e
|
110
|
-
e.message << " (You may need to install the aws-sdk gem)"
|
128
|
+
e.message << " (You may need to install the aws-sdk-s3 gem)"
|
111
129
|
raise e
|
112
|
-
end unless defined?(AWS::Core)
|
113
|
-
|
114
|
-
# Overriding log formatter to make sure it return a UTF-8 string
|
115
|
-
if defined?(AWS::Core::LogFormatter)
|
116
|
-
AWS::Core::LogFormatter.class_eval do
|
117
|
-
def summarize_hash(hash)
|
118
|
-
hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
|
119
|
-
end
|
120
|
-
end
|
121
|
-
elsif defined?(AWS::Core::ClientLogging)
|
122
|
-
AWS::Core::ClientLogging.class_eval do
|
123
|
-
def sanitize_hash(hash)
|
124
|
-
hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
|
125
|
-
end
|
126
|
-
end
|
127
130
|
end
|
128
131
|
|
129
132
|
base.instance_eval do
|
130
133
|
@s3_options = @options[:s3_options] || {}
|
131
134
|
@s3_permissions = set_permissions(@options[:s3_permissions])
|
132
|
-
@s3_protocol = @options[:s3_protocol]
|
133
|
-
Proc.new do |style, attachment|
|
134
|
-
permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
|
135
|
-
permission = permission.call(attachment, style) if permission.respond_to?(:call)
|
136
|
-
(permission == :public_read) ? 'http' : 'https'
|
137
|
-
end
|
135
|
+
@s3_protocol = @options[:s3_protocol] || "".freeze
|
138
136
|
@s3_metadata = @options[:s3_metadata] || {}
|
139
137
|
@s3_headers = {}
|
140
138
|
merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)
|
141
139
|
|
142
|
-
@
|
140
|
+
@s3_storage_class = set_storage_class(@options[:s3_storage_class])
|
143
141
|
|
144
|
-
@s3_server_side_encryption =
|
142
|
+
@s3_server_side_encryption = "AES256"
|
145
143
|
if @options[:s3_server_side_encryption].blank?
|
146
144
|
@s3_server_side_encryption = false
|
147
145
|
end
|
148
146
|
if @s3_server_side_encryption
|
149
|
-
@s3_server_side_encryption = @options[:s3_server_side_encryption]
|
147
|
+
@s3_server_side_encryption = @options[:s3_server_side_encryption]
|
150
148
|
end
|
151
149
|
|
152
|
-
unless @options[:url].to_s.match(/\A:s3.*url\
|
153
|
-
@options[:path] =
|
154
|
-
@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
|
155
153
|
end
|
156
154
|
@options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
|
157
155
|
|
158
156
|
@http_proxy = @options[:http_proxy] || nil
|
157
|
+
|
158
|
+
@use_accelerate_endpoint = @options[:use_accelerate_endpoint]
|
159
159
|
end
|
160
160
|
|
161
161
|
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
|
162
|
-
|
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}"
|
163
169
|
end unless Paperclip::Interpolations.respond_to? :s3_alias_url
|
164
170
|
Paperclip.interpolates(:s3_path_url) do |attachment, style|
|
165
|
-
"#{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)}"
|
166
172
|
end unless Paperclip::Interpolations.respond_to? :s3_path_url
|
167
173
|
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
|
168
|
-
"#{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)}"
|
169
175
|
end unless Paperclip::Interpolations.respond_to? :s3_domain_url
|
170
176
|
Paperclip.interpolates(:asset_host) do |attachment, style|
|
171
|
-
"#{attachment.path(style).
|
177
|
+
"#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
|
172
178
|
end unless Paperclip::Interpolations.respond_to? :asset_host
|
173
179
|
end
|
174
180
|
|
175
181
|
def expiring_url(time = 3600, style_name = default_style)
|
176
182
|
if path(style_name)
|
177
|
-
base_options = { :
|
178
|
-
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
|
179
188
|
else
|
180
189
|
url(style_name)
|
181
190
|
end
|
@@ -189,7 +198,14 @@ module Paperclip
|
|
189
198
|
host_name = @options[:s3_host_name]
|
190
199
|
host_name = host_name.call(self) if host_name.is_a?(Proc)
|
191
200
|
|
192
|
-
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]
|
193
209
|
end
|
194
210
|
|
195
211
|
def s3_host_alias
|
@@ -198,6 +214,10 @@ module Paperclip
|
|
198
214
|
@s3_host_alias
|
199
215
|
end
|
200
216
|
|
217
|
+
def s3_prefixes_in_alias
|
218
|
+
@s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
|
219
|
+
end
|
220
|
+
|
201
221
|
def s3_url_options
|
202
222
|
s3_url_options = @options[:s3_url_options] || {}
|
203
223
|
s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
|
@@ -212,7 +232,7 @@ module Paperclip
|
|
212
232
|
|
213
233
|
def s3_interface
|
214
234
|
@s3_interface ||= begin
|
215
|
-
config = { :
|
235
|
+
config = { region: s3_region }
|
216
236
|
|
217
237
|
if using_http_proxy?
|
218
238
|
|
@@ -226,7 +246,9 @@ module Paperclip
|
|
226
246
|
config[:proxy_uri] = URI::HTTP.build(proxy_opts)
|
227
247
|
end
|
228
248
|
|
229
|
-
[:
|
249
|
+
config[:use_accelerate_endpoint] = use_accelerate_endpoint?
|
250
|
+
|
251
|
+
[:access_key_id, :secret_access_key, :credential_provider, :credentials].each do |opt|
|
230
252
|
config[opt] = s3_credentials[opt] if s3_credentials[opt]
|
231
253
|
end
|
232
254
|
|
@@ -236,15 +258,23 @@ module Paperclip
|
|
236
258
|
|
237
259
|
def obtain_s3_instance_for(options)
|
238
260
|
instances = (Thread.current[:paperclip_s3_instances] ||= {})
|
239
|
-
instances[options] ||=
|
261
|
+
instances[options] ||= ::Aws::S3::Resource.new(options)
|
240
262
|
end
|
241
263
|
|
242
264
|
def s3_bucket
|
243
|
-
@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/},'')
|
244
270
|
end
|
245
271
|
|
246
272
|
def s3_object style_name = default_style
|
247
|
-
s3_bucket.
|
273
|
+
s3_bucket.object style_name_as_path(style_name)
|
274
|
+
end
|
275
|
+
|
276
|
+
def use_accelerate_endpoint?
|
277
|
+
!!@use_accelerate_endpoint
|
248
278
|
end
|
249
279
|
|
250
280
|
def using_http_proxy?
|
@@ -269,14 +299,18 @@ module Paperclip
|
|
269
299
|
|
270
300
|
def set_permissions permissions
|
271
301
|
permissions = { :default => permissions } unless permissions.respond_to?(:merge)
|
272
|
-
permissions.merge :default => (permissions[:default] || :
|
302
|
+
permissions.merge :default => (permissions[:default] || :"public-read")
|
303
|
+
end
|
304
|
+
|
305
|
+
def set_storage_class(storage_class)
|
306
|
+
storage_class = {:default => storage_class} unless storage_class.respond_to?(:merge)
|
307
|
+
storage_class
|
273
308
|
end
|
274
309
|
|
275
310
|
def parse_credentials creds
|
276
|
-
creds = creds.respond_to?(
|
311
|
+
creds = creds.respond_to?(:call) ? creds.call(self) : creds
|
277
312
|
creds = find_credentials(creds).stringify_keys
|
278
|
-
|
279
|
-
(creds[env] || creds).symbolize_keys
|
313
|
+
(creds[RailsEnvironment.get] || creds).symbolize_keys
|
280
314
|
end
|
281
315
|
|
282
316
|
def exists?(style = default_style)
|
@@ -285,7 +319,7 @@ module Paperclip
|
|
285
319
|
else
|
286
320
|
false
|
287
321
|
end
|
288
|
-
rescue
|
322
|
+
rescue Aws::Errors::ServiceError => e
|
289
323
|
false
|
290
324
|
end
|
291
325
|
|
@@ -295,6 +329,10 @@ module Paperclip
|
|
295
329
|
s3_permissions
|
296
330
|
end
|
297
331
|
|
332
|
+
def s3_storage_class(style = default_style)
|
333
|
+
@s3_storage_class[style] || @s3_storage_class[:default]
|
334
|
+
end
|
335
|
+
|
298
336
|
def s3_protocol(style = default_style, with_colon = false)
|
299
337
|
protocol = @s3_protocol
|
300
338
|
protocol = protocol.call(style, self) if protocol.respond_to?(:call)
|
@@ -307,19 +345,23 @@ module Paperclip
|
|
307
345
|
end
|
308
346
|
|
309
347
|
def create_bucket
|
310
|
-
s3_interface.
|
348
|
+
s3_interface.bucket(bucket_name).create
|
311
349
|
end
|
312
350
|
|
313
351
|
def flush_writes #:nodoc:
|
314
352
|
@queued_for_write.each do |style, file|
|
353
|
+
retries = 0
|
315
354
|
begin
|
316
355
|
log("saving #{path(style)}")
|
317
|
-
acl = @s3_permissions[style] || @s3_permissions[:default]
|
318
|
-
acl = acl.call(self, style) if acl.respond_to?(:call)
|
319
356
|
write_options = {
|
320
357
|
:content_type => file.content_type,
|
321
|
-
:acl =>
|
358
|
+
:acl => s3_permissions(style)
|
322
359
|
}
|
360
|
+
|
361
|
+
# add storage class for this style if defined
|
362
|
+
storage_class = s3_storage_class(style)
|
363
|
+
write_options.merge!(:storage_class => storage_class) if storage_class
|
364
|
+
|
323
365
|
if @s3_server_side_encryption
|
324
366
|
write_options[:server_side_encryption] = @s3_server_side_encryption
|
325
367
|
end
|
@@ -334,10 +376,18 @@ module Paperclip
|
|
334
376
|
write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
|
335
377
|
write_options.merge!(@s3_headers)
|
336
378
|
|
337
|
-
s3_object(style).
|
338
|
-
rescue
|
379
|
+
s3_object(style).upload_file(file.path, write_options)
|
380
|
+
rescue ::Aws::S3::Errors::NoSuchBucket
|
339
381
|
create_bucket
|
340
382
|
retry
|
383
|
+
rescue ::Aws::S3::Errors::SlowDown
|
384
|
+
retries += 1
|
385
|
+
if retries <= 5
|
386
|
+
sleep((2 ** retries) * 0.5)
|
387
|
+
retry
|
388
|
+
else
|
389
|
+
raise
|
390
|
+
end
|
341
391
|
ensure
|
342
392
|
file.rewind
|
343
393
|
end
|
@@ -352,8 +402,8 @@ module Paperclip
|
|
352
402
|
@queued_for_delete.each do |path|
|
353
403
|
begin
|
354
404
|
log("deleting #{path}")
|
355
|
-
s3_bucket.
|
356
|
-
rescue
|
405
|
+
s3_bucket.object(path.sub(%r{\A/}, "")).delete
|
406
|
+
rescue Aws::Errors::ServiceError => e
|
357
407
|
# Ignore this.
|
358
408
|
end
|
359
409
|
end
|
@@ -362,11 +412,12 @@ module Paperclip
|
|
362
412
|
|
363
413
|
def copy_to_local_file(style, local_dest_path)
|
364
414
|
log("copying #{path(style)} to local file #{local_dest_path}")
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
415
|
+
::File.open(local_dest_path, 'wb') do |local_file|
|
416
|
+
s3_object(style).get do |chunk|
|
417
|
+
local_file.write(chunk)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
rescue Aws::Errors::ServiceError => e
|
370
421
|
warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
|
371
422
|
false
|
372
423
|
end
|
data/lib/paperclip/style.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module Paperclip
|
3
2
|
# The Style class holds the definition of a thumbnail style, applying
|
4
3
|
# whatever processing is required to normalize the definition and delaying
|
@@ -29,7 +28,7 @@ module Paperclip
|
|
29
28
|
@geometry, @format = [definition, nil].flatten[0..1]
|
30
29
|
@other_args = {}
|
31
30
|
end
|
32
|
-
@format
|
31
|
+
@format = default_format if @format.blank?
|
33
32
|
end
|
34
33
|
|
35
34
|
# retrieves from the attachment the processors defined in the has_attached_file call
|
@@ -71,7 +70,7 @@ module Paperclip
|
|
71
70
|
# Arguments other than the standard geometry, format etc are just passed through from
|
72
71
|
# initialization and any procs are called here, just before post-processing.
|
73
72
|
def processor_options
|
74
|
-
args = {}
|
73
|
+
args = {:style => name}
|
75
74
|
@other_args.each do |k,v|
|
76
75
|
args[k] = v.respond_to?(:call) ? v.call(attachment) : v
|
77
76
|
end
|
@@ -99,5 +98,11 @@ module Paperclip
|
|
99
98
|
end
|
100
99
|
end
|
101
100
|
|
101
|
+
# defaults to default format (nil by default)
|
102
|
+
def default_format
|
103
|
+
base = attachment.options[:default_format]
|
104
|
+
base.respond_to?(:call) ? base.call(attachment, name) : base
|
105
|
+
end
|
106
|
+
|
102
107
|
end
|
103
108
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Paperclip
|
2
2
|
class TempfileFactory
|
3
3
|
|
4
|
-
def generate(name)
|
4
|
+
def generate(name = random_name)
|
5
5
|
@name = name
|
6
6
|
file = Tempfile.new([basename, extension])
|
7
7
|
file.binmode
|
@@ -15,5 +15,9 @@ module Paperclip
|
|
15
15
|
def basename
|
16
16
|
Digest::MD5.hexdigest(File.basename(@name, extension))
|
17
17
|
end
|
18
|
+
|
19
|
+
def random_name
|
20
|
+
SecureRandom.uuid
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|