carrierwave 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +95 -34
- data/lib/carrierwave/downloader/base.rb +50 -0
- data/lib/carrierwave/downloader/remote_file.rb +42 -0
- data/lib/carrierwave/locale/en.yml +1 -1
- data/lib/carrierwave/mount.rb +29 -21
- data/lib/carrierwave/mounter.rb +70 -47
- data/lib/carrierwave/orm/activerecord.rb +18 -12
- data/lib/carrierwave/processing/mini_magick.rb +102 -99
- data/lib/carrierwave/processing/rmagick.rb +24 -3
- data/lib/carrierwave/sanitized_file.rb +39 -24
- data/lib/carrierwave/storage/file.rb +7 -3
- data/lib/carrierwave/storage/fog.rb +77 -19
- data/lib/carrierwave/storage.rb +1 -0
- data/lib/carrierwave/uploader/cache.rb +24 -16
- data/lib/carrierwave/uploader/configuration.rb +32 -16
- data/lib/carrierwave/uploader/content_type_whitelist.rb +1 -1
- data/lib/carrierwave/uploader/download.rb +2 -75
- data/lib/carrierwave/uploader/extension_whitelist.rb +2 -1
- data/lib/carrierwave/uploader/file_size.rb +4 -2
- data/lib/carrierwave/uploader/mountable.rb +6 -0
- data/lib/carrierwave/uploader/processing.rb +9 -7
- data/lib/carrierwave/uploader/proxy.rb +2 -2
- data/lib/carrierwave/uploader/serialization.rb +1 -1
- data/lib/carrierwave/uploader/store.rb +13 -2
- data/lib/carrierwave/uploader/url.rb +2 -2
- data/lib/carrierwave/uploader/versions.rb +50 -13
- data/lib/carrierwave/utilities/uri.rb +2 -0
- data/lib/carrierwave/validations/active_model.rb +3 -3
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +28 -0
- data/lib/generators/templates/uploader.rb +1 -3
- metadata +103 -34
- data/lib/carrierwave/uploader/magic_mime_blacklist.rb +0 -94
- data/lib/carrierwave/uploader/magic_mime_whitelist.rb +0 -94
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
require 'pathname'
|
|
2
2
|
require 'active_support/core_ext/string/multibyte'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
# Use mime/types/columnar if available, for reduced memory usage
|
|
6
|
-
require 'mime/types/columnar'
|
|
7
|
-
rescue LoadError
|
|
8
|
-
require 'mime/types'
|
|
9
|
-
end
|
|
3
|
+
require 'mini_mime'
|
|
4
|
+
require 'mimemagic'
|
|
10
5
|
|
|
11
6
|
module CarrierWave
|
|
12
7
|
|
|
@@ -20,7 +15,7 @@ module CarrierWave
|
|
|
20
15
|
#
|
|
21
16
|
class SanitizedFile
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
attr_reader :file
|
|
24
19
|
|
|
25
20
|
class << self
|
|
26
21
|
attr_writer :sanitize_regexp
|
|
@@ -32,6 +27,7 @@ module CarrierWave
|
|
|
32
27
|
|
|
33
28
|
def initialize(file)
|
|
34
29
|
self.file = file
|
|
30
|
+
@content = nil
|
|
35
31
|
end
|
|
36
32
|
|
|
37
33
|
##
|
|
@@ -113,12 +109,11 @@ module CarrierWave
|
|
|
113
109
|
# [String, nil] the path where the file is located.
|
|
114
110
|
#
|
|
115
111
|
def path
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
end
|
|
112
|
+
return if @file.blank?
|
|
113
|
+
if is_path?
|
|
114
|
+
File.expand_path(@file)
|
|
115
|
+
elsif @file.respond_to?(:path) && !@file.path.blank?
|
|
116
|
+
File.expand_path(@file.path)
|
|
122
117
|
end
|
|
123
118
|
end
|
|
124
119
|
|
|
@@ -186,9 +181,9 @@ module CarrierWave
|
|
|
186
181
|
move!(new_path)
|
|
187
182
|
chmod!(new_path, permissions)
|
|
188
183
|
if keep_filename
|
|
189
|
-
self.file = {:tempfile => new_path, :filename => original_filename}
|
|
184
|
+
self.file = {:tempfile => new_path, :filename => original_filename, :content_type => content_type}
|
|
190
185
|
else
|
|
191
|
-
self.file = new_path
|
|
186
|
+
self.file = {:tempfile => new_path, :content_type => content_type}
|
|
192
187
|
end
|
|
193
188
|
self
|
|
194
189
|
end
|
|
@@ -197,7 +192,7 @@ module CarrierWave
|
|
|
197
192
|
#
|
|
198
193
|
def move!(new_path)
|
|
199
194
|
if exists?
|
|
200
|
-
FileUtils.mv(path, new_path) unless new_path
|
|
195
|
+
FileUtils.mv(path, new_path) unless File.identical?(new_path, path)
|
|
201
196
|
else
|
|
202
197
|
File.open(new_path, "wb") { |f| f.write(read) }
|
|
203
198
|
end
|
|
@@ -264,12 +259,10 @@ module CarrierWave
|
|
|
264
259
|
# [String] the content type of the file
|
|
265
260
|
#
|
|
266
261
|
def content_type
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
@content_type = ::MIME::Types.type_for(path).first.to_s
|
|
272
|
-
end
|
|
262
|
+
@content_type ||=
|
|
263
|
+
existing_content_type ||
|
|
264
|
+
mime_magic_content_type ||
|
|
265
|
+
mini_mime_content_type
|
|
273
266
|
end
|
|
274
267
|
|
|
275
268
|
##
|
|
@@ -325,10 +318,32 @@ module CarrierWave
|
|
|
325
318
|
name = File.basename(name)
|
|
326
319
|
name = name.gsub(sanitize_regexp,"_")
|
|
327
320
|
name = "_#{name}" if name =~ /\A\.+\z/
|
|
328
|
-
name = "unnamed" if name.size
|
|
321
|
+
name = "unnamed" if name.size.zero?
|
|
329
322
|
return name.mb_chars.to_s
|
|
330
323
|
end
|
|
331
324
|
|
|
325
|
+
def existing_content_type
|
|
326
|
+
if @file.respond_to?(:content_type) && @file.content_type
|
|
327
|
+
@file.content_type.to_s.chomp
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def mime_magic_content_type
|
|
332
|
+
if path
|
|
333
|
+
File.open(path) do |file|
|
|
334
|
+
MimeMagic.by_magic(file).try(:type) || 'invalid/invalid'
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
rescue Errno::ENOENT
|
|
338
|
+
nil
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def mini_mime_content_type
|
|
342
|
+
return unless path
|
|
343
|
+
mime_type = ::MiniMime.lookup_by_filename(path)
|
|
344
|
+
@content_type = (mime_type && mime_type.content_type).to_s
|
|
345
|
+
end
|
|
346
|
+
|
|
332
347
|
def split_extension(filename)
|
|
333
348
|
# regular expressions to try for identifying extensions
|
|
334
349
|
extension_matchers = [
|
|
@@ -7,6 +7,10 @@ module CarrierWave
|
|
|
7
7
|
# pretty much it.
|
|
8
8
|
#
|
|
9
9
|
class File < Abstract
|
|
10
|
+
def initialize(*)
|
|
11
|
+
super
|
|
12
|
+
@cache_called = nil
|
|
13
|
+
end
|
|
10
14
|
|
|
11
15
|
##
|
|
12
16
|
# Move the file to the uploader's store path.
|
|
@@ -62,7 +66,7 @@ module CarrierWave
|
|
|
62
66
|
#
|
|
63
67
|
def cache!(new_file)
|
|
64
68
|
new_file.move_to(::File.expand_path(uploader.cache_path, uploader.root), uploader.permissions, uploader.directory_permissions, true)
|
|
65
|
-
rescue Errno::EMLINK => e
|
|
69
|
+
rescue Errno::EMLINK, Errno::ENOSPC => e
|
|
66
70
|
raise(e) if @cache_called
|
|
67
71
|
@cache_called = true
|
|
68
72
|
|
|
@@ -106,8 +110,8 @@ module CarrierWave
|
|
|
106
110
|
|
|
107
111
|
def clean_cache!(seconds)
|
|
108
112
|
Dir.glob(::File.expand_path(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir|
|
|
109
|
-
# generate_cache_id returns key formated TIMEINT-PID-COUNTER-RND
|
|
110
|
-
time = dir.scan(/(\d+)-\d+-\d
|
|
113
|
+
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
|
|
114
|
+
time = dir.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map(&:to_i)
|
|
111
115
|
time = Time.at(*time)
|
|
112
116
|
if time < (Time.now.utc - seconds)
|
|
113
117
|
FileUtils.rm_rf(dir)
|
|
@@ -16,6 +16,8 @@ module CarrierWave
|
|
|
16
16
|
# [:fog_authenticated_url_expiration] (optional) time (in seconds) that authenticated urls
|
|
17
17
|
# will be valid, when fog_public is false and provider is AWS or Google, defaults to 600
|
|
18
18
|
# [:fog_use_ssl_for_aws] (optional) #public_url will use https for the AWS generated URL]
|
|
19
|
+
# [:fog_aws_accelerate] (optional) #public_url will use s3-accelerate subdomain
|
|
20
|
+
# instead of s3, defaults to false
|
|
19
21
|
#
|
|
20
22
|
#
|
|
21
23
|
# AWS credentials contain the following keys:
|
|
@@ -58,6 +60,14 @@ module CarrierWave
|
|
|
58
60
|
def connection_cache
|
|
59
61
|
@connection_cache ||= {}
|
|
60
62
|
end
|
|
63
|
+
|
|
64
|
+
def eager_load
|
|
65
|
+
# see #1198. This will hopefully no longer be necessary in future release of fog
|
|
66
|
+
fog_credentials = CarrierWave::Uploader::Base.fog_credentials
|
|
67
|
+
if fog_credentials.present?
|
|
68
|
+
CarrierWave::Storage::Fog.connection_cache[fog_credentials] ||= ::Fog::Storage.new(fog_credentials)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
61
71
|
end
|
|
62
72
|
|
|
63
73
|
##
|
|
@@ -136,8 +146,8 @@ module CarrierWave
|
|
|
136
146
|
:key => uploader.fog_directory,
|
|
137
147
|
:public => uploader.fog_public
|
|
138
148
|
).files.all(:prefix => uploader.cache_dir).each do |file|
|
|
139
|
-
# generate_cache_id returns key formated TIMEINT-PID-COUNTER-RND
|
|
140
|
-
time = file.key.scan(/(\d+)-\d+-\d
|
|
149
|
+
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
|
|
150
|
+
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
|
|
141
151
|
time = Time.at(*time)
|
|
142
152
|
file.destroy if time < (Time.now.utc - seconds)
|
|
143
153
|
end
|
|
@@ -175,7 +185,7 @@ module CarrierWave
|
|
|
175
185
|
|
|
176
186
|
##
|
|
177
187
|
# Return a temporary authenticated url to a private file, if available
|
|
178
|
-
# Only supported for AWS, Rackspace and
|
|
188
|
+
# Only supported for AWS, Rackspace, Google, AzureRM and Aliyun providers
|
|
179
189
|
#
|
|
180
190
|
# === Returns
|
|
181
191
|
#
|
|
@@ -184,18 +194,25 @@ module CarrierWave
|
|
|
184
194
|
# [NilClass] no authenticated url available
|
|
185
195
|
#
|
|
186
196
|
def authenticated_url(options = {})
|
|
187
|
-
if ['AWS', 'Google', 'Rackspace', 'OpenStack'].include?(@uploader.fog_credentials[:provider])
|
|
197
|
+
if ['AWS', 'Google', 'Rackspace', 'OpenStack', 'AzureRM', 'Aliyun'].include?(@uploader.fog_credentials[:provider])
|
|
188
198
|
# avoid a get by using local references
|
|
189
199
|
local_directory = connection.directories.new(:key => @uploader.fog_directory)
|
|
190
200
|
local_file = local_directory.files.new(:key => path)
|
|
191
|
-
expire_at = ::Fog::Time.now + @uploader.fog_authenticated_url_expiration
|
|
201
|
+
expire_at = options[:expire_at] || ::Fog::Time.now + @uploader.fog_authenticated_url_expiration
|
|
192
202
|
case @uploader.fog_credentials[:provider]
|
|
193
|
-
when 'AWS'
|
|
194
|
-
|
|
195
|
-
|
|
203
|
+
when 'AWS', 'Google'
|
|
204
|
+
# Older versions of fog-google do not support options as a parameter
|
|
205
|
+
if url_options_supported?(local_file)
|
|
206
|
+
local_file.url(expire_at, options)
|
|
207
|
+
else
|
|
208
|
+
warn "Options hash not supported in #{local_file.class}. You may need to upgrade your Fog provider."
|
|
209
|
+
local_file.url(expire_at)
|
|
210
|
+
end
|
|
211
|
+
when 'Rackspace', 'OpenStack'
|
|
196
212
|
connection.get_object_https_url(@uploader.fog_directory, path, expire_at, options)
|
|
197
|
-
when '
|
|
198
|
-
|
|
213
|
+
when 'Aliyun'
|
|
214
|
+
expire_at = expire_at - Time.now
|
|
215
|
+
local_file.url(expire_at)
|
|
199
216
|
else
|
|
200
217
|
local_file.url(expire_at)
|
|
201
218
|
end
|
|
@@ -210,7 +227,7 @@ module CarrierWave
|
|
|
210
227
|
# [String] value of content-type
|
|
211
228
|
#
|
|
212
229
|
def content_type
|
|
213
|
-
@content_type ||
|
|
230
|
+
@content_type || file.try(:content_type)
|
|
214
231
|
end
|
|
215
232
|
|
|
216
233
|
##
|
|
@@ -233,7 +250,9 @@ module CarrierWave
|
|
|
233
250
|
#
|
|
234
251
|
def delete
|
|
235
252
|
# avoid a get by just using local reference
|
|
236
|
-
directory.files.new(:key => path).destroy
|
|
253
|
+
directory.files.new(:key => path).destroy.tap do |result|
|
|
254
|
+
@file = nil if result
|
|
255
|
+
end
|
|
237
256
|
end
|
|
238
257
|
|
|
239
258
|
##
|
|
@@ -264,7 +283,7 @@ module CarrierWave
|
|
|
264
283
|
end
|
|
265
284
|
|
|
266
285
|
def initialize(uploader, base, path)
|
|
267
|
-
@uploader, @base, @path = uploader, base, path
|
|
286
|
+
@uploader, @base, @path, @content_type = uploader, base, path, nil
|
|
268
287
|
end
|
|
269
288
|
|
|
270
289
|
##
|
|
@@ -274,6 +293,16 @@ module CarrierWave
|
|
|
274
293
|
#
|
|
275
294
|
# [String] contents of file
|
|
276
295
|
def read
|
|
296
|
+
file_body = file.body
|
|
297
|
+
|
|
298
|
+
return if file_body.nil?
|
|
299
|
+
return file_body unless file_body.is_a?(::File)
|
|
300
|
+
|
|
301
|
+
# Fog::Storage::XXX::File#body could return the source file which was upoloaded to the remote server.
|
|
302
|
+
read_source_file(file_body) if ::File.exist?(file_body.path)
|
|
303
|
+
|
|
304
|
+
# If the source file doesn't exist, the remote content is read
|
|
305
|
+
@file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
277
306
|
file.body
|
|
278
307
|
end
|
|
279
308
|
|
|
@@ -295,7 +324,7 @@ module CarrierWave
|
|
|
295
324
|
#
|
|
296
325
|
# [Boolean] true if file exists or false
|
|
297
326
|
def exists?
|
|
298
|
-
!!
|
|
327
|
+
!!file
|
|
299
328
|
end
|
|
300
329
|
|
|
301
330
|
##
|
|
@@ -311,7 +340,7 @@ module CarrierWave
|
|
|
311
340
|
fog_file = new_file.to_file
|
|
312
341
|
@content_type ||= new_file.content_type
|
|
313
342
|
@file = directory.files.create({
|
|
314
|
-
:body =>
|
|
343
|
+
:body => fog_file ? fog_file : new_file.read,
|
|
315
344
|
:content_type => @content_type,
|
|
316
345
|
:key => path,
|
|
317
346
|
:public => @uploader.fog_public
|
|
@@ -340,16 +369,21 @@ module CarrierWave
|
|
|
340
369
|
end
|
|
341
370
|
else
|
|
342
371
|
# AWS/Google optimized for speed over correctness
|
|
343
|
-
case
|
|
372
|
+
case fog_provider
|
|
344
373
|
when 'AWS'
|
|
345
374
|
# check if some endpoint is set in fog_credentials
|
|
346
375
|
if @uploader.fog_credentials.has_key?(:endpoint)
|
|
347
376
|
"#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}"
|
|
348
377
|
else
|
|
349
378
|
protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http"
|
|
379
|
+
|
|
380
|
+
subdomain_regex = /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
|
381
|
+
valid_subdomain = @uploader.fog_directory.to_s =~ subdomain_regex && !(protocol == 'https' && @uploader.fog_directory =~ /\./)
|
|
382
|
+
|
|
350
383
|
# if directory is a valid subdomain, use that style for access
|
|
351
|
-
if
|
|
352
|
-
|
|
384
|
+
if valid_subdomain
|
|
385
|
+
s3_subdomain = @uploader.fog_aws_accelerate ? "s3-accelerate" : "s3"
|
|
386
|
+
"#{protocol}://#{@uploader.fog_directory}.#{s3_subdomain}.amazonaws.com/#{encoded_path}"
|
|
353
387
|
else
|
|
354
388
|
# directory is not a valid subdomain, so use path style for access
|
|
355
389
|
"#{protocol}://s3.amazonaws.com/#{@uploader.fog_directory}/#{encoded_path}"
|
|
@@ -453,7 +487,31 @@ module CarrierWave
|
|
|
453
487
|
end
|
|
454
488
|
|
|
455
489
|
def acl_header
|
|
456
|
-
|
|
490
|
+
if fog_provider == 'AWS'
|
|
491
|
+
{ 'x-amz-acl' => @uploader.fog_public ? 'public-read' : 'private' }
|
|
492
|
+
else
|
|
493
|
+
{}
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
def fog_provider
|
|
498
|
+
@uploader.fog_credentials[:provider].to_s
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
def read_source_file(file_body)
|
|
502
|
+
return unless ::File.exist?(file_body.path)
|
|
503
|
+
|
|
504
|
+
begin
|
|
505
|
+
file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's already closed
|
|
506
|
+
file_body.read
|
|
507
|
+
ensure
|
|
508
|
+
file_body.close
|
|
509
|
+
end
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
def url_options_supported?(local_file)
|
|
513
|
+
parameters = local_file.method(:url).parameters
|
|
514
|
+
parameters.count == 2 && parameters[1].include?(:options)
|
|
457
515
|
end
|
|
458
516
|
end
|
|
459
517
|
|
data/lib/carrierwave/storage.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
|
|
1
3
|
module CarrierWave
|
|
2
4
|
|
|
3
5
|
class FormNotMultipart < UploadError
|
|
@@ -23,9 +25,9 @@ module CarrierWave
|
|
|
23
25
|
#
|
|
24
26
|
def self.generate_cache_id
|
|
25
27
|
[Time.now.utc.to_i,
|
|
26
|
-
|
|
27
|
-
'%04d' % (CarrierWave::CacheCounter.increment %
|
|
28
|
-
'%04d' %
|
|
28
|
+
SecureRandom.random_number(1_000_000_000_000_000),
|
|
29
|
+
'%04d' % (CarrierWave::CacheCounter.increment % 10_000),
|
|
30
|
+
'%04d' % SecureRandom.random_number(10_000)
|
|
29
31
|
].map(&:to_s).join('-')
|
|
30
32
|
end
|
|
31
33
|
|
|
@@ -36,6 +38,16 @@ module CarrierWave
|
|
|
36
38
|
include CarrierWave::Uploader::Callbacks
|
|
37
39
|
include CarrierWave::Uploader::Configuration
|
|
38
40
|
|
|
41
|
+
included do
|
|
42
|
+
prepend Module.new {
|
|
43
|
+
def initialize(*)
|
|
44
|
+
super
|
|
45
|
+
@staged = false
|
|
46
|
+
end
|
|
47
|
+
}
|
|
48
|
+
attr_accessor :staged
|
|
49
|
+
end
|
|
50
|
+
|
|
39
51
|
module ClassMethods
|
|
40
52
|
|
|
41
53
|
##
|
|
@@ -52,7 +64,7 @@ module CarrierWave
|
|
|
52
64
|
# It's recommended that you keep cache files in one place only.
|
|
53
65
|
#
|
|
54
66
|
def clean_cached_files!(seconds=60*60*24)
|
|
55
|
-
cache_storage.new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
|
|
67
|
+
(cache_storage || storage).new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
|
|
56
68
|
end
|
|
57
69
|
end
|
|
58
70
|
|
|
@@ -78,14 +90,8 @@ module CarrierWave
|
|
|
78
90
|
end
|
|
79
91
|
|
|
80
92
|
def sanitized_file
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
sanitized = CarrierWave::Storage::Fog.new(self).retrieve!(File.basename(_content.path))
|
|
84
|
-
else
|
|
85
|
-
sanitized = SanitizedFile.new :tempfile => StringIO.new(_content),
|
|
86
|
-
:filename => File.basename(path), :content_type => file.content_type
|
|
87
|
-
end
|
|
88
|
-
sanitized
|
|
93
|
+
ActiveSupport::Deprecation.warn('#sanitized_file is deprecated, use #file instead.')
|
|
94
|
+
file
|
|
89
95
|
end
|
|
90
96
|
|
|
91
97
|
##
|
|
@@ -96,7 +102,7 @@ module CarrierWave
|
|
|
96
102
|
# [String] a cache name, in the format TIMEINT-PID-COUNTER-RND/filename.txt
|
|
97
103
|
#
|
|
98
104
|
def cache_name
|
|
99
|
-
File.join(cache_id, full_original_filename) if cache_id
|
|
105
|
+
File.join(cache_id, full_original_filename) if cache_id && original_filename
|
|
100
106
|
end
|
|
101
107
|
|
|
102
108
|
##
|
|
@@ -115,7 +121,7 @@ module CarrierWave
|
|
|
115
121
|
#
|
|
116
122
|
# [CarrierWave::FormNotMultipart] if the assigned parameter is a string
|
|
117
123
|
#
|
|
118
|
-
def cache!(new_file =
|
|
124
|
+
def cache!(new_file = file)
|
|
119
125
|
new_file = CarrierWave::SanitizedFile.new(new_file)
|
|
120
126
|
return if new_file.empty?
|
|
121
127
|
|
|
@@ -123,6 +129,7 @@ module CarrierWave
|
|
|
123
129
|
|
|
124
130
|
self.cache_id = CarrierWave.generate_cache_id unless cache_id
|
|
125
131
|
|
|
132
|
+
@staged = true
|
|
126
133
|
@filename = new_file.filename
|
|
127
134
|
self.original_filename = new_file.filename
|
|
128
135
|
|
|
@@ -156,6 +163,7 @@ module CarrierWave
|
|
|
156
163
|
def retrieve_from_cache!(cache_name)
|
|
157
164
|
with_callbacks(:retrieve_from_cache, cache_name) do
|
|
158
165
|
self.cache_id, self.original_filename = cache_name.to_s.split('/', 2)
|
|
166
|
+
@staged = true
|
|
159
167
|
@filename = original_filename
|
|
160
168
|
@file = cache_storage.retrieve_from_cache!(full_filename(original_filename))
|
|
161
169
|
end
|
|
@@ -190,7 +198,7 @@ module CarrierWave
|
|
|
190
198
|
def cache_id=(cache_id)
|
|
191
199
|
# Earlier version used 3 part cache_id. Thus we should allow for
|
|
192
200
|
# the cache_id to have both 3 part and 4 part formats.
|
|
193
|
-
raise CarrierWave::InvalidParameter, "invalid cache id" unless cache_id =~ /\A[\d]+\-[\d]+(\-[\d]{4})?\-[\d]{4}\z/
|
|
201
|
+
raise CarrierWave::InvalidParameter, "invalid cache id" unless cache_id =~ /\A(-)?[\d]+\-[\d]+(\-[\d]{4})?\-[\d]{4}\z/
|
|
194
202
|
@cache_id = cache_id
|
|
195
203
|
end
|
|
196
204
|
|
|
@@ -200,7 +208,7 @@ module CarrierWave
|
|
|
200
208
|
end
|
|
201
209
|
|
|
202
210
|
def cache_storage
|
|
203
|
-
@cache_storage ||= self.class.cache_storage.new(self)
|
|
211
|
+
@cache_storage ||= (self.class.cache_storage || self.class.storage).new(self)
|
|
204
212
|
end
|
|
205
213
|
end # Cache
|
|
206
214
|
end # Uploader
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'carrierwave/downloader/base'
|
|
2
|
+
|
|
1
3
|
module CarrierWave
|
|
2
4
|
|
|
3
5
|
module Uploader
|
|
@@ -21,15 +23,17 @@ module CarrierWave
|
|
|
21
23
|
add_config :move_to_cache
|
|
22
24
|
add_config :move_to_store
|
|
23
25
|
add_config :remove_previously_stored_files_after_update
|
|
26
|
+
add_config :downloader
|
|
24
27
|
|
|
25
28
|
# fog
|
|
26
|
-
|
|
29
|
+
add_deprecated_config :fog_provider
|
|
27
30
|
add_config :fog_attributes
|
|
28
31
|
add_config :fog_credentials
|
|
29
32
|
add_config :fog_directory
|
|
30
33
|
add_config :fog_public
|
|
31
34
|
add_config :fog_authenticated_url_expiration
|
|
32
35
|
add_config :fog_use_ssl_for_aws
|
|
36
|
+
add_config :fog_aws_accelerate
|
|
33
37
|
|
|
34
38
|
# Mounting
|
|
35
39
|
add_config :ignore_integrity_errors
|
|
@@ -106,8 +110,8 @@ module CarrierWave
|
|
|
106
110
|
# cache_storage CarrierWave::Storage::File
|
|
107
111
|
# cache_storage MyCustomStorageEngine
|
|
108
112
|
#
|
|
109
|
-
def cache_storage(storage =
|
|
110
|
-
|
|
113
|
+
def cache_storage(storage = false)
|
|
114
|
+
unless storage == false
|
|
111
115
|
self._cache_storage = storage.is_a?(Symbol) ? eval(storage_engines[storage]) : storage
|
|
112
116
|
end
|
|
113
117
|
_cache_storage
|
|
@@ -116,29 +120,21 @@ module CarrierWave
|
|
|
116
120
|
|
|
117
121
|
def add_config(name)
|
|
118
122
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
119
|
-
|
|
120
|
-
# see #1198. This will hopefully no longer be necessary after fog 2.0
|
|
121
|
-
require self.fog_provider
|
|
122
|
-
require 'carrierwave/storage/fog'
|
|
123
|
-
Fog::Storage.new(fog_credentials) if fog_credentials.present?
|
|
124
|
-
end
|
|
123
|
+
@#{name} = nil
|
|
125
124
|
|
|
126
125
|
def self.#{name}(value=nil)
|
|
127
126
|
@#{name} = value if value
|
|
128
|
-
eager_load_fog(value) if value && '#{name}' == 'fog_credentials'
|
|
129
127
|
return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
|
|
130
128
|
name = superclass.#{name}
|
|
131
|
-
return nil if name.nil? && !instance_variable_defined?(
|
|
129
|
+
return nil if name.nil? && !instance_variable_defined?(:@#{name})
|
|
132
130
|
@#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
|
|
133
131
|
end
|
|
134
132
|
|
|
135
133
|
def self.#{name}=(value)
|
|
136
|
-
eager_load_fog(value) if '#{name}' == 'fog_credentials' && value.present?
|
|
137
134
|
@#{name} = value
|
|
138
135
|
end
|
|
139
136
|
|
|
140
137
|
def #{name}=(value)
|
|
141
|
-
self.class.eager_load_fog(value) if '#{name}' == 'fog_credentials' && value.present?
|
|
142
138
|
@#{name} = value
|
|
143
139
|
end
|
|
144
140
|
|
|
@@ -154,6 +150,26 @@ module CarrierWave
|
|
|
154
150
|
RUBY
|
|
155
151
|
end
|
|
156
152
|
|
|
153
|
+
def add_deprecated_config(name)
|
|
154
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
155
|
+
def self.#{name}(value=nil)
|
|
156
|
+
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def self.#{name}=(value)
|
|
160
|
+
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def #{name}=(value)
|
|
164
|
+
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def #{name}
|
|
168
|
+
ActiveSupport::Deprecation.warn "##{name} is deprecated and has no effect"
|
|
169
|
+
end
|
|
170
|
+
RUBY
|
|
171
|
+
end
|
|
172
|
+
|
|
157
173
|
def configure
|
|
158
174
|
yield self
|
|
159
175
|
end
|
|
@@ -170,19 +186,20 @@ module CarrierWave
|
|
|
170
186
|
:fog => "CarrierWave::Storage::Fog"
|
|
171
187
|
}
|
|
172
188
|
config.storage = :file
|
|
173
|
-
config.cache_storage =
|
|
174
|
-
config.fog_provider = 'fog'
|
|
189
|
+
config.cache_storage = nil
|
|
175
190
|
config.fog_attributes = {}
|
|
176
191
|
config.fog_credentials = {}
|
|
177
192
|
config.fog_public = true
|
|
178
193
|
config.fog_authenticated_url_expiration = 600
|
|
179
194
|
config.fog_use_ssl_for_aws = true
|
|
195
|
+
config.fog_aws_accelerate = false
|
|
180
196
|
config.store_dir = 'uploads'
|
|
181
197
|
config.cache_dir = 'uploads/tmp'
|
|
182
198
|
config.delete_tmp_file_after_storage = true
|
|
183
199
|
config.move_to_cache = false
|
|
184
200
|
config.move_to_store = false
|
|
185
201
|
config.remove_previously_stored_files_after_update = true
|
|
202
|
+
config.downloader = CarrierWave::Downloader::Base
|
|
186
203
|
config.ignore_integrity_errors = true
|
|
187
204
|
config.ignore_processing_errors = true
|
|
188
205
|
config.ignore_download_errors = true
|
|
@@ -200,4 +217,3 @@ module CarrierWave
|
|
|
200
217
|
end
|
|
201
218
|
end
|
|
202
219
|
end
|
|
203
|
-
|
|
@@ -35,7 +35,7 @@ module CarrierWave
|
|
|
35
35
|
def check_content_type_whitelist!(new_file)
|
|
36
36
|
content_type = new_file.content_type
|
|
37
37
|
if content_type_whitelist && !whitelisted_content_type?(content_type)
|
|
38
|
-
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: content_type)
|
|
38
|
+
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.content_type_whitelist_error", content_type: content_type, allowed_types: Array(content_type_whitelist).join(", "))
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'open-uri'
|
|
2
|
-
|
|
3
1
|
module CarrierWave
|
|
4
2
|
module Uploader
|
|
5
3
|
module Download
|
|
@@ -9,59 +7,8 @@ module CarrierWave
|
|
|
9
7
|
include CarrierWave::Uploader::Configuration
|
|
10
8
|
include CarrierWave::Uploader::Cache
|
|
11
9
|
|
|
12
|
-
class RemoteFile
|
|
13
|
-
def initialize(uri, remote_headers = {})
|
|
14
|
-
@uri = uri
|
|
15
|
-
@remote_headers = remote_headers
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def original_filename
|
|
19
|
-
filename = filename_from_header || File.basename(file.base_uri.path)
|
|
20
|
-
mime_type = MIME::Types[file.content_type].first
|
|
21
|
-
unless File.extname(filename).present? || mime_type.blank?
|
|
22
|
-
filename = "#{filename}.#{mime_type.extensions.first}"
|
|
23
|
-
end
|
|
24
|
-
filename
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def respond_to?(*args)
|
|
28
|
-
super or file.respond_to?(*args)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def http?
|
|
32
|
-
@uri.scheme =~ /^https?$/
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
private
|
|
36
|
-
|
|
37
|
-
def file
|
|
38
|
-
if @file.blank?
|
|
39
|
-
headers = @remote_headers.
|
|
40
|
-
reverse_merge('User-Agent' => "CarrierWave/#{CarrierWave::VERSION}")
|
|
41
|
-
|
|
42
|
-
@file = Kernel.open(@uri.to_s, headers)
|
|
43
|
-
@file = @file.is_a?(String) ? StringIO.new(@file) : @file
|
|
44
|
-
end
|
|
45
|
-
@file
|
|
46
|
-
|
|
47
|
-
rescue StandardError => e
|
|
48
|
-
raise CarrierWave::DownloadError, "could not download file: #{e.message}"
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def filename_from_header
|
|
52
|
-
if file.meta.include? 'content-disposition'
|
|
53
|
-
match = file.meta['content-disposition'].match(/filename="?([^"]+)/)
|
|
54
|
-
return match[1] unless match.nil? || match[1].empty?
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def method_missing(*args, &block)
|
|
59
|
-
file.send(*args, &block)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
10
|
##
|
|
64
|
-
# Caches the file by downloading it from the given URL.
|
|
11
|
+
# Caches the file by downloading it from the given URL, using downloader.
|
|
65
12
|
#
|
|
66
13
|
# === Parameters
|
|
67
14
|
#
|
|
@@ -69,29 +16,9 @@ module CarrierWave
|
|
|
69
16
|
# [remote_headers (Hash)] Request headers
|
|
70
17
|
#
|
|
71
18
|
def download!(uri, remote_headers = {})
|
|
72
|
-
|
|
73
|
-
file = RemoteFile.new(processed_uri, remote_headers)
|
|
74
|
-
raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
|
|
19
|
+
file = downloader.new(self).download(uri, remote_headers)
|
|
75
20
|
cache!(file)
|
|
76
21
|
end
|
|
77
|
-
|
|
78
|
-
##
|
|
79
|
-
# Processes the given URL by parsing and escaping it. Public to allow overriding.
|
|
80
|
-
#
|
|
81
|
-
# === Parameters
|
|
82
|
-
#
|
|
83
|
-
# [url (String)] The URL where the remote file is stored
|
|
84
|
-
#
|
|
85
|
-
def process_uri(uri)
|
|
86
|
-
URI.parse(uri)
|
|
87
|
-
rescue URI::InvalidURIError
|
|
88
|
-
uri_parts = uri.split('?')
|
|
89
|
-
# regexp from Ruby's URI::Parser#regexp[:UNSAFE], with [] specifically removed
|
|
90
|
-
encoded_uri = URI.encode(uri_parts.shift, /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,]/)
|
|
91
|
-
encoded_uri << '?' << URI.encode(uri_parts.join('?')) if uri_parts.any?
|
|
92
|
-
URI.parse(encoded_uri) rescue raise CarrierWave::DownloadError, "couldn't parse URL"
|
|
93
|
-
end
|
|
94
|
-
|
|
95
22
|
end # Download
|
|
96
23
|
end # Uploader
|
|
97
24
|
end # CarrierWave
|