carrierwave 0.10.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of carrierwave might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +248 -116
- data/lib/carrierwave/compatibility/paperclip.rb +0 -2
- data/lib/carrierwave/error.rb +1 -0
- data/lib/carrierwave/locale/en.yml +7 -4
- data/lib/carrierwave/mount.rb +219 -176
- data/lib/carrierwave/mounter.rb +165 -0
- data/lib/carrierwave/orm/activerecord.rb +50 -21
- data/lib/carrierwave/processing/mini_magick.rb +82 -15
- data/lib/carrierwave/processing/rmagick.rb +47 -4
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/sanitized_file.rb +38 -18
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +65 -2
- data/lib/carrierwave/storage/fog.rb +106 -31
- data/lib/carrierwave/storage.rb +0 -7
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader/cache.rb +58 -30
- data/lib/carrierwave/uploader/callbacks.rb +0 -2
- data/lib/carrierwave/uploader/configuration.rb +51 -8
- data/lib/carrierwave/uploader/content_type_blacklist.rb +48 -0
- data/lib/carrierwave/uploader/content_type_whitelist.rb +48 -0
- data/lib/carrierwave/uploader/default_url.rb +3 -5
- data/lib/carrierwave/uploader/download.rb +15 -8
- data/lib/carrierwave/uploader/extension_blacklist.rb +14 -10
- data/lib/carrierwave/uploader/extension_whitelist.rb +13 -10
- data/lib/carrierwave/uploader/file_size.rb +43 -0
- data/lib/carrierwave/uploader/magic_mime_blacklist.rb +94 -0
- data/lib/carrierwave/uploader/magic_mime_whitelist.rb +94 -0
- data/lib/carrierwave/uploader/mountable.rb +7 -8
- data/lib/carrierwave/uploader/processing.rb +10 -10
- data/lib/carrierwave/uploader/proxy.rb +5 -7
- data/lib/carrierwave/uploader/remove.rb +0 -2
- data/lib/carrierwave/uploader/serialization.rb +1 -3
- data/lib/carrierwave/uploader/store.rb +5 -23
- data/lib/carrierwave/uploader/url.rb +3 -5
- data/lib/carrierwave/uploader/versions.rb +75 -82
- data/lib/carrierwave/uploader.rb +6 -2
- data/lib/carrierwave/utilities/uri.rb +5 -6
- data/lib/carrierwave/utilities.rb +0 -3
- data/lib/carrierwave/validations/active_model.rb +3 -5
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +12 -10
- data/lib/generators/templates/uploader.rb +4 -6
- metadata +37 -61
- data/lib/carrierwave/locale/cs.yml +0 -11
- data/lib/carrierwave/locale/de.yml +0 -11
- data/lib/carrierwave/locale/el.yml +0 -11
- data/lib/carrierwave/locale/es.yml +0 -11
- data/lib/carrierwave/locale/fr.yml +0 -11
- data/lib/carrierwave/locale/ja.yml +0 -11
- data/lib/carrierwave/locale/nb.yml +0 -11
- data/lib/carrierwave/locale/nl.yml +0 -11
- data/lib/carrierwave/locale/pl.yml +0 -11
- data/lib/carrierwave/locale/pt-BR.yml +0 -11
- data/lib/carrierwave/locale/pt-PT.yml +0 -11
- data/lib/carrierwave/locale/ru.yml +0 -11
- data/lib/carrierwave/locale/sk.yml +0 -11
- data/lib/carrierwave/locale/tr.yml +0 -11
- data/lib/carrierwave/processing/mime_types.rb +0 -74
- data/lib/carrierwave/utilities/deprecation.rb +0 -18
@@ -1,7 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "fog"
|
4
|
-
|
5
1
|
module CarrierWave
|
6
2
|
module Storage
|
7
3
|
|
@@ -20,6 +16,8 @@ module CarrierWave
|
|
20
16
|
# [:fog_authenticated_url_expiration] (optional) time (in seconds) that authenticated urls
|
21
17
|
# will be valid, when fog_public is false and provider is AWS or Google, defaults to 600
|
22
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
|
23
21
|
#
|
24
22
|
#
|
25
23
|
# AWS credentials contain the following keys:
|
@@ -27,7 +25,7 @@ module CarrierWave
|
|
27
25
|
# [:aws_access_key_id]
|
28
26
|
# [:aws_secret_access_key]
|
29
27
|
# [:region] (optional) defaults to 'us-east-1'
|
30
|
-
# :region should be one of ['eu-west-1', 'us-east-1', 'ap-southeast-1', 'us-west-1', 'ap-northeast-1']
|
28
|
+
# :region should be one of ['eu-west-1', 'us-east-1', 'ap-southeast-1', 'us-west-1', 'ap-northeast-1', 'eu-central-1']
|
31
29
|
#
|
32
30
|
#
|
33
31
|
# Google credentials contain the following keys:
|
@@ -96,6 +94,57 @@ module CarrierWave
|
|
96
94
|
CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path(identifier))
|
97
95
|
end
|
98
96
|
|
97
|
+
##
|
98
|
+
# Stores given file to cache directory.
|
99
|
+
#
|
100
|
+
# === Parameters
|
101
|
+
#
|
102
|
+
# [new_file (File, IOString, Tempfile)] any kind of file object
|
103
|
+
#
|
104
|
+
# === Returns
|
105
|
+
#
|
106
|
+
# [CarrierWave::SanitizedFile] a sanitized file
|
107
|
+
#
|
108
|
+
def cache!(new_file)
|
109
|
+
f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.cache_path)
|
110
|
+
f.store(new_file)
|
111
|
+
f
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# Retrieves the file with the given cache_name from the cache.
|
116
|
+
#
|
117
|
+
# === Parameters
|
118
|
+
#
|
119
|
+
# [cache_name (String)] uniquely identifies a cache file
|
120
|
+
#
|
121
|
+
# === Raises
|
122
|
+
#
|
123
|
+
# [CarrierWave::InvalidParameter] if the cache_name is incorrectly formatted.
|
124
|
+
#
|
125
|
+
def retrieve_from_cache!(identifier)
|
126
|
+
CarrierWave::Storage::Fog::File.new(uploader, self, uploader.cache_path(identifier))
|
127
|
+
end
|
128
|
+
|
129
|
+
##
|
130
|
+
# Deletes a cache dir
|
131
|
+
#
|
132
|
+
def delete_dir!(path)
|
133
|
+
# do nothing, because there's no such things as 'empty directory'
|
134
|
+
end
|
135
|
+
|
136
|
+
def clean_cache!(seconds)
|
137
|
+
connection.directories.new(
|
138
|
+
:key => uploader.fog_directory,
|
139
|
+
:public => uploader.fog_public
|
140
|
+
).files.all(:prefix => uploader.cache_dir).each do |file|
|
141
|
+
# generate_cache_id returns key formated TIMEINT-PID-COUNTER-RND
|
142
|
+
time = file.key.scan(/(\d+)-\d+-\d+-\d+/).first.map { |t| t.to_i }
|
143
|
+
time = Time.at(*time)
|
144
|
+
file.destroy if time < (Time.now.utc - seconds)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
99
148
|
def connection
|
100
149
|
@connection ||= begin
|
101
150
|
options = credentials = uploader.fog_credentials
|
@@ -141,15 +190,17 @@ module CarrierWave
|
|
141
190
|
# avoid a get by using local references
|
142
191
|
local_directory = connection.directories.new(:key => @uploader.fog_directory)
|
143
192
|
local_file = local_directory.files.new(:key => path)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
193
|
+
expire_at = ::Fog::Time.now + @uploader.fog_authenticated_url_expiration
|
194
|
+
case @uploader.fog_credentials[:provider]
|
195
|
+
when 'AWS'
|
196
|
+
local_file.url(expire_at, options)
|
197
|
+
when 'Rackspace'
|
198
|
+
connection.get_object_https_url(@uploader.fog_directory, path, expire_at, options)
|
199
|
+
when 'OpenStack'
|
200
|
+
connection.get_object_https_url(@uploader.fog_directory, path, expire_at)
|
201
|
+
else
|
202
|
+
local_file.url(expire_at)
|
150
203
|
end
|
151
|
-
else
|
152
|
-
nil
|
153
204
|
end
|
154
205
|
end
|
155
206
|
|
@@ -161,7 +212,7 @@ module CarrierWave
|
|
161
212
|
# [String] value of content-type
|
162
213
|
#
|
163
214
|
def content_type
|
164
|
-
@content_type || file.content_type
|
215
|
+
@content_type || !file.nil? && file.content_type
|
165
216
|
end
|
166
217
|
|
167
218
|
##
|
@@ -236,7 +287,7 @@ module CarrierWave
|
|
236
287
|
# [Integer] size of file body
|
237
288
|
#
|
238
289
|
def size
|
239
|
-
file.content_length
|
290
|
+
file.nil? ? 0 : file.content_length
|
240
291
|
end
|
241
292
|
|
242
293
|
##
|
@@ -246,7 +297,7 @@ module CarrierWave
|
|
246
297
|
#
|
247
298
|
# [Boolean] true if file exists or false
|
248
299
|
def exists?
|
249
|
-
!!
|
300
|
+
!!file
|
250
301
|
end
|
251
302
|
|
252
303
|
##
|
@@ -256,15 +307,19 @@ module CarrierWave
|
|
256
307
|
#
|
257
308
|
# [Boolean] true on success or raises error
|
258
309
|
def store(new_file)
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
310
|
+
if new_file.is_a?(self.class)
|
311
|
+
new_file.copy_to(path)
|
312
|
+
else
|
313
|
+
fog_file = new_file.to_file
|
314
|
+
@content_type ||= new_file.content_type
|
315
|
+
@file = directory.files.create({
|
316
|
+
:body => (fog_file ? fog_file : new_file).read,
|
317
|
+
:content_type => @content_type,
|
318
|
+
:key => path,
|
319
|
+
:public => @uploader.fog_public
|
320
|
+
}.merge(@uploader.fog_attributes))
|
321
|
+
fog_file.close if fog_file && !fog_file.closed?
|
322
|
+
end
|
268
323
|
true
|
269
324
|
end
|
270
325
|
|
@@ -287,7 +342,7 @@ module CarrierWave
|
|
287
342
|
end
|
288
343
|
else
|
289
344
|
# AWS/Google optimized for speed over correctness
|
290
|
-
case @uploader.fog_credentials[:provider]
|
345
|
+
case @uploader.fog_credentials[:provider].to_s
|
291
346
|
when 'AWS'
|
292
347
|
# check if some endpoint is set in fog_credentials
|
293
348
|
if @uploader.fog_credentials.has_key?(:endpoint)
|
@@ -296,14 +351,16 @@ module CarrierWave
|
|
296
351
|
protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http"
|
297
352
|
# if directory is a valid subdomain, use that style for access
|
298
353
|
if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
299
|
-
|
354
|
+
s3_subdomain = @uploader.fog_aws_accelerate ? "s3-accelerate" : "s3"
|
355
|
+
"#{protocol}://#{@uploader.fog_directory}.#{s3_subdomain}.amazonaws.com/#{encoded_path}"
|
300
356
|
else
|
301
357
|
# directory is not a valid subdomain, so use path style for access
|
302
358
|
"#{protocol}://s3.amazonaws.com/#{@uploader.fog_directory}/#{encoded_path}"
|
303
359
|
end
|
304
360
|
end
|
305
361
|
when 'Google'
|
306
|
-
|
362
|
+
# https://cloud.google.com/storage/docs/access-public-data
|
363
|
+
"https://storage.googleapis.com/#{@uploader.fog_directory}/#{encoded_path}"
|
307
364
|
else
|
308
365
|
# avoid a get by just using local reference
|
309
366
|
directory.files.new(:key => path).public_url
|
@@ -338,9 +395,24 @@ module CarrierWave
|
|
338
395
|
# [NilClass] no file name available
|
339
396
|
#
|
340
397
|
def filename(options = {})
|
341
|
-
|
342
|
-
|
343
|
-
|
398
|
+
return unless file_url = url(options)
|
399
|
+
CGI.unescape(file_url.split('?').first).gsub(/.*\/(.*?$)/, '\1')
|
400
|
+
end
|
401
|
+
|
402
|
+
##
|
403
|
+
# Creates a copy of this file and returns it.
|
404
|
+
#
|
405
|
+
# === Parameters
|
406
|
+
#
|
407
|
+
# [new_path (String)] The path where the file should be copied to.
|
408
|
+
#
|
409
|
+
# === Returns
|
410
|
+
#
|
411
|
+
# @return [CarrierWave::Storage::Fog::File] the location where the file will be stored.
|
412
|
+
#
|
413
|
+
def copy_to(new_path)
|
414
|
+
connection.copy_object(@uploader.fog_directory, file.key, @uploader.fog_directory, new_path, acl_header)
|
415
|
+
CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path)
|
344
416
|
end
|
345
417
|
|
346
418
|
private
|
@@ -383,6 +455,9 @@ module CarrierWave
|
|
383
455
|
@file ||= directory.files.head(path)
|
384
456
|
end
|
385
457
|
|
458
|
+
def acl_header
|
459
|
+
{'x-amz-acl' => @uploader.fog_public ? 'public-read' : 'private'}
|
460
|
+
end
|
386
461
|
end
|
387
462
|
|
388
463
|
end # Fog
|
data/lib/carrierwave/storage.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Test
|
5
3
|
|
@@ -23,13 +21,16 @@ module CarrierWave
|
|
23
21
|
"expected #{@actual.inspect} to be identical to #{@expected.inspect}"
|
24
22
|
end
|
25
23
|
|
26
|
-
def
|
24
|
+
def failure_message_when_negated
|
27
25
|
"expected #{@actual.inspect} to not be identical to #{@expected.inspect}"
|
28
26
|
end
|
29
27
|
|
30
28
|
def description
|
31
29
|
"be identical to #{@expected.inspect}"
|
32
30
|
end
|
31
|
+
|
32
|
+
# RSpec 2 compatibility:
|
33
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
33
34
|
end
|
34
35
|
|
35
36
|
def be_identical_to(expected)
|
@@ -51,13 +52,16 @@ module CarrierWave
|
|
51
52
|
"expected #{@actual.current_path.inspect} to have permissions #{@expected.to_s(8)}, but they were #{(File.stat(@actual.path).mode & 0777).to_s(8)}"
|
52
53
|
end
|
53
54
|
|
54
|
-
def
|
55
|
+
def failure_message_when_negated
|
55
56
|
"expected #{@actual.current_path.inspect} not to have permissions #{@expected.to_s(8)}, but it did"
|
56
57
|
end
|
57
58
|
|
58
59
|
def description
|
59
60
|
"have permissions #{@expected.to_s(8)}"
|
60
61
|
end
|
62
|
+
|
63
|
+
# RSpec 2 compatibility:
|
64
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
61
65
|
end
|
62
66
|
|
63
67
|
def have_permissions(expected)
|
@@ -79,13 +83,16 @@ module CarrierWave
|
|
79
83
|
"expected #{File.dirname @actual.current_path.inspect} to have permissions #{@expected.to_s(8)}, but they were #{(File.stat(@actual.path).mode & 0777).to_s(8)}"
|
80
84
|
end
|
81
85
|
|
82
|
-
def
|
86
|
+
def failure_message_when_negated
|
83
87
|
"expected #{File.dirname @actual.current_path.inspect} not to have permissions #{@expected.to_s(8)}, but it did"
|
84
88
|
end
|
85
89
|
|
86
90
|
def description
|
87
91
|
"have permissions #{@expected.to_s(8)}"
|
88
92
|
end
|
93
|
+
|
94
|
+
# RSpec 2 compatibility:
|
95
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
89
96
|
end
|
90
97
|
|
91
98
|
def have_directory_permissions(expected)
|
@@ -110,13 +117,16 @@ module CarrierWave
|
|
110
117
|
"expected #{@actual.current_path.inspect} to be no larger than #{@width} by #{@height}, but it was #{@actual_width} by #{@actual_height}."
|
111
118
|
end
|
112
119
|
|
113
|
-
def
|
120
|
+
def failure_message_when_negated
|
114
121
|
"expected #{@actual.current_path.inspect} to be larger than #{@width} by #{@height}, but it wasn't."
|
115
122
|
end
|
116
123
|
|
117
124
|
def description
|
118
125
|
"be no larger than #{@width} by #{@height}"
|
119
126
|
end
|
127
|
+
|
128
|
+
# RSpec 2 compatibility:
|
129
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
120
130
|
end
|
121
131
|
|
122
132
|
def be_no_larger_than(width, height)
|
@@ -141,13 +151,16 @@ module CarrierWave
|
|
141
151
|
"expected #{@actual.current_path.inspect} to have an exact size of #{@width} by #{@height}, but it was #{@actual_width} by #{@actual_height}."
|
142
152
|
end
|
143
153
|
|
144
|
-
def
|
154
|
+
def failure_message_when_negated
|
145
155
|
"expected #{@actual.current_path.inspect} not to have an exact size of #{@width} by #{@height}, but it did."
|
146
156
|
end
|
147
157
|
|
148
158
|
def description
|
149
159
|
"have an exact size of #{@width} by #{@height}"
|
150
160
|
end
|
161
|
+
|
162
|
+
# RSpec 2 compatibility:
|
163
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
151
164
|
end
|
152
165
|
|
153
166
|
def have_dimensions(width, height)
|
@@ -171,13 +184,16 @@ module CarrierWave
|
|
171
184
|
"expected #{@actual.current_path.inspect} to have an exact size of #{@height}, but it was #{@actual_height}."
|
172
185
|
end
|
173
186
|
|
174
|
-
def
|
187
|
+
def failure_message_when_negated
|
175
188
|
"expected #{@actual.current_path.inspect} not to have an exact size of #{@height}, but it did."
|
176
189
|
end
|
177
190
|
|
178
191
|
def description
|
179
192
|
"have an exact height of #{@height}"
|
180
193
|
end
|
194
|
+
|
195
|
+
# RSpec 2 compatibility:
|
196
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
181
197
|
end
|
182
198
|
|
183
199
|
def have_height(height)
|
@@ -201,13 +217,16 @@ module CarrierWave
|
|
201
217
|
"expected #{@actual.current_path.inspect} to have an exact size of #{@width}, but it was #{@actual_width}."
|
202
218
|
end
|
203
219
|
|
204
|
-
def
|
220
|
+
def failure_message_when_negated
|
205
221
|
"expected #{@actual.current_path.inspect} not to have an exact size of #{@width}, but it did."
|
206
222
|
end
|
207
223
|
|
208
224
|
def description
|
209
225
|
"have an exact width of #{@width}"
|
210
226
|
end
|
227
|
+
|
228
|
+
# RSpec 2 compatibility:
|
229
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
211
230
|
end
|
212
231
|
|
213
232
|
def have_width(width)
|
@@ -231,13 +250,16 @@ module CarrierWave
|
|
231
250
|
"expected #{@actual.current_path.inspect} to be no wider than #{@width}, but it was #{@actual_width}."
|
232
251
|
end
|
233
252
|
|
234
|
-
def
|
253
|
+
def failure_message_when_negated
|
235
254
|
"expected #{@actual.current_path.inspect} not to be wider than #{@width}, but it is."
|
236
255
|
end
|
237
256
|
|
238
257
|
def description
|
239
258
|
"have a width less than or equal to #{@width}"
|
240
259
|
end
|
260
|
+
|
261
|
+
# RSpec 2 compatibility:
|
262
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
241
263
|
end
|
242
264
|
|
243
265
|
def be_no_wider_than(width)
|
@@ -261,19 +283,55 @@ module CarrierWave
|
|
261
283
|
"expected #{@actual.current_path.inspect} to be no taller than #{@height}, but it was #{@actual_height}."
|
262
284
|
end
|
263
285
|
|
264
|
-
def
|
286
|
+
def failure_message_when_negated
|
265
287
|
"expected #{@actual.current_path.inspect} not to be taller than #{@height}, but it is."
|
266
288
|
end
|
267
289
|
|
268
290
|
def description
|
269
291
|
"have a height less than or equal to #{@height}"
|
270
292
|
end
|
293
|
+
|
294
|
+
# RSpec 2 compatibility:
|
295
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
271
296
|
end
|
272
297
|
|
273
298
|
def be_no_taller_than(height)
|
274
299
|
BeNoTallerThan.new(height)
|
275
300
|
end
|
276
301
|
|
302
|
+
class BeFormat # :nodoc:
|
303
|
+
def initialize(expected)
|
304
|
+
@expected = expected
|
305
|
+
end
|
306
|
+
|
307
|
+
def matches?(actual)
|
308
|
+
@actual = actual
|
309
|
+
# Satisfy expectation here. Return false or raise an error if it's not met.
|
310
|
+
image = ImageLoader.load_image(@actual.current_path)
|
311
|
+
@actual_expected = image.format
|
312
|
+
!@expected.nil? && @actual_expected.casecmp(@expected).zero?
|
313
|
+
end
|
314
|
+
|
315
|
+
def failure_message
|
316
|
+
"expected #{@actual.current_path.inspect} to have #{@expected} format, but it was #{@actual_expected}."
|
317
|
+
end
|
318
|
+
|
319
|
+
def failure_message_when_negated
|
320
|
+
"expected #{@actual.current_path.inspect} not to have #{@expected} format, but it did."
|
321
|
+
end
|
322
|
+
|
323
|
+
def description
|
324
|
+
"have #{@expected} format"
|
325
|
+
end
|
326
|
+
|
327
|
+
# RSpec 2 compatibility:
|
328
|
+
alias_method :negative_failure_message, :failure_message_when_negated
|
329
|
+
end
|
330
|
+
|
331
|
+
def be_format(expected)
|
332
|
+
BeFormat.new(expected)
|
333
|
+
end
|
334
|
+
|
277
335
|
class ImageLoader # :nodoc:
|
278
336
|
def self.load_image(filename)
|
279
337
|
if defined? ::MiniMagick
|
@@ -303,6 +361,10 @@ module CarrierWave
|
|
303
361
|
image.rows
|
304
362
|
end
|
305
363
|
|
364
|
+
def format
|
365
|
+
image.format
|
366
|
+
end
|
367
|
+
|
306
368
|
def initialize(filename)
|
307
369
|
@image = ::Magick::Image.read(filename).first
|
308
370
|
end
|
@@ -318,6 +380,10 @@ module CarrierWave
|
|
318
380
|
image[:height]
|
319
381
|
end
|
320
382
|
|
383
|
+
def format
|
384
|
+
image[:format]
|
385
|
+
end
|
386
|
+
|
321
387
|
def initialize(filename)
|
322
388
|
@image = ::MiniMagick::Image.open(filename)
|
323
389
|
end
|
@@ -326,4 +392,3 @@ module CarrierWave
|
|
326
392
|
end # Matchers
|
327
393
|
end # Test
|
328
394
|
end # CarrierWave
|
329
|
-
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
|
5
3
|
class FormNotMultipart < UploadError
|
@@ -8,15 +6,27 @@ module CarrierWave
|
|
8
6
|
end
|
9
7
|
end
|
10
8
|
|
9
|
+
class CacheCounter
|
10
|
+
@@counter = 0
|
11
|
+
|
12
|
+
def self.increment
|
13
|
+
@@counter += 1
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
11
17
|
##
|
12
18
|
# Generates a unique cache id for use in the caching system
|
13
19
|
#
|
14
20
|
# === Returns
|
15
21
|
#
|
16
|
-
# [String] a cache id in the format TIMEINT-PID-RND
|
22
|
+
# [String] a cache id in the format TIMEINT-PID-COUNTER-RND
|
17
23
|
#
|
18
24
|
def self.generate_cache_id
|
19
|
-
Time.now.utc.to_i
|
25
|
+
[Time.now.utc.to_i,
|
26
|
+
Process.pid,
|
27
|
+
'%04d' % (CarrierWave::CacheCounter.increment % 1000),
|
28
|
+
'%04d' % rand(9999)
|
29
|
+
].map(&:to_s).join('-')
|
20
30
|
end
|
21
31
|
|
22
32
|
module Uploader
|
@@ -42,13 +52,7 @@ module CarrierWave
|
|
42
52
|
# It's recommended that you keep cache files in one place only.
|
43
53
|
#
|
44
54
|
def clean_cached_files!(seconds=60*60*24)
|
45
|
-
|
46
|
-
time = dir.scan(/(\d+)-\d+-\d+/).first.map { |t| t.to_i }
|
47
|
-
time = Time.at(*time)
|
48
|
-
if time < (Time.now.utc - seconds)
|
49
|
-
FileUtils.rm_rf(dir)
|
50
|
-
end
|
51
|
-
end
|
55
|
+
cache_storage.new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
@@ -77,10 +81,8 @@ module CarrierWave
|
|
77
81
|
_content = file.read
|
78
82
|
if _content.is_a?(File) # could be if storage is Fog
|
79
83
|
sanitized = CarrierWave::Storage::Fog.new(self).retrieve!(File.basename(_content.path))
|
80
|
-
sanitized.read if sanitized.exists?
|
81
|
-
|
82
84
|
else
|
83
|
-
sanitized = SanitizedFile.new :tempfile => StringIO.new(
|
85
|
+
sanitized = SanitizedFile.new :tempfile => StringIO.new(_content),
|
84
86
|
:filename => File.basename(path), :content_type => file.content_type
|
85
87
|
end
|
86
88
|
sanitized
|
@@ -91,7 +93,7 @@ module CarrierWave
|
|
91
93
|
#
|
92
94
|
# === Returns
|
93
95
|
#
|
94
|
-
# [String] a cache name, in the format
|
96
|
+
# [String] a cache name, in the format TIMEINT-PID-COUNTER-RND/filename.txt
|
95
97
|
#
|
96
98
|
def cache_name
|
97
99
|
File.join(cache_id, full_original_filename) if cache_id and original_filename
|
@@ -115,22 +117,28 @@ module CarrierWave
|
|
115
117
|
#
|
116
118
|
def cache!(new_file = sanitized_file)
|
117
119
|
new_file = CarrierWave::SanitizedFile.new(new_file)
|
120
|
+
return if new_file.empty?
|
118
121
|
|
119
|
-
|
120
|
-
raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form
|
122
|
+
raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form
|
121
123
|
|
122
|
-
|
123
|
-
self.cache_id = CarrierWave.generate_cache_id unless cache_id
|
124
|
+
self.cache_id = CarrierWave.generate_cache_id unless cache_id
|
124
125
|
|
125
|
-
|
126
|
-
|
126
|
+
@filename = new_file.filename
|
127
|
+
self.original_filename = new_file.filename
|
127
128
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
begin
|
130
|
+
# first, create a workfile on which we perform processings
|
131
|
+
if move_to_cache
|
132
|
+
@file = new_file.move_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
|
133
|
+
else
|
134
|
+
@file = new_file.copy_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
|
133
135
|
end
|
136
|
+
|
137
|
+
with_callbacks(:cache, @file) do
|
138
|
+
@file = cache_storage.cache!(@file)
|
139
|
+
end
|
140
|
+
ensure
|
141
|
+
FileUtils.rm_rf(workfile_path(''))
|
134
142
|
end
|
135
143
|
end
|
136
144
|
|
@@ -149,14 +157,29 @@ module CarrierWave
|
|
149
157
|
with_callbacks(:retrieve_from_cache, cache_name) do
|
150
158
|
self.cache_id, self.original_filename = cache_name.to_s.split('/', 2)
|
151
159
|
@filename = original_filename
|
152
|
-
@file =
|
160
|
+
@file = cache_storage.retrieve_from_cache!(full_filename(original_filename))
|
153
161
|
end
|
154
162
|
end
|
155
163
|
|
164
|
+
##
|
165
|
+
# Calculates the path where the cache file should be stored.
|
166
|
+
#
|
167
|
+
# === Parameters
|
168
|
+
#
|
169
|
+
# [for_file (String)] name of the file <optional>
|
170
|
+
#
|
171
|
+
# === Returns
|
172
|
+
#
|
173
|
+
# [String] the cache path
|
174
|
+
#
|
175
|
+
def cache_path(for_file=full_filename(original_filename))
|
176
|
+
File.join(*[cache_dir, @cache_id, for_file].compact)
|
177
|
+
end
|
178
|
+
|
156
179
|
private
|
157
180
|
|
158
|
-
def
|
159
|
-
File.
|
181
|
+
def workfile_path(for_file=original_filename)
|
182
|
+
File.join(CarrierWave.tmp_path, @cache_id, version_name.to_s, for_file)
|
160
183
|
end
|
161
184
|
|
162
185
|
attr_reader :cache_id, :original_filename
|
@@ -165,7 +188,9 @@ module CarrierWave
|
|
165
188
|
alias_method :full_original_filename, :original_filename
|
166
189
|
|
167
190
|
def cache_id=(cache_id)
|
168
|
-
|
191
|
+
# Earlier version used 3 part cache_id. Thus we should allow for
|
192
|
+
# 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/
|
169
194
|
@cache_id = cache_id
|
170
195
|
end
|
171
196
|
|
@@ -174,6 +199,9 @@ module CarrierWave
|
|
174
199
|
@original_filename = filename
|
175
200
|
end
|
176
201
|
|
202
|
+
def cache_storage
|
203
|
+
@cache_storage ||= self.class.cache_storage.new(self)
|
204
|
+
end
|
177
205
|
end # Cache
|
178
206
|
end # Uploader
|
179
207
|
end # CarrierWave
|