-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
versions.rb
316 lines (274 loc) · 9.58 KB
/
versions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
module CarrierWave
module Uploader
module Versions
extend ActiveSupport::Concern
include CarrierWave::Uploader::Callbacks
included do
class_attribute :versions, :version_names, :version_options, :instance_reader => false, :instance_writer => false
self.versions = {}
self.version_names = []
attr_accessor :parent_version
after :cache, :cache_versions!
after :store, :store_versions!
after :remove, :remove_versions!
after :retrieve_from_cache, :retrieve_versions_from_cache!
after :retrieve_from_store, :retrieve_versions_from_store!
prepend Module.new {
def initialize(*)
super
@versions = nil
end
}
end
module ClassMethods
##
# Adds a new version to this uploader
#
# === Parameters
#
# [name (#to_sym)] name of the version
# [options (Hash)] optional options hash
# [&block (Proc)] a block to eval on this version of the uploader
#
# === Examples
#
# class MyUploader < CarrierWave::Uploader::Base
#
# version :thumb do
# process :scale => [200, 200]
# end
#
# version :preview, :if => :image? do
# process :scale => [200, 200]
# end
#
# version :square, :unless => :invalid_image_type? do
# process :scale => [100, 100]
# end
#
# end
#
def version(name, options = {}, &block)
name = name.to_sym
build_version(name, options)
versions[name].class_eval(&block) if block
versions[name]
end
def recursively_apply_block_to_versions(&block)
versions.each do |name, version|
version.class_eval(&block)
version.recursively_apply_block_to_versions(&block)
end
end
private
def build_version(name, options)
if !versions.has_key?(name)
uploader = Class.new(self)
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
uploader.version_names += [name]
uploader.versions = {}
uploader.processors = []
uploader.version_options = options
uploader.class_eval <<-RUBY, __FILE__, __LINE__ + 1
# Define the enable_processing method for versions so they get the
# value from the parent class unless explicitly overwritten
def self.enable_processing(value=nil)
self.enable_processing = value if value
if defined?(@enable_processing) && !@enable_processing.nil?
@enable_processing
else
superclass.enable_processing
end
end
# Regardless of what is set in the parent uploader, do not enforce the
# move_to_cache config option on versions because it moves the original
# file to the version's target file.
#
# If you want to enforce this setting on versions, override this method
# in each version:
#
# version :thumb do
# def move_to_cache
# true
# end
# end
#
def move_to_cache
false
end
RUBY
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}
versions[:#{name}]
end
RUBY
else
uploader = Class.new(versions[name])
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
uploader.processors = []
uploader.version_options = uploader.version_options.merge(options)
end
# Add the current version hash to class attribute :versions
self.versions = versions.merge(name => uploader)
end
end # ClassMethods
##
# Returns a hash mapping the name of each version of the uploader to an instance of it
#
# === Returns
#
# [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
#
def versions
return @versions if @versions
@versions = {}
self.class.versions.each do |name, version|
@versions[name] = version.new(model, mounted_as)
@versions[name].parent_version = self
end
@versions
end
##
# === Returns
#
# [String] the name of this version of the uploader
#
def version_name
self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
end
##
#
# === Parameters
#
# [name (#to_sym)] name of the version
#
# === Returns
#
# [Boolean] True when the version exists according to its :if or :unless condition
#
def version_exists?(name)
name = name.to_sym
return false unless self.class.versions.has_key?(name)
if_condition = self.class.versions[name].version_options[:if]
unless_condition = self.class.versions[name].version_options[:unless]
if if_condition
if if_condition.respond_to?(:call)
if_condition.call(self, :version => name, :file => file)
else
send(if_condition, file)
end
elsif unless_condition
if unless_condition.respond_to?(:call)
!unless_condition.call(self, :version => name, :file => file)
else
!send(unless_condition, file)
end
else
true
end
end
##
# Copies the parent's cache_id when caching a version file.
# This behavior is not essential but it makes easier to understand
# that the cached files are generated by the single upload attempt.
#
def cache!(*args)
self.cache_id = parent_version.cache_id if parent_version
super
end
##
# When given a version name as a parameter, will return the url for that version
# This also works with nested versions.
# When given a query hash as a parameter, will return the url with signature that contains query params
# Query hash only works with AWS (S3 storage).
#
# === Example
#
# my_uploader.url # => /path/to/my/uploader.gif
# my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
# my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
# my_uploader.url(:query => {"response-content-disposition" => "attachment"})
# my_uploader.url(:version, :sub_version, :query => {"response-content-disposition" => "attachment"})
#
# === Parameters
#
# [*args (Symbol)] any number of versions
# OR/AND
# [Hash] query params
#
# === Returns
#
# [String] the location where this file is accessible via a url
#
def url(*args)
if (version = args.first) && version.respond_to?(:to_sym)
raise ArgumentError, "Version #{version} doesn't exist!" if versions[version.to_sym].nil?
# recursively proxy to version
versions[version.to_sym].url(*args[1..-1])
elsif args.first
super(args.first)
else
super
end
end
##
# Recreate versions and reprocess them. This can be used to recreate
# versions if their parameters somehow have changed.
#
def recreate_versions!(*names)
# As well as specified versions, we need to reprocess versions
# that are the source of another version.
self.cache_id = CarrierWave.generate_cache_id
derived_versions.each do |name, v|
v.cache!(file) if names.empty? || !(v.descendant_version_names & names).empty?
end
active_versions.each do |name, v|
v.store! if names.empty? || names.include?(name)
end
end
protected
def descendant_version_names
[version_name] + derived_versions.flat_map do |name, version|
version.descendant_version_names
end
end
def active_versions
versions.select do |name, uploader|
version_exists?(name)
end
end
private
def derived_versions
active_versions.reject do |name, v|
v.class.version_options[:from_version]
end.to_a + active_sibling_versions.select do |name, v|
v.class.version_options[:from_version] == self.class.version_names.last
end.to_a
end
def active_sibling_versions
parent_version&.active_versions || []
end
def full_filename(for_file)
[version_name, super(for_file)].compact.join('_')
end
def full_original_filename
[version_name, super].compact.join('_')
end
def cache_versions!(new_file)
derived_versions.each { |name, v| v.cache!(new_file) }
end
def store_versions!(new_file)
active_versions.each { |name, v| v.store!(new_file) }
end
def remove_versions!
versions.each { |name, v| v.remove! }
end
def retrieve_versions_from_cache!(cache_name)
active_versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
end
def retrieve_versions_from_store!(identifier)
active_versions.each { |name, v| v.retrieve_from_store!(identifier) }
end
end # Versions
end # Uploader
end # CarrierWave