carrierwave 0.9.0 → 2.1.1
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 +7 -0
- data/README.md +362 -116
- data/lib/carrierwave/compatibility/paperclip.rb +29 -21
- data/lib/carrierwave/downloader/base.rb +83 -0
- data/lib/carrierwave/downloader/remote_file.rb +65 -0
- data/lib/carrierwave/error.rb +1 -0
- data/lib/carrierwave/locale/en.yml +7 -4
- data/lib/carrierwave/mount.rb +238 -186
- data/lib/carrierwave/mounter.rb +188 -0
- data/lib/carrierwave/orm/activerecord.rb +60 -24
- data/lib/carrierwave/processing/mini_magick.rb +139 -78
- data/lib/carrierwave/processing/rmagick.rb +68 -23
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/sanitized_file.rb +67 -27
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +69 -2
- data/lib/carrierwave/storage/fog.rb +180 -41
- data/lib/carrierwave/storage.rb +1 -7
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader/cache.rb +74 -38
- data/lib/carrierwave/uploader/callbacks.rb +0 -2
- data/lib/carrierwave/uploader/configuration.rb +72 -6
- 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 +5 -69
- 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/mountable.rb +13 -8
- data/lib/carrierwave/uploader/processing.rb +15 -17
- data/lib/carrierwave/uploader/proxy.rb +16 -7
- data/lib/carrierwave/uploader/remove.rb +0 -2
- data/lib/carrierwave/uploader/serialization.rb +3 -5
- data/lib/carrierwave/uploader/store.rb +17 -24
- data/lib/carrierwave/uploader/url.rb +3 -5
- data/lib/carrierwave/uploader/versions.rb +117 -86
- data/lib/carrierwave/uploader.rb +6 -2
- data/lib/carrierwave/utilities/uri.rb +5 -6
- data/lib/carrierwave/utilities.rb +1 -3
- data/lib/carrierwave/validations/active_model.rb +3 -7
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +36 -3
- data/lib/generators/templates/uploader.rb +4 -8
- data/lib/generators/uploader_generator.rb +1 -1
- metadata +195 -94
- data/lib/carrierwave/locale/cs.yml +0 -11
- data/lib/carrierwave/locale/de.yml +0 -11
- data/lib/carrierwave/locale/nl.yml +0 -11
- data/lib/carrierwave/locale/sk.yml +0 -11
- data/lib/carrierwave/processing/mime_types.rb +0 -73
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Uploader
|
5
|
+
module FileSize
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
before :cache, :check_size!
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Override this method in your uploader to provide a Range of Size which
|
14
|
+
# are allowed to be uploaded.
|
15
|
+
# === Returns
|
16
|
+
#
|
17
|
+
# [NilClass, Range] a size range which are permitted to be uploaded
|
18
|
+
#
|
19
|
+
# === Examples
|
20
|
+
#
|
21
|
+
# def size_range
|
22
|
+
# 3256...5748
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
def size_range; end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def check_size!(new_file)
|
30
|
+
size = new_file.size
|
31
|
+
expected_size_range = size_range
|
32
|
+
if expected_size_range.is_a?(::Range)
|
33
|
+
if size < expected_size_range.min
|
34
|
+
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.min_size_error", :min_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.min))
|
35
|
+
elsif size > expected_size_range.max
|
36
|
+
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.max_size_error", :max_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.max))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end # FileSize
|
42
|
+
end # Uploader
|
43
|
+
end # CarrierWave
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Mountable
|
@@ -7,13 +5,14 @@ module CarrierWave
|
|
7
5
|
attr_reader :model, :mounted_as
|
8
6
|
|
9
7
|
##
|
10
|
-
# If a model is given as the first parameter, it will be stored in the
|
11
|
-
# available
|
12
|
-
# where this instance of the uploader is mounted.
|
13
|
-
# your uploader.
|
8
|
+
# If a model is given as the first parameter, it will be stored in the
|
9
|
+
# uploader, and available through +#model+. Likewise, mounted_as stores
|
10
|
+
# the name of the column where this instance of the uploader is mounted.
|
11
|
+
# These values can then be used inside your uploader.
|
14
12
|
#
|
15
|
-
# If you do not wish to mount your uploaders with the ORM extensions in
|
16
|
-
# can override this method inside your uploader. Just be
|
13
|
+
# If you do not wish to mount your uploaders with the ORM extensions in
|
14
|
+
# -more then you can override this method inside your uploader. Just be
|
15
|
+
# sure to call +super+
|
17
16
|
#
|
18
17
|
# === Parameters
|
19
18
|
#
|
@@ -34,6 +33,12 @@ module CarrierWave
|
|
34
33
|
@mounted_as = mounted_as
|
35
34
|
end
|
36
35
|
|
36
|
+
##
|
37
|
+
# Returns array index of given uploader within currently mounted uploaders
|
38
|
+
#
|
39
|
+
def index
|
40
|
+
model.__send__(:_mounter, mounted_as).uploaders.index(self)
|
41
|
+
end
|
37
42
|
end # Mountable
|
38
43
|
end # Uploader
|
39
44
|
end # CarrierWave
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Processing
|
@@ -11,7 +9,7 @@ module CarrierWave
|
|
11
9
|
class_attribute :processors, :instance_writer => false
|
12
10
|
self.processors = []
|
13
11
|
|
14
|
-
|
12
|
+
before :cache, :process!
|
15
13
|
end
|
16
14
|
|
17
15
|
module ClassMethods
|
@@ -54,20 +52,14 @@ module CarrierWave
|
|
54
52
|
# end
|
55
53
|
#
|
56
54
|
def process(*args)
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
new_processors = args.inject({}) do |hash, arg|
|
56
|
+
arg = { arg => [] } unless arg.is_a?(Hash)
|
57
|
+
hash.merge!(arg)
|
60
58
|
end
|
61
59
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
arg.each do |method, args|
|
66
|
-
self.processors += [[method, args, condition]]
|
67
|
-
end
|
68
|
-
else
|
69
|
-
self.processors += [[arg, [], nil]]
|
70
|
-
end
|
60
|
+
condition = new_processors.delete(:if)
|
61
|
+
new_processors.each do |processor, processor_args|
|
62
|
+
self.processors += [[processor, processor_args, condition]]
|
71
63
|
end
|
72
64
|
end
|
73
65
|
|
@@ -77,10 +69,16 @@ module CarrierWave
|
|
77
69
|
# Apply all process callbacks added through CarrierWave.process
|
78
70
|
#
|
79
71
|
def process!(new_file=nil)
|
80
|
-
|
72
|
+
return unless enable_processing
|
73
|
+
|
74
|
+
with_callbacks(:process, new_file) do
|
81
75
|
self.class.processors.each do |method, args, condition|
|
82
76
|
if(condition)
|
83
|
-
|
77
|
+
if condition.respond_to?(:call)
|
78
|
+
next unless condition.call(self, :args => args, :method => method, :file => new_file)
|
79
|
+
else
|
80
|
+
next unless self.send(condition, new_file)
|
81
|
+
end
|
84
82
|
end
|
85
83
|
self.send(method, *args)
|
86
84
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Proxy
|
@@ -19,20 +17,20 @@ module CarrierWave
|
|
19
17
|
# [String] the path where the file is currently located.
|
20
18
|
#
|
21
19
|
def current_path
|
22
|
-
file.
|
20
|
+
file.try(:path)
|
23
21
|
end
|
24
22
|
|
25
23
|
alias_method :path, :current_path
|
26
24
|
|
27
25
|
##
|
28
|
-
# Returns a string that uniquely identifies the last stored file
|
26
|
+
# Returns a string that uniquely identifies the retrieved or last stored file
|
29
27
|
#
|
30
28
|
# === Returns
|
31
29
|
#
|
32
30
|
# [String] uniquely identifies a file
|
33
31
|
#
|
34
32
|
def identifier
|
35
|
-
|
33
|
+
@identifier || storage.try(:identifier)
|
36
34
|
end
|
37
35
|
|
38
36
|
##
|
@@ -43,7 +41,7 @@ module CarrierWave
|
|
43
41
|
# [String] contents of the file
|
44
42
|
#
|
45
43
|
def read
|
46
|
-
file.
|
44
|
+
file.try(:read)
|
47
45
|
end
|
48
46
|
|
49
47
|
##
|
@@ -54,7 +52,7 @@ module CarrierWave
|
|
54
52
|
# [Integer] size of the file
|
55
53
|
#
|
56
54
|
def size
|
57
|
-
file.
|
55
|
+
file.try(:size) || 0
|
58
56
|
end
|
59
57
|
|
60
58
|
##
|
@@ -72,6 +70,17 @@ module CarrierWave
|
|
72
70
|
size
|
73
71
|
end
|
74
72
|
|
73
|
+
##
|
74
|
+
# Read the content type of the file
|
75
|
+
#
|
76
|
+
# === Returns
|
77
|
+
#
|
78
|
+
# [String] content type of the file
|
79
|
+
#
|
80
|
+
def content_type
|
81
|
+
file.try(:content_type)
|
82
|
+
end
|
83
|
+
|
75
84
|
end # Proxy
|
76
85
|
end # Uploader
|
77
86
|
end # CarrierWave
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require "json"
|
4
2
|
require "active_support/core_ext/hash"
|
5
3
|
|
@@ -8,12 +6,12 @@ module CarrierWave
|
|
8
6
|
module Serialization
|
9
7
|
extend ActiveSupport::Concern
|
10
8
|
|
11
|
-
def serializable_hash
|
12
|
-
{"url" => url}.merge Hash[versions.map { |name, version| [name, { "url" => version.url }] }]
|
9
|
+
def serializable_hash(options = nil)
|
10
|
+
{"url" => url}.merge Hash[versions.map { |name, version| [name.to_s, { "url" => version.url }] }]
|
13
11
|
end
|
14
12
|
|
15
13
|
def as_json(options=nil)
|
16
|
-
|
14
|
+
serializable_hash
|
17
15
|
end
|
18
16
|
|
19
17
|
def to_json(options=nil)
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Store
|
@@ -9,6 +7,15 @@ module CarrierWave
|
|
9
7
|
include CarrierWave::Uploader::Configuration
|
10
8
|
include CarrierWave::Uploader::Cache
|
11
9
|
|
10
|
+
included do
|
11
|
+
prepend Module.new {
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
@file, @filename, @cache_id, @identifier = nil
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
12
19
|
##
|
13
20
|
# Override this in your Uploader to change the filename.
|
14
21
|
#
|
@@ -54,31 +61,16 @@ module CarrierWave
|
|
54
61
|
#
|
55
62
|
def store!(new_file=nil)
|
56
63
|
cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?)
|
57
|
-
if @file
|
64
|
+
if !cache_only && @file && @cache_id
|
58
65
|
with_callbacks(:store, new_file) do
|
59
66
|
new_file = storage.store!(@file)
|
60
|
-
|
61
|
-
|
67
|
+
if delete_tmp_file_after_storage
|
68
|
+
@file.delete unless move_to_store
|
69
|
+
cache_storage.delete_dir!(cache_path(nil))
|
70
|
+
end
|
62
71
|
@file = new_file
|
63
|
-
@cache_id = nil
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# Deletes a cache id (tmp dir in cache)
|
70
|
-
#
|
71
|
-
def delete_cache_id
|
72
|
-
if @cache_id
|
73
|
-
path = File.expand_path(File.join(cache_dir, @cache_id), CarrierWave.root)
|
74
|
-
begin
|
75
|
-
Dir.rmdir(path)
|
76
|
-
rescue Errno::ENOENT
|
77
|
-
# Ignore: path does not exist
|
78
|
-
rescue Errno::ENOTDIR
|
79
|
-
# Ignore: path is not a dir
|
80
|
-
rescue Errno::ENOTEMPTY, Errno::EEXIST
|
81
|
-
# Ignore: dir is not empty
|
72
|
+
@cache_id = @identifier = nil
|
73
|
+
@staged = false
|
82
74
|
end
|
83
75
|
end
|
84
76
|
end
|
@@ -93,6 +85,7 @@ module CarrierWave
|
|
93
85
|
def retrieve_from_store!(identifier)
|
94
86
|
with_callbacks(:retrieve_from_store, identifier) do
|
95
87
|
@file = storage.retrieve!(identifier)
|
88
|
+
@identifier = identifier
|
96
89
|
end
|
97
90
|
end
|
98
91
|
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Url
|
@@ -17,10 +15,10 @@ module CarrierWave
|
|
17
15
|
# [String] the location where this file is accessible via a url
|
18
16
|
#
|
19
17
|
def url(options = {})
|
20
|
-
if file.respond_to?(:url)
|
21
|
-
file.method(:url).arity
|
18
|
+
if file.respond_to?(:url) && !(tmp_url = file.url).blank?
|
19
|
+
file.method(:url).arity.zero? ? tmp_url : file.url(options)
|
22
20
|
elsif file.respond_to?(:path)
|
23
|
-
path = encode_path(file.path.
|
21
|
+
path = encode_path(file.path.sub(File.expand_path(root), ''))
|
24
22
|
|
25
23
|
if host = asset_host
|
26
24
|
if host.respond_to? :call
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module CarrierWave
|
4
2
|
module Uploader
|
5
3
|
module Versions
|
@@ -8,12 +6,12 @@ module CarrierWave
|
|
8
6
|
include CarrierWave::Uploader::Callbacks
|
9
7
|
|
10
8
|
included do
|
11
|
-
class_attribute :versions, :version_names, :instance_reader => false, :instance_writer => false
|
9
|
+
class_attribute :versions, :version_names, :version_options, :instance_reader => false, :instance_writer => false
|
12
10
|
|
13
11
|
self.versions = {}
|
14
12
|
self.version_names = []
|
15
13
|
|
16
|
-
attr_accessor :parent_cache_id
|
14
|
+
attr_accessor :parent_cache_id, :parent_version
|
17
15
|
|
18
16
|
after :cache, :assign_parent_cache_id
|
19
17
|
after :cache, :cache_versions!
|
@@ -21,6 +19,13 @@ module CarrierWave
|
|
21
19
|
after :remove, :remove_versions!
|
22
20
|
after :retrieve_from_cache, :retrieve_versions_from_cache!
|
23
21
|
after :retrieve_from_store, :retrieve_versions_from_store!
|
22
|
+
|
23
|
+
prepend Module.new {
|
24
|
+
def initialize(*)
|
25
|
+
super
|
26
|
+
@versions = nil
|
27
|
+
end
|
28
|
+
}
|
24
29
|
end
|
25
30
|
|
26
31
|
module ClassMethods
|
@@ -50,73 +55,76 @@ module CarrierWave
|
|
50
55
|
#
|
51
56
|
def version(name, options = {}, &block)
|
52
57
|
name = name.to_sym
|
53
|
-
|
58
|
+
build_version(name, options)
|
59
|
+
|
60
|
+
versions[name].class_eval(&block) if block
|
61
|
+
versions[name]
|
62
|
+
end
|
63
|
+
|
64
|
+
def recursively_apply_block_to_versions(&block)
|
65
|
+
versions.each do |name, version|
|
66
|
+
version.class_eval(&block)
|
67
|
+
version.recursively_apply_block_to_versions(&block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def build_version(name, options)
|
74
|
+
if !versions.has_key?(name)
|
54
75
|
uploader = Class.new(self)
|
55
|
-
const_set("Uploader#{uploader.object_id}".
|
76
|
+
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
|
77
|
+
uploader.version_names += [name]
|
56
78
|
uploader.versions = {}
|
79
|
+
uploader.processors = []
|
80
|
+
uploader.version_options = options
|
57
81
|
|
58
|
-
# Define the enable_processing method for versions so they get the
|
59
|
-
# value from the parent class unless explicitly overwritten
|
60
82
|
uploader.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
83
|
+
# Define the enable_processing method for versions so they get the
|
84
|
+
# value from the parent class unless explicitly overwritten
|
61
85
|
def self.enable_processing(value=nil)
|
62
86
|
self.enable_processing = value if value
|
63
|
-
if !@enable_processing.nil?
|
87
|
+
if defined?(@enable_processing) && !@enable_processing.nil?
|
64
88
|
@enable_processing
|
65
89
|
else
|
66
90
|
superclass.enable_processing
|
67
91
|
end
|
68
92
|
end
|
69
|
-
RUBY
|
70
93
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
uploader.class_eval <<-RUBY
|
94
|
+
# Regardless of what is set in the parent uploader, do not enforce the
|
95
|
+
# move_to_cache config option on versions because it moves the original
|
96
|
+
# file to the version's target file.
|
97
|
+
#
|
98
|
+
# If you want to enforce this setting on versions, override this method
|
99
|
+
# in each version:
|
100
|
+
#
|
101
|
+
# version :thumb do
|
102
|
+
# def move_to_cache
|
103
|
+
# true
|
104
|
+
# end
|
105
|
+
# end
|
106
|
+
#
|
85
107
|
def move_to_cache
|
86
108
|
false
|
87
109
|
end
|
88
110
|
RUBY
|
89
111
|
|
90
|
-
|
91
|
-
current_version = {}
|
92
|
-
current_version[name] = {
|
93
|
-
:uploader => uploader,
|
94
|
-
:options => options
|
95
|
-
}
|
96
|
-
self.versions = versions.merge(current_version)
|
97
|
-
|
98
|
-
versions[name][:uploader].version_names += [name]
|
99
|
-
|
100
|
-
class_eval <<-RUBY
|
112
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
101
113
|
def #{name}
|
102
114
|
versions[:#{name}]
|
103
115
|
end
|
104
116
|
RUBY
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
117
|
+
else
|
118
|
+
uploader = Class.new(versions[name])
|
119
|
+
const_set("Uploader#{uploader.object_id}".tr('-', '_'), uploader)
|
120
|
+
uploader.processors = []
|
121
|
+
uploader.version_options = uploader.version_options.merge(options)
|
109
122
|
end
|
110
|
-
versions[name][:uploader].class_eval(&block) if block
|
111
|
-
versions[name]
|
112
|
-
end
|
113
123
|
|
114
|
-
|
115
|
-
versions
|
116
|
-
version[:uploader].class_eval(&block)
|
117
|
-
version[:uploader].recursively_apply_block_to_versions(&block)
|
118
|
-
end
|
124
|
+
# Add the current version hash to class attribute :versions
|
125
|
+
self.versions = versions.merge(name => uploader)
|
119
126
|
end
|
127
|
+
|
120
128
|
end # ClassMethods
|
121
129
|
|
122
130
|
##
|
@@ -130,7 +138,8 @@ module CarrierWave
|
|
130
138
|
return @versions if @versions
|
131
139
|
@versions = {}
|
132
140
|
self.class.versions.each do |name, version|
|
133
|
-
@versions[name] = version
|
141
|
+
@versions[name] = version.new(model, mounted_as)
|
142
|
+
@versions[name].parent_version = self
|
134
143
|
end
|
135
144
|
@versions
|
136
145
|
end
|
@@ -159,7 +168,7 @@ module CarrierWave
|
|
159
168
|
|
160
169
|
return false unless self.class.versions.has_key?(name)
|
161
170
|
|
162
|
-
condition = self.class.versions[name][:
|
171
|
+
condition = self.class.versions[name].version_options[:if]
|
163
172
|
if(condition)
|
164
173
|
if(condition.respond_to?(:call))
|
165
174
|
condition.call(self, :version => name, :file => file)
|
@@ -199,7 +208,7 @@ module CarrierWave
|
|
199
208
|
if (version = args.first) && version.respond_to?(:to_sym)
|
200
209
|
raise ArgumentError, "Version #{version} doesn't exist!" if versions[version.to_sym].nil?
|
201
210
|
# recursively proxy to version
|
202
|
-
versions[version.to_sym].url(*args[1..-1])
|
211
|
+
versions[version.to_sym].url(*args[1..-1])
|
203
212
|
elsif args.first
|
204
213
|
super(args.first)
|
205
214
|
else
|
@@ -211,16 +220,18 @@ module CarrierWave
|
|
211
220
|
# Recreate versions and reprocess them. This can be used to recreate
|
212
221
|
# versions if their parameters somehow have changed.
|
213
222
|
#
|
214
|
-
def recreate_versions!(*
|
223
|
+
def recreate_versions!(*names)
|
215
224
|
# Some files could possibly not be stored on the local disk. This
|
216
225
|
# doesn't play nicely with processing. Make sure that we're only
|
217
226
|
# processing a cached file
|
218
227
|
#
|
219
228
|
# The call to store! will trigger the necessary callbacks to both
|
220
229
|
# process this version and all sub-versions
|
221
|
-
|
222
|
-
|
223
|
-
|
230
|
+
|
231
|
+
if names.any?
|
232
|
+
set_versions_to_cache_and_store(names)
|
233
|
+
store!(file)
|
234
|
+
reset_versions_to_cache_and_store
|
224
235
|
else
|
225
236
|
cache! if !cached?
|
226
237
|
store!
|
@@ -228,6 +239,39 @@ module CarrierWave
|
|
228
239
|
end
|
229
240
|
|
230
241
|
private
|
242
|
+
|
243
|
+
def set_versions_to_cache_and_store(names)
|
244
|
+
@versions_to_cache = source_versions_of(names)
|
245
|
+
@versions_to_store = active_versions_with_names_in(@versions_to_cache + names)
|
246
|
+
end
|
247
|
+
|
248
|
+
def reset_versions_to_cache_and_store
|
249
|
+
@versions_to_cache, @versions_to_store = nil, nil
|
250
|
+
end
|
251
|
+
|
252
|
+
def versions_to_cache
|
253
|
+
@versions_to_cache || dependent_versions
|
254
|
+
end
|
255
|
+
|
256
|
+
def versions_to_store
|
257
|
+
@versions_to_store || active_versions
|
258
|
+
end
|
259
|
+
|
260
|
+
def source_versions_of(requested_names)
|
261
|
+
versions.inject([]) do |sources, (name, uploader)|
|
262
|
+
next sources unless requested_names.include?(name)
|
263
|
+
next sources unless source_name = uploader.class.version_options[:from_version]
|
264
|
+
|
265
|
+
sources << [source_name, versions[source_name]]
|
266
|
+
end.uniq
|
267
|
+
end
|
268
|
+
|
269
|
+
def active_versions_with_names_in(names)
|
270
|
+
active_versions.select do |pretendent_name, uploader|
|
271
|
+
names.include?(pretendent_name)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
231
275
|
def assign_parent_cache_id(file)
|
232
276
|
active_versions.each do |name, uploader|
|
233
277
|
uploader.parent_cache_id = @cache_id
|
@@ -240,6 +284,18 @@ module CarrierWave
|
|
240
284
|
end
|
241
285
|
end
|
242
286
|
|
287
|
+
def dependent_versions
|
288
|
+
active_versions.reject do |name, v|
|
289
|
+
v.class.version_options[:from_version]
|
290
|
+
end.to_a + sibling_versions.select do |name, v|
|
291
|
+
v.class.version_options[:from_version] == self.class.version_names.last
|
292
|
+
end.to_a
|
293
|
+
end
|
294
|
+
|
295
|
+
def sibling_versions
|
296
|
+
parent_version.try(:versions) || []
|
297
|
+
end
|
298
|
+
|
243
299
|
def full_filename(for_file)
|
244
300
|
[version_name, super(for_file)].compact.join('_')
|
245
301
|
end
|
@@ -249,39 +305,14 @@ module CarrierWave
|
|
249
305
|
end
|
250
306
|
|
251
307
|
def cache_versions!(new_file)
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
processed_parent = SanitizedFile.new :tempfile => self.file,
|
256
|
-
:filename => new_file.original_filename
|
257
|
-
|
258
|
-
active_versions.each do |name, v|
|
259
|
-
next if v.cached?
|
260
|
-
|
261
|
-
v.send(:cache_id=, cache_id)
|
262
|
-
# If option :from_version is present, create cache using cached file from
|
263
|
-
# version indicated
|
264
|
-
if self.class.versions[name][:options] && self.class.versions[name][:options][:from_version]
|
265
|
-
# Maybe the reference version has not been cached yet
|
266
|
-
unless versions[self.class.versions[name][:options][:from_version]].cached?
|
267
|
-
versions[self.class.versions[name][:options][:from_version]].cache!(processed_parent)
|
268
|
-
end
|
269
|
-
processed_version = SanitizedFile.new :tempfile => versions[self.class.versions[name][:options][:from_version]],
|
270
|
-
:filename => new_file.original_filename
|
271
|
-
v.cache!(processed_version)
|
272
|
-
else
|
273
|
-
v.cache!(processed_parent)
|
274
|
-
end
|
308
|
+
versions_to_cache.each do |name, v|
|
309
|
+
v.send(:cache_id=, @cache_id)
|
310
|
+
v.cache!(new_file)
|
275
311
|
end
|
276
312
|
end
|
277
313
|
|
278
|
-
def store_versions!(new_file
|
279
|
-
|
280
|
-
active = Hash[active_versions]
|
281
|
-
versions.each { |v| active[v].try(:store!, new_file) } unless active.empty?
|
282
|
-
else
|
283
|
-
active_versions.each { |name, v| v.store!(new_file) }
|
284
|
-
end
|
314
|
+
def store_versions!(new_file)
|
315
|
+
versions_to_store.each { |name, v| v.store!(new_file) }
|
285
316
|
end
|
286
317
|
|
287
318
|
def remove_versions!
|
@@ -289,11 +320,11 @@ module CarrierWave
|
|
289
320
|
end
|
290
321
|
|
291
322
|
def retrieve_versions_from_cache!(cache_name)
|
292
|
-
|
323
|
+
active_versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
|
293
324
|
end
|
294
325
|
|
295
326
|
def retrieve_versions_from_store!(identifier)
|
296
|
-
|
327
|
+
active_versions.each { |name, v| v.retrieve_from_store!(identifier) }
|
297
328
|
end
|
298
329
|
|
299
330
|
end # Versions
|
data/lib/carrierwave/uploader.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require "carrierwave/uploader/configuration"
|
4
2
|
require "carrierwave/uploader/callbacks"
|
5
3
|
require "carrierwave/uploader/proxy"
|
@@ -11,6 +9,9 @@ require "carrierwave/uploader/download"
|
|
11
9
|
require "carrierwave/uploader/remove"
|
12
10
|
require "carrierwave/uploader/extension_whitelist"
|
13
11
|
require "carrierwave/uploader/extension_blacklist"
|
12
|
+
require "carrierwave/uploader/content_type_whitelist"
|
13
|
+
require "carrierwave/uploader/content_type_blacklist"
|
14
|
+
require "carrierwave/uploader/file_size"
|
14
15
|
require "carrierwave/uploader/processing"
|
15
16
|
require "carrierwave/uploader/versions"
|
16
17
|
require "carrierwave/uploader/default_url"
|
@@ -53,6 +54,9 @@ module CarrierWave
|
|
53
54
|
include CarrierWave::Uploader::Remove
|
54
55
|
include CarrierWave::Uploader::ExtensionWhitelist
|
55
56
|
include CarrierWave::Uploader::ExtensionBlacklist
|
57
|
+
include CarrierWave::Uploader::ContentTypeWhitelist
|
58
|
+
include CarrierWave::Uploader::ContentTypeBlacklist
|
59
|
+
include CarrierWave::Uploader::FileSize
|
56
60
|
include CarrierWave::Uploader::Processing
|
57
61
|
include CarrierWave::Uploader::Versions
|
58
62
|
include CarrierWave::Uploader::DefaultUrl
|