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
data/lib/carrierwave/mounter.rb
CHANGED
|
@@ -3,14 +3,17 @@ module CarrierWave
|
|
|
3
3
|
# this is an internal class, used by CarrierWave::Mount so that
|
|
4
4
|
# we don't pollute the model with a lot of methods.
|
|
5
5
|
class Mounter #:nodoc:
|
|
6
|
-
attr_reader :column, :record, :remote_urls, :
|
|
7
|
-
:
|
|
6
|
+
attr_reader :column, :record, :remote_urls, :integrity_errors,
|
|
7
|
+
:processing_errors, :download_errors
|
|
8
8
|
attr_accessor :remove, :remote_request_headers
|
|
9
9
|
|
|
10
10
|
def initialize(record, column, options={})
|
|
11
11
|
@record = record
|
|
12
12
|
@column = column
|
|
13
13
|
@options = record.class.uploader_options[column]
|
|
14
|
+
@download_errors = []
|
|
15
|
+
@processing_errors = []
|
|
16
|
+
@integrity_errors = []
|
|
14
17
|
end
|
|
15
18
|
|
|
16
19
|
def uploader_class
|
|
@@ -38,21 +41,30 @@ module CarrierWave
|
|
|
38
41
|
end
|
|
39
42
|
|
|
40
43
|
def cache(new_files)
|
|
41
|
-
return if
|
|
44
|
+
return if !new_files.is_a?(Array) && new_files.blank?
|
|
45
|
+
old_uploaders = uploaders
|
|
42
46
|
@uploaders = new_files.map do |new_file|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
handle_error do
|
|
48
|
+
if new_file.is_a?(String)
|
|
49
|
+
if (uploader = old_uploaders.detect { |uploader| uploader.identifier == new_file })
|
|
50
|
+
uploader.staged = true
|
|
51
|
+
uploader
|
|
52
|
+
else
|
|
53
|
+
begin
|
|
54
|
+
uploader = blank_uploader
|
|
55
|
+
uploader.retrieve_from_cache!(new_file)
|
|
56
|
+
uploader
|
|
57
|
+
rescue CarrierWave::InvalidParameter
|
|
58
|
+
nil
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
else
|
|
62
|
+
uploader = blank_uploader
|
|
63
|
+
uploader.cache!(new_file)
|
|
64
|
+
uploader
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end.compact
|
|
56
68
|
end
|
|
57
69
|
|
|
58
70
|
def cache_names
|
|
@@ -60,45 +72,36 @@ module CarrierWave
|
|
|
60
72
|
end
|
|
61
73
|
|
|
62
74
|
def cache_names=(cache_names)
|
|
63
|
-
return if
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
return if cache_names.blank?
|
|
76
|
+
clear_unstaged
|
|
77
|
+
cache_names.map do |cache_name|
|
|
78
|
+
begin
|
|
79
|
+
uploader = blank_uploader
|
|
80
|
+
uploader.retrieve_from_cache!(cache_name)
|
|
81
|
+
@uploaders << uploader
|
|
82
|
+
rescue CarrierWave::InvalidParameter
|
|
83
|
+
# ignore
|
|
84
|
+
end
|
|
68
85
|
end
|
|
69
|
-
rescue CarrierWave::InvalidParameter
|
|
70
86
|
end
|
|
71
87
|
|
|
72
88
|
def remote_urls=(urls)
|
|
73
|
-
return if
|
|
89
|
+
return if urls.blank? || urls.all?(&:blank?)
|
|
74
90
|
|
|
75
91
|
@remote_urls = urls
|
|
76
|
-
@download_error = nil
|
|
77
|
-
@integrity_error = nil
|
|
78
92
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
93
|
+
clear_unstaged
|
|
94
|
+
urls.zip(remote_request_headers || []).map do |url, header|
|
|
95
|
+
handle_error do
|
|
96
|
+
uploader = blank_uploader
|
|
97
|
+
uploader.download!(url, header || {})
|
|
98
|
+
@uploaders << uploader
|
|
99
|
+
end
|
|
83
100
|
end
|
|
84
|
-
|
|
85
|
-
rescue CarrierWave::DownloadError => e
|
|
86
|
-
@download_error = e
|
|
87
|
-
raise e unless option(:ignore_download_errors)
|
|
88
|
-
rescue CarrierWave::ProcessingError => e
|
|
89
|
-
@processing_error = e
|
|
90
|
-
raise e unless option(:ignore_processing_errors)
|
|
91
|
-
rescue CarrierWave::IntegrityError => e
|
|
92
|
-
@integrity_error = e
|
|
93
|
-
raise e unless option(:ignore_integrity_errors)
|
|
94
101
|
end
|
|
95
102
|
|
|
96
103
|
def store!
|
|
97
|
-
|
|
98
|
-
remove!
|
|
99
|
-
else
|
|
100
|
-
uploaders.reject(&:blank?).each(&:store!)
|
|
101
|
-
end
|
|
104
|
+
uploaders.reject(&:blank?).each(&:store!)
|
|
102
105
|
end
|
|
103
106
|
|
|
104
107
|
def urls(*args)
|
|
@@ -118,11 +121,16 @@ module CarrierWave
|
|
|
118
121
|
@uploaders = []
|
|
119
122
|
end
|
|
120
123
|
|
|
124
|
+
def clear!
|
|
125
|
+
@uploaders = []
|
|
126
|
+
end
|
|
127
|
+
|
|
121
128
|
def serialization_column
|
|
122
129
|
option(:mount_on) || column
|
|
123
130
|
end
|
|
124
131
|
|
|
125
132
|
def remove_previous(before=nil, after=nil)
|
|
133
|
+
after ||= []
|
|
126
134
|
return unless before
|
|
127
135
|
|
|
128
136
|
# both 'before' and 'after' can be string when 'mount_on' option is set
|
|
@@ -145,9 +153,7 @@ module CarrierWave
|
|
|
145
153
|
end.path
|
|
146
154
|
end
|
|
147
155
|
before.each do |uploader|
|
|
148
|
-
if uploader.remove_previously_stored_files_after_update
|
|
149
|
-
uploader.remove!
|
|
150
|
-
end
|
|
156
|
+
uploader.remove! if uploader.remove_previously_stored_files_after_update && !after_paths.include?(uploader.path)
|
|
151
157
|
end
|
|
152
158
|
end
|
|
153
159
|
|
|
@@ -160,5 +166,22 @@ module CarrierWave
|
|
|
160
166
|
self.uploader_options[name] ||= record.class.uploader_option(column, name)
|
|
161
167
|
end
|
|
162
168
|
|
|
169
|
+
def clear_unstaged
|
|
170
|
+
@uploaders ||= []
|
|
171
|
+
@uploaders.keep_if(&:staged)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def handle_error
|
|
175
|
+
yield
|
|
176
|
+
rescue CarrierWave::DownloadError => e
|
|
177
|
+
@download_errors << e
|
|
178
|
+
raise e unless option(:ignore_download_errors)
|
|
179
|
+
rescue CarrierWave::ProcessingError => e
|
|
180
|
+
@processing_errors << e
|
|
181
|
+
raise e unless option(:ignore_processing_errors)
|
|
182
|
+
rescue CarrierWave::IntegrityError => e
|
|
183
|
+
@integrity_errors << e
|
|
184
|
+
raise e unless option(:ignore_integrity_errors)
|
|
185
|
+
end
|
|
163
186
|
end # Mounter
|
|
164
187
|
end # CarrierWave
|
|
@@ -12,10 +12,12 @@ module CarrierWave
|
|
|
12
12
|
def mount_uploader(column, uploader=nil, options={}, &block)
|
|
13
13
|
super
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
mod = Module.new
|
|
16
|
+
prepend mod
|
|
17
|
+
mod.class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
16
18
|
def remote_#{column}_url=(url)
|
|
17
19
|
column = _mounter(:#{column}).serialization_column
|
|
18
|
-
|
|
20
|
+
__send__(:"\#{column}_will_change!")
|
|
19
21
|
super
|
|
20
22
|
end
|
|
21
23
|
RUBY
|
|
@@ -27,10 +29,12 @@ module CarrierWave
|
|
|
27
29
|
def mount_uploaders(column, uploader=nil, options={}, &block)
|
|
28
30
|
super
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
mod = Module.new
|
|
33
|
+
prepend mod
|
|
34
|
+
mod.class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
31
35
|
def remote_#{column}_urls=(url)
|
|
32
36
|
column = _mounter(:#{column}).serialization_column
|
|
33
|
-
|
|
37
|
+
__send__(:"\#{column}_will_change!")
|
|
34
38
|
super
|
|
35
39
|
end
|
|
36
40
|
RUBY
|
|
@@ -52,19 +56,20 @@ module CarrierWave
|
|
|
52
56
|
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
|
53
57
|
validates_download_of column if uploader_option(column.to_sym, :validate_download)
|
|
54
58
|
|
|
55
|
-
after_save :"store_#{column}!"
|
|
56
59
|
before_save :"write_#{column}_identifier"
|
|
60
|
+
after_save :"store_previous_changes_for_#{column}"
|
|
57
61
|
after_commit :"remove_#{column}!", :on => :destroy
|
|
58
62
|
after_commit :"mark_remove_#{column}_false", :on => :update
|
|
59
|
-
|
|
60
|
-
after_save :"store_previous_changes_for_#{column}"
|
|
61
63
|
after_commit :"remove_previously_stored_#{column}", :on => :update
|
|
64
|
+
after_commit :"store_#{column}!", :on => [:create, :update]
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
mod = Module.new
|
|
67
|
+
prepend mod
|
|
68
|
+
mod.class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
64
69
|
def #{column}=(new_file)
|
|
65
70
|
column = _mounter(:#{column}).serialization_column
|
|
66
|
-
if !(new_file.blank? &&
|
|
67
|
-
|
|
71
|
+
if !(new_file.blank? && __send__(:#{column}).blank?)
|
|
72
|
+
__send__(:"\#{column}_will_change!")
|
|
68
73
|
end
|
|
69
74
|
|
|
70
75
|
super
|
|
@@ -72,8 +77,9 @@ module CarrierWave
|
|
|
72
77
|
|
|
73
78
|
def remove_#{column}=(value)
|
|
74
79
|
column = _mounter(:#{column}).serialization_column
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
result = super
|
|
81
|
+
__send__(:"\#{column}_will_change!") if _mounter(:#{column}).remove?
|
|
82
|
+
result
|
|
77
83
|
end
|
|
78
84
|
|
|
79
85
|
def remove_#{column}!
|
|
@@ -21,9 +21,11 @@ module CarrierWave
|
|
|
21
21
|
# process :resize_to_fit => [200, 200]
|
|
22
22
|
# end
|
|
23
23
|
#
|
|
24
|
-
# Or create your own helpers with the powerful
|
|
25
|
-
#
|
|
26
|
-
#
|
|
24
|
+
# Or create your own helpers with the powerful minimagick! method, which
|
|
25
|
+
# yields an ImageProcessing::Builder object. Check out the ImageProcessing
|
|
26
|
+
# docs at http://github.com/janko-m/image_processing and the list of all
|
|
27
|
+
# available ImageMagick options at
|
|
28
|
+
# http://www.imagemagick.org/script/command-line-options.php for more info.
|
|
27
29
|
#
|
|
28
30
|
# class MyUploader < CarrierWave::Uploader::Base
|
|
29
31
|
# include CarrierWave::MiniMagick
|
|
@@ -31,23 +33,22 @@ module CarrierWave
|
|
|
31
33
|
# process :radial_blur => 10
|
|
32
34
|
#
|
|
33
35
|
# def radial_blur(amount)
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
36
|
+
# minimagick! do |builder|
|
|
37
|
+
# builder.radial_blur(amount)
|
|
38
|
+
# builder = yield(builder) if block_given?
|
|
39
|
+
# builder
|
|
38
40
|
# end
|
|
39
41
|
# end
|
|
40
42
|
# end
|
|
41
43
|
#
|
|
42
44
|
# === Note
|
|
43
45
|
#
|
|
44
|
-
# MiniMagick
|
|
45
|
-
#
|
|
46
|
+
# The ImageProcessing gem uses MiniMagick, a mini replacement for RMagick
|
|
47
|
+
# that uses ImageMagick command-line tools, to build a "convert" command that
|
|
48
|
+
# performs the processing.
|
|
46
49
|
#
|
|
47
50
|
# You can find more information here:
|
|
48
51
|
#
|
|
49
|
-
# http://mini_magick.rubyforge.org/
|
|
50
|
-
# and
|
|
51
52
|
# https://github.com/minimagick/minimagick/
|
|
52
53
|
#
|
|
53
54
|
#
|
|
@@ -55,12 +56,7 @@ module CarrierWave
|
|
|
55
56
|
extend ActiveSupport::Concern
|
|
56
57
|
|
|
57
58
|
included do
|
|
58
|
-
|
|
59
|
-
require "mini_magick"
|
|
60
|
-
rescue LoadError => e
|
|
61
|
-
e.message << " (You may need to install the mini_magick gem)"
|
|
62
|
-
raise e
|
|
63
|
-
end
|
|
59
|
+
require "image_processing/mini_magick"
|
|
64
60
|
end
|
|
65
61
|
|
|
66
62
|
module ClassMethods
|
|
@@ -102,12 +98,11 @@ module CarrierWave
|
|
|
102
98
|
#
|
|
103
99
|
# image.convert(:png)
|
|
104
100
|
#
|
|
105
|
-
def convert(format, page=nil)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
img
|
|
101
|
+
def convert(format, page=nil, &block)
|
|
102
|
+
minimagick!(block) do |builder|
|
|
103
|
+
builder = builder.convert(format)
|
|
104
|
+
builder = builder.loader(page: page) if page
|
|
105
|
+
builder
|
|
111
106
|
end
|
|
112
107
|
end
|
|
113
108
|
|
|
@@ -121,19 +116,18 @@ module CarrierWave
|
|
|
121
116
|
#
|
|
122
117
|
# [width (Integer)] the width to scale the image to
|
|
123
118
|
# [height (Integer)] the height to scale the image to
|
|
119
|
+
# [combine_options (Hash)] additional ImageMagick options to apply before resizing
|
|
124
120
|
#
|
|
125
121
|
# === Yields
|
|
126
122
|
#
|
|
127
123
|
# [MiniMagick::Image] additional manipulations to perform
|
|
128
124
|
#
|
|
129
|
-
def resize_to_limit(width, height, combine_options: {})
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
img = yield(img) if block_given?
|
|
136
|
-
img
|
|
125
|
+
def resize_to_limit(width, height, combine_options: {}, &block)
|
|
126
|
+
width, height = resolve_dimensions(width, height)
|
|
127
|
+
|
|
128
|
+
minimagick!(block) do |builder|
|
|
129
|
+
builder.resize_to_limit(width, height)
|
|
130
|
+
.apply(combine_options)
|
|
137
131
|
end
|
|
138
132
|
end
|
|
139
133
|
|
|
@@ -146,19 +140,18 @@ module CarrierWave
|
|
|
146
140
|
#
|
|
147
141
|
# [width (Integer)] the width to scale the image to
|
|
148
142
|
# [height (Integer)] the height to scale the image to
|
|
143
|
+
# [combine_options (Hash)] additional ImageMagick options to apply before resizing
|
|
149
144
|
#
|
|
150
145
|
# === Yields
|
|
151
146
|
#
|
|
152
147
|
# [MiniMagick::Image] additional manipulations to perform
|
|
153
148
|
#
|
|
154
|
-
def resize_to_fit(width, height, combine_options: {})
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
img = yield(img) if block_given?
|
|
161
|
-
img
|
|
149
|
+
def resize_to_fit(width, height, combine_options: {}, &block)
|
|
150
|
+
width, height = resolve_dimensions(width, height)
|
|
151
|
+
|
|
152
|
+
minimagick!(block) do |builder|
|
|
153
|
+
builder.resize_to_fit(width, height)
|
|
154
|
+
.apply(combine_options)
|
|
162
155
|
end
|
|
163
156
|
end
|
|
164
157
|
|
|
@@ -172,35 +165,18 @@ module CarrierWave
|
|
|
172
165
|
# [width (Integer)] the width to scale the image to
|
|
173
166
|
# [height (Integer)] the height to scale the image to
|
|
174
167
|
# [gravity (String)] the current gravity suggestion (default: 'Center'; options: 'NorthWest', 'North', 'NorthEast', 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast')
|
|
168
|
+
# [combine_options (Hash)] additional ImageMagick options to apply before resizing
|
|
175
169
|
#
|
|
176
170
|
# === Yields
|
|
177
171
|
#
|
|
178
172
|
# [MiniMagick::Image] additional manipulations to perform
|
|
179
173
|
#
|
|
180
|
-
def resize_to_fill(width, height, gravity = 'Center', combine_options: {})
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
scale_y = height/rows.to_f
|
|
187
|
-
if scale_x >= scale_y
|
|
188
|
-
cols = (scale_x * (cols + 0.5)).round
|
|
189
|
-
rows = (scale_x * (rows + 0.5)).round
|
|
190
|
-
cmd.resize "#{cols}"
|
|
191
|
-
else
|
|
192
|
-
cols = (scale_y * (cols + 0.5)).round
|
|
193
|
-
rows = (scale_y * (rows + 0.5)).round
|
|
194
|
-
cmd.resize "x#{rows}"
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
cmd.gravity gravity
|
|
198
|
-
cmd.background "rgba(255,255,255,0.0)"
|
|
199
|
-
cmd.extent "#{width}x#{height}" if cols != width || rows != height
|
|
200
|
-
append_combine_options cmd, combine_options
|
|
201
|
-
end
|
|
202
|
-
img = yield(img) if block_given?
|
|
203
|
-
img
|
|
174
|
+
def resize_to_fill(width, height, gravity = 'Center', combine_options: {}, &block)
|
|
175
|
+
width, height = resolve_dimensions(width, height)
|
|
176
|
+
|
|
177
|
+
minimagick!(block) do |builder|
|
|
178
|
+
builder.resize_to_fill(width, height, gravity: gravity)
|
|
179
|
+
.apply(combine_options)
|
|
204
180
|
end
|
|
205
181
|
end
|
|
206
182
|
|
|
@@ -219,26 +195,18 @@ module CarrierWave
|
|
|
219
195
|
# [height (Integer)] the height to scale the image to
|
|
220
196
|
# [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
|
|
221
197
|
# [gravity (String)] how to position the image
|
|
198
|
+
# [combine_options (Hash)] additional ImageMagick options to apply before resizing
|
|
222
199
|
#
|
|
223
200
|
# === Yields
|
|
224
201
|
#
|
|
225
202
|
# [MiniMagick::Image] additional manipulations to perform
|
|
226
203
|
#
|
|
227
|
-
def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {})
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
else
|
|
234
|
-
cmd.background background
|
|
235
|
-
end
|
|
236
|
-
cmd.gravity gravity
|
|
237
|
-
cmd.extent "#{width}x#{height}"
|
|
238
|
-
append_combine_options cmd, combine_options
|
|
239
|
-
end
|
|
240
|
-
img = yield(img) if block_given?
|
|
241
|
-
img
|
|
204
|
+
def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {}, &block)
|
|
205
|
+
width, height = resolve_dimensions(width, height)
|
|
206
|
+
|
|
207
|
+
minimagick!(block) do |builder|
|
|
208
|
+
builder.resize_and_pad(width, height, background: background, gravity: gravity)
|
|
209
|
+
.apply(combine_options)
|
|
242
210
|
end
|
|
243
211
|
end
|
|
244
212
|
|
|
@@ -269,6 +237,9 @@ module CarrierWave
|
|
|
269
237
|
# and then pass each of its frames to the supplied block. It will then
|
|
270
238
|
# save the image to disk.
|
|
271
239
|
#
|
|
240
|
+
# NOTE: This method exists mostly for backwards compatibility, you should
|
|
241
|
+
# probably use #minimagick!.
|
|
242
|
+
#
|
|
272
243
|
# === Gotcha
|
|
273
244
|
#
|
|
274
245
|
# This method assumes that the object responds to +current_path+.
|
|
@@ -288,40 +259,72 @@ module CarrierWave
|
|
|
288
259
|
cache_stored_file! if !cached?
|
|
289
260
|
image = ::MiniMagick::Image.open(current_path)
|
|
290
261
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
image = yield(image)
|
|
294
|
-
image.write(current_path)
|
|
262
|
+
image = yield(image)
|
|
263
|
+
FileUtils.mv image.path, current_path
|
|
295
264
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
265
|
+
image.run_command("identify", current_path)
|
|
266
|
+
rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
|
|
267
|
+
message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e)
|
|
268
|
+
raise CarrierWave::ProcessingError, message
|
|
269
|
+
ensure
|
|
270
|
+
image.destroy! if image
|
|
271
|
+
end
|
|
300
272
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
273
|
+
# Process the image with MiniMagick, using the ImageProcessing gem. This
|
|
274
|
+
# method will build a "convert" ImageMagick command and execute it on the
|
|
275
|
+
# current image.
|
|
276
|
+
#
|
|
277
|
+
# === Gotcha
|
|
278
|
+
#
|
|
279
|
+
# This method assumes that the object responds to +current_path+.
|
|
280
|
+
# Any class that this module is mixed into must have a +current_path+ method.
|
|
281
|
+
# CarrierWave::Uploader does, so you won't need to worry about this in
|
|
282
|
+
# most cases.
|
|
283
|
+
#
|
|
284
|
+
# === Yields
|
|
285
|
+
#
|
|
286
|
+
# [ImageProcessing::Builder] use it to define processing to be performed
|
|
287
|
+
#
|
|
288
|
+
# === Raises
|
|
289
|
+
#
|
|
290
|
+
# [CarrierWave::ProcessingError] if processing failed.
|
|
291
|
+
def minimagick!(block = nil)
|
|
292
|
+
builder = ImageProcessing::MiniMagick.source(current_path)
|
|
293
|
+
builder = yield(builder)
|
|
294
|
+
|
|
295
|
+
result = builder.call
|
|
296
|
+
result.close
|
|
297
|
+
|
|
298
|
+
# backwards compatibility (we want to eventually move away from MiniMagick::Image)
|
|
299
|
+
if block
|
|
300
|
+
image = MiniMagick::Image.new(result.path, result)
|
|
301
|
+
image = block.call(image)
|
|
302
|
+
result = image.instance_variable_get(:@tempfile)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
FileUtils.mv result.path, current_path
|
|
306
|
+
|
|
307
|
+
if File.extname(result.path) != File.extname(current_path)
|
|
308
|
+
move_to = current_path.chomp(File.extname(current_path)) + File.extname(result.path)
|
|
309
|
+
file.content_type = ::MiniMime.lookup_by_filename(move_to).content_type
|
|
310
|
+
file.move_to(move_to, permissions, directory_permissions)
|
|
304
311
|
end
|
|
305
312
|
rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
|
|
306
|
-
|
|
307
|
-
message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :default => default)
|
|
313
|
+
message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e)
|
|
308
314
|
raise CarrierWave::ProcessingError, message
|
|
309
315
|
end
|
|
310
316
|
|
|
311
317
|
private
|
|
312
318
|
|
|
313
|
-
def
|
|
314
|
-
|
|
315
|
-
|
|
319
|
+
def resolve_dimensions(*dimensions)
|
|
320
|
+
dimensions.map do |value|
|
|
321
|
+
next value unless value.instance_of?(Proc)
|
|
322
|
+
value.arity >= 1 ? value.call(self) : value.call
|
|
316
323
|
end
|
|
317
324
|
end
|
|
318
325
|
|
|
319
326
|
def mini_magick_image
|
|
320
|
-
|
|
321
|
-
::MiniMagick::Image.open(url)
|
|
322
|
-
else
|
|
323
|
-
::MiniMagick::Image.open(current_path)
|
|
324
|
-
end
|
|
327
|
+
::MiniMagick::Image.read(read)
|
|
325
328
|
end
|
|
326
329
|
|
|
327
330
|
end # MiniMagick
|
|
@@ -67,6 +67,13 @@ module CarrierWave
|
|
|
67
67
|
e.message << " (You may need to install the rmagick gem)"
|
|
68
68
|
raise e
|
|
69
69
|
end
|
|
70
|
+
|
|
71
|
+
prepend Module.new {
|
|
72
|
+
def initialize(*)
|
|
73
|
+
super
|
|
74
|
+
@format = nil
|
|
75
|
+
end
|
|
76
|
+
}
|
|
70
77
|
end
|
|
71
78
|
|
|
72
79
|
module ClassMethods
|
|
@@ -133,6 +140,8 @@ module CarrierWave
|
|
|
133
140
|
# [Magick::Image] additional manipulations to perform
|
|
134
141
|
#
|
|
135
142
|
def resize_to_limit(width, height)
|
|
143
|
+
width = dimension_from width
|
|
144
|
+
height = dimension_from height
|
|
136
145
|
manipulate! do |img|
|
|
137
146
|
geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
|
|
138
147
|
new_img = img.change_geometry(geometry) do |new_width, new_height|
|
|
@@ -162,6 +171,8 @@ module CarrierWave
|
|
|
162
171
|
# [Magick::Image] additional manipulations to perform
|
|
163
172
|
#
|
|
164
173
|
def resize_to_fit(width, height)
|
|
174
|
+
width = dimension_from width
|
|
175
|
+
height = dimension_from height
|
|
165
176
|
manipulate! do |img|
|
|
166
177
|
img.resize_to_fit!(width, height)
|
|
167
178
|
img = yield(img) if block_given?
|
|
@@ -186,6 +197,8 @@ module CarrierWave
|
|
|
186
197
|
# [Magick::Image] additional manipulations to perform
|
|
187
198
|
#
|
|
188
199
|
def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
|
|
200
|
+
width = dimension_from width
|
|
201
|
+
height = dimension_from height
|
|
189
202
|
manipulate! do |img|
|
|
190
203
|
img.crop_resized!(width, height, gravity)
|
|
191
204
|
img = yield(img) if block_given?
|
|
@@ -211,6 +224,8 @@ module CarrierWave
|
|
|
211
224
|
# [Magick::Image] additional manipulations to perform
|
|
212
225
|
#
|
|
213
226
|
def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
|
|
227
|
+
width = dimension_from width
|
|
228
|
+
height = dimension_from height
|
|
214
229
|
manipulate! do |img|
|
|
215
230
|
img.resize_to_fit!(width, height)
|
|
216
231
|
new_img = ::Magick::Image.new(width, height) { self.background_color = background == :transparent ? 'rgba(255,255,255,0)' : background.to_s }
|
|
@@ -338,7 +353,7 @@ module CarrierWave
|
|
|
338
353
|
frames = ::Magick::ImageList.new
|
|
339
354
|
|
|
340
355
|
image.each_with_index do |frame, index|
|
|
341
|
-
frame = yield
|
|
356
|
+
frame = yield(*[frame, index, options].take(block.arity)) if block_given?
|
|
342
357
|
frames << frame if frame
|
|
343
358
|
end
|
|
344
359
|
frames.append(true) if block_given?
|
|
@@ -348,6 +363,7 @@ module CarrierWave
|
|
|
348
363
|
if options[:format] || @format
|
|
349
364
|
frames.write("#{options[:format] || @format}:#{current_path}", &write_block)
|
|
350
365
|
move_to = current_path.chomp(File.extname(current_path)) + ".#{options[:format] || @format}"
|
|
366
|
+
file.content_type = ::MiniMime.lookup_by_filename(move_to).content_type
|
|
351
367
|
file.move_to(move_to, permissions, directory_permissions)
|
|
352
368
|
else
|
|
353
369
|
frames.write(current_path, &write_block)
|
|
@@ -355,7 +371,7 @@ module CarrierWave
|
|
|
355
371
|
|
|
356
372
|
destroy_image(frames)
|
|
357
373
|
rescue ::Magick::ImageMagickError => e
|
|
358
|
-
raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.rmagick_processing_error", :e => e
|
|
374
|
+
raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.rmagick_processing_error", :e => e)
|
|
359
375
|
end
|
|
360
376
|
|
|
361
377
|
private
|
|
@@ -371,8 +387,13 @@ module CarrierWave
|
|
|
371
387
|
image.try(:destroy!)
|
|
372
388
|
end
|
|
373
389
|
|
|
390
|
+
def dimension_from(value)
|
|
391
|
+
return value unless value.instance_of?(Proc)
|
|
392
|
+
value.arity >= 1 ? value.call(self) : value.call
|
|
393
|
+
end
|
|
394
|
+
|
|
374
395
|
def rmagick_image
|
|
375
|
-
::Magick::Image.read
|
|
396
|
+
::Magick::Image.from_blob(self.read).first
|
|
376
397
|
end
|
|
377
398
|
|
|
378
399
|
end # RMagick
|