carrierwave-pressplane 0.5.8.3

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.
Files changed (36) hide show
  1. data/README.md +748 -0
  2. data/lib/carrierwave.rb +116 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/locale/en.yml +9 -0
  5. data/lib/carrierwave/mount.rb +382 -0
  6. data/lib/carrierwave/orm/activerecord.rb +46 -0
  7. data/lib/carrierwave/processing/mime_types.rb +58 -0
  8. data/lib/carrierwave/processing/mini_magick.rb +252 -0
  9. data/lib/carrierwave/processing/rmagick.rb +279 -0
  10. data/lib/carrierwave/sanitized_file.rb +315 -0
  11. data/lib/carrierwave/storage/abstract.rb +30 -0
  12. data/lib/carrierwave/storage/cloud_files.rb +188 -0
  13. data/lib/carrierwave/storage/file.rb +56 -0
  14. data/lib/carrierwave/storage/fog.rb +333 -0
  15. data/lib/carrierwave/storage/right_s3.rb +1 -0
  16. data/lib/carrierwave/storage/s3.rb +240 -0
  17. data/lib/carrierwave/test/matchers.rb +241 -0
  18. data/lib/carrierwave/uploader.rb +44 -0
  19. data/lib/carrierwave/uploader/cache.rb +178 -0
  20. data/lib/carrierwave/uploader/callbacks.rb +35 -0
  21. data/lib/carrierwave/uploader/configuration.rb +168 -0
  22. data/lib/carrierwave/uploader/default_url.rb +19 -0
  23. data/lib/carrierwave/uploader/download.rb +75 -0
  24. data/lib/carrierwave/uploader/extension_whitelist.rb +49 -0
  25. data/lib/carrierwave/uploader/mountable.rb +39 -0
  26. data/lib/carrierwave/uploader/processing.rb +90 -0
  27. data/lib/carrierwave/uploader/proxy.rb +77 -0
  28. data/lib/carrierwave/uploader/remove.rb +23 -0
  29. data/lib/carrierwave/uploader/store.rb +113 -0
  30. data/lib/carrierwave/uploader/url.rb +47 -0
  31. data/lib/carrierwave/uploader/versions.rb +237 -0
  32. data/lib/carrierwave/validations/active_model.rb +64 -0
  33. data/lib/carrierwave/version.rb +3 -0
  34. data/lib/generators/templates/uploader.rb +48 -0
  35. data/lib/generators/uploader_generator.rb +7 -0
  36. metadata +215 -0
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_record'
4
+ require 'carrierwave/validations/active_model'
5
+
6
+ module CarrierWave
7
+ module ActiveRecord
8
+
9
+ include CarrierWave::Mount
10
+
11
+ ##
12
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
13
+ #
14
+ def mount_uploader(column, uploader=nil, options={}, &block)
15
+ super
16
+
17
+ alias_method :read_uploader, :read_attribute
18
+ alias_method :write_uploader, :write_attribute
19
+ public :read_uploader
20
+ public :write_uploader
21
+
22
+ include CarrierWave::Validations::ActiveModel
23
+
24
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
25
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
26
+
27
+ after_save :"store_#{column}!"
28
+ before_save :"write_#{column}_identifier"
29
+ after_destroy :"remove_#{column}!"
30
+ before_update :"store_previous_model_for_#{column}"
31
+ after_save :"remove_previously_stored_#{column}"
32
+
33
+ class_eval <<-RUBY, __FILE__, __LINE__+1
34
+ def #{column}=(new_file)
35
+ column = _mounter(:#{column}).serialization_column
36
+ send(:"\#{column}_will_change!")
37
+ super
38
+ end
39
+ RUBY
40
+
41
+ end
42
+
43
+ end # ActiveRecord
44
+ end # CarrierWave
45
+
46
+ ActiveRecord::Base.extend CarrierWave::ActiveRecord
@@ -0,0 +1,58 @@
1
+ require 'mime/types'
2
+
3
+ module CarrierWave
4
+
5
+ ##
6
+ # This module simplifies the use of the mime-types gem to intelligently
7
+ # guess and set the content-type of a file. If you want to use this, you'll
8
+ # need to require this file:
9
+ #
10
+ # require 'carrierwave/processing/mime_types'
11
+ #
12
+ # And then include it in your uploader:
13
+ #
14
+ # class MyUploader < CarrierWave::Uploader::Base
15
+ # include CarrierWave::MimeTypes
16
+ # end
17
+ #
18
+ # You can now use the provided helper:
19
+ #
20
+ # class MyUploader < CarrierWave::Uploader::Base
21
+ # include CarrierWave::MimeTypes
22
+ #
23
+ # process :set_content_type
24
+ # end
25
+ #
26
+ module MimeTypes
27
+ extend ActiveSupport::Concern
28
+
29
+ module ClassMethods
30
+ def set_content_type(override=false)
31
+ process :set_content_type => override
32
+ end
33
+ end
34
+
35
+ ##
36
+ # Changes the file content_type using the mime-types gem
37
+ #
38
+ # === Parameters
39
+ #
40
+ # [override (Boolean)] whether or not to override the file's content_type
41
+ # if it is already set and not a generic content-type,
42
+ # false by default
43
+ #
44
+ def set_content_type(override=false)
45
+ if override || file.content_type.blank? || file.content_type == 'application/octet-stream'
46
+ new_content_type = ::MIME::Types.type_for(file.original_filename).first.to_s
47
+ if file.respond_to?(:content_type=)
48
+ file.content_type = new_content_type
49
+ else
50
+ file.set_instance_variable(:@content_type, new_content_type)
51
+ end
52
+ end
53
+ rescue ::MIME::InvalidContentType => e
54
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.mime_types_processing_error", :e => e)
55
+ end
56
+
57
+ end # MimeTypes
58
+ end # CarrierWave
@@ -0,0 +1,252 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mini_magick'
4
+
5
+ module CarrierWave
6
+
7
+ ##
8
+ # This module simplifies manipulation with MiniMagick by providing a set
9
+ # of convenient helper methods. If you want to use them, you'll need to
10
+ # require this file:
11
+ #
12
+ # require 'carrierwave/processing/mini_magick'
13
+ #
14
+ # And then include it in your uploader:
15
+ #
16
+ # class MyUploader < CarrierWave::Uploader::Base
17
+ # include CarrierWave::MiniMagick
18
+ # end
19
+ #
20
+ # You can now use the provided helpers:
21
+ #
22
+ # class MyUploader < CarrierWave::Uploader::Base
23
+ # include CarrierWave::MiniMagick
24
+ #
25
+ # process :resize_to_fit => [200, 200]
26
+ # end
27
+ #
28
+ # Or create your own helpers with the powerful manipulate! method. Check
29
+ # out the ImageMagick docs at http://www.imagemagick.org/script/command-line-options.php for more
30
+ # info
31
+ #
32
+ # class MyUploader < CarrierWave::Uploader::Base
33
+ # include CarrierWave::MiniMagick
34
+ #
35
+ # process :radial_blur => 10
36
+ #
37
+ # def radial_blur(amount)
38
+ # manipulate! do |img|
39
+ # img.radial_blur(amount)
40
+ # img = yield(img) if block_given?
41
+ # img
42
+ # end
43
+ # end
44
+ #
45
+ # === Note
46
+ #
47
+ # MiniMagick is a mini replacement for RMagick that uses the command line
48
+ # tool "mogrify" for image manipulation.
49
+ #
50
+ # You can find more information here:
51
+ #
52
+ # http://mini_magick.rubyforge.org/
53
+ # and
54
+ # http://github.com/probablycorey/mini_magick/
55
+ #
56
+ #
57
+ module MiniMagick
58
+ extend ActiveSupport::Concern
59
+
60
+ module ClassMethods
61
+ def convert(format)
62
+ process :convert => format
63
+ end
64
+
65
+ def resize_to_limit(width, height)
66
+ process :resize_to_limit => [width, height]
67
+ end
68
+
69
+ def resize_to_fit(width, height)
70
+ process :resize_to_fit => [width, height]
71
+ end
72
+
73
+ def resize_to_fill(width, height)
74
+ process :resize_to_fill => [width, height]
75
+ end
76
+
77
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
78
+ process :resize_and_pad => [width, height, background, gravity]
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Changes the image encoding format to the given format
84
+ #
85
+ # See http://www.imagemagick.org/script/command-line-options.php#format
86
+ #
87
+ # === Parameters
88
+ #
89
+ # [format (#to_s)] an abreviation of the format
90
+ #
91
+ # === Yields
92
+ #
93
+ # [MiniMagick::Image] additional manipulations to perform
94
+ #
95
+ # === Examples
96
+ #
97
+ # image.convert(:png)
98
+ #
99
+ def convert(format)
100
+ manipulate! do |img|
101
+ img.format(format.to_s.downcase)
102
+ img = yield(img) if block_given?
103
+ img
104
+ end
105
+ end
106
+
107
+ ##
108
+ # Resize the image to fit within the specified dimensions while retaining
109
+ # the original aspect ratio. Will only resize the image if it is larger than the
110
+ # specified dimensions. The resulting image may be shorter or narrower than specified
111
+ # in the smaller dimension but will not be larger than the specified values.
112
+ #
113
+ # === Parameters
114
+ #
115
+ # [width (Integer)] the width to scale the image to
116
+ # [height (Integer)] the height to scale the image to
117
+ #
118
+ # === Yields
119
+ #
120
+ # [MiniMagick::Image] additional manipulations to perform
121
+ #
122
+ def resize_to_limit(width, height)
123
+ manipulate! do |img|
124
+ img.resize "#{width}x#{height}>"
125
+ img = yield(img) if block_given?
126
+ img
127
+ end
128
+ end
129
+
130
+ ##
131
+ # Resize the image to fit within the specified dimensions while retaining
132
+ # the original aspect ratio. The image may be shorter or narrower than
133
+ # specified in the smaller dimension but will not be larger than the specified values.
134
+ #
135
+ # === Parameters
136
+ #
137
+ # [width (Integer)] the width to scale the image to
138
+ # [height (Integer)] the height to scale the image to
139
+ #
140
+ # === Yields
141
+ #
142
+ # [MiniMagick::Image] additional manipulations to perform
143
+ #
144
+ def resize_to_fit(width, height)
145
+ manipulate! do |img|
146
+ img.resize "#{width}x#{height}"
147
+ img = yield(img) if block_given?
148
+ img
149
+ end
150
+ end
151
+
152
+ ##
153
+ # Resize the image to fit within the specified dimensions while retaining
154
+ # the aspect ratio of the original image. If necessary, crop the image in the
155
+ # larger dimension.
156
+ #
157
+ # === Parameters
158
+ #
159
+ # [width (Integer)] the width to scale the image to
160
+ # [height (Integer)] the height to scale the image to
161
+ #
162
+ # === Yields
163
+ #
164
+ # [MiniMagick::Image] additional manipulations to perform
165
+ #
166
+ def resize_to_fill(width, height, gravity = 'Center')
167
+ manipulate! do |img|
168
+ cols, rows = img[:dimensions]
169
+ img.combine_options do |cmd|
170
+ if width != cols || height != rows
171
+ scale = [width/cols.to_f, height/rows.to_f].max
172
+ cols = (scale * (cols + 0.5)).round
173
+ rows = (scale * (rows + 0.5)).round
174
+ cmd.resize "#{cols}x#{rows}"
175
+ end
176
+ cmd.gravity gravity
177
+ cmd.extent "#{width}x#{height}" if cols != width || rows != height
178
+ end
179
+ img = yield(img) if block_given?
180
+ img
181
+ end
182
+ end
183
+
184
+ ##
185
+ # Resize the image to fit within the specified dimensions while retaining
186
+ # the original aspect ratio. If necessary, will pad the remaining area
187
+ # with the given color, which defaults to transparent (for gif and png,
188
+ # white for jpeg).
189
+ #
190
+ # See http://www.imagemagick.org/script/command-line-options.php#gravity
191
+ # for gravity options.
192
+ #
193
+ # === Parameters
194
+ #
195
+ # [width (Integer)] the width to scale the image to
196
+ # [height (Integer)] the height to scale the image to
197
+ # [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
198
+ # [gravity (String)] how to position the image
199
+ #
200
+ # === Yields
201
+ #
202
+ # [MiniMagick::Image] additional manipulations to perform
203
+ #
204
+ def resize_and_pad(width, height, background=:transparent, gravity='Center')
205
+ manipulate! do |img|
206
+ img.combine_options do |cmd|
207
+ cmd.thumbnail "#{width}x#{height}>"
208
+ if background == :transparent
209
+ cmd.background "rgba(0, 0, 0, 0.0)"
210
+ else
211
+ cmd.background background
212
+ end
213
+ cmd.gravity gravity
214
+ cmd.extent "#{width}x#{height}"
215
+ end
216
+ img = yield(img) if block_given?
217
+ img
218
+ end
219
+ end
220
+
221
+ ##
222
+ # Manipulate the image with MiniMagick. This method will load up an image
223
+ # and then pass each of its frames to the supplied block. It will then
224
+ # save the image to disk.
225
+ #
226
+ # === Gotcha
227
+ #
228
+ # This method assumes that the object responds to +current_path+.
229
+ # Any class that this module is mixed into must have a +current_path+ method.
230
+ # CarrierWave::Uploader does, so you won't need to worry about this in
231
+ # most cases.
232
+ #
233
+ # === Yields
234
+ #
235
+ # [MiniMagick::Image] manipulations to perform
236
+ #
237
+ # === Raises
238
+ #
239
+ # [CarrierWave::ProcessingError] if manipulation failed.
240
+ #
241
+ def manipulate!
242
+ cache_stored_file! if !cached?
243
+ image = ::MiniMagick::Image.open(current_path)
244
+ image = yield(image)
245
+ image.write(current_path)
246
+ ::MiniMagick::Image.open(current_path)
247
+ rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
248
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e)
249
+ end
250
+
251
+ end # MiniMagick
252
+ end # CarrierWave
@@ -0,0 +1,279 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined? Magick
4
+ begin
5
+ require 'rmagick'
6
+ rescue LoadError
7
+ require 'RMagick'
8
+ rescue LoadError
9
+ puts "WARNING: Failed to require rmagick, image processing may fail!"
10
+ end
11
+ end
12
+
13
+ module CarrierWave
14
+
15
+ ##
16
+ # This module simplifies manipulation with RMagick by providing a set
17
+ # of convenient helper methods. If you want to use them, you'll need to
18
+ # require this file:
19
+ #
20
+ # require 'carrierwave/processing/rmagick'
21
+ #
22
+ # And then include it in your uploader:
23
+ #
24
+ # class MyUploader < CarrierWave::Uploader::Base
25
+ # include CarrierWave::RMagick
26
+ # end
27
+ #
28
+ # You can now use the provided helpers:
29
+ #
30
+ # class MyUploader < CarrierWave::Uploader::Base
31
+ # include CarrierWave::RMagick
32
+ #
33
+ # process :resize_to_fit => [200, 200]
34
+ # end
35
+ #
36
+ # Or create your own helpers with the powerful manipulate! method. Check
37
+ # out the RMagick docs at http://www.imagemagick.org/RMagick/doc/ for more
38
+ # info
39
+ #
40
+ # class MyUploader < CarrierWave::Uploader::Base
41
+ # include CarrierWave::RMagick
42
+ #
43
+ # process :do_stuff => 10.0
44
+ #
45
+ # def do_stuff(blur_factor)
46
+ # manipulate! do |img|
47
+ # img = img.sepiatone
48
+ # img = img.auto_orient
49
+ # img = img.radial_blur(blur_factor)
50
+ # end
51
+ # end
52
+ # end
53
+ #
54
+ # === Note
55
+ #
56
+ # You should be aware how RMagick handles memory. manipulate! takes care
57
+ # of freeing up memory for you, but for optimum memory usage you should
58
+ # use destructive operations as much as possible:
59
+ #
60
+ # DON'T DO THIS:
61
+ # img = img.resize_to_fit
62
+ #
63
+ # DO THIS INSTEAD:
64
+ # img.resize_to_fit!
65
+ #
66
+ # Read this for more information why:
67
+ #
68
+ # http://rubyforge.org/forum/forum.php?thread_id=1374&forum_id=1618
69
+ #
70
+ module RMagick
71
+ extend ActiveSupport::Concern
72
+
73
+ module ClassMethods
74
+ def convert(format)
75
+ process :convert => format
76
+ end
77
+
78
+ def resize_to_limit(width, height)
79
+ process :resize_to_limit => [width, height]
80
+ end
81
+
82
+ def resize_to_fit(width, height)
83
+ process :resize_to_fit => [width, height]
84
+ end
85
+
86
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
87
+ process :resize_to_fill => [width, height, gravity]
88
+ end
89
+
90
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
91
+ process :resize_and_pad => [width, height, background, gravity]
92
+ end
93
+ end
94
+
95
+ ##
96
+ # Changes the image encoding format to the given format
97
+ #
98
+ # See even http://www.imagemagick.org/RMagick/doc/magick.html#formats
99
+ #
100
+ # === Parameters
101
+ #
102
+ # [format (#to_s)] an abreviation of the format
103
+ #
104
+ # === Yields
105
+ #
106
+ # [Magick::Image] additional manipulations to perform
107
+ #
108
+ # === Examples
109
+ #
110
+ # image.convert(:png)
111
+ #
112
+ def convert(format)
113
+ manipulate!(:format => format)
114
+ end
115
+
116
+ ##
117
+ # Resize the image to fit within the specified dimensions while retaining
118
+ # the original aspect ratio. Will only resize the image if it is larger than the
119
+ # specified dimensions. The resulting image may be shorter or narrower than specified
120
+ # in the smaller dimension but will not be larger than the specified values.
121
+ #
122
+ # === Parameters
123
+ #
124
+ # [width (Integer)] the width to scale the image to
125
+ # [height (Integer)] the height to scale the image to
126
+ #
127
+ # === Yields
128
+ #
129
+ # [Magick::Image] additional manipulations to perform
130
+ #
131
+ def resize_to_limit(width, height)
132
+ manipulate! do |img|
133
+ geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
134
+ new_img = img.change_geometry(geometry) do |new_width, new_height|
135
+ img.resize(new_width, new_height)
136
+ end
137
+ destroy_image(img)
138
+ new_img = yield(new_img) if block_given?
139
+ new_img
140
+ end
141
+ end
142
+
143
+ ##
144
+ # From the RMagick documentation: "Resize the image to fit within the
145
+ # specified dimensions while retaining the original aspect ratio. The
146
+ # image may be shorter or narrower than specified in the smaller dimension
147
+ # but will not be larger than the specified values."
148
+ #
149
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
150
+ #
151
+ # === Parameters
152
+ #
153
+ # [width (Integer)] the width to scale the image to
154
+ # [height (Integer)] the height to scale the image to
155
+ #
156
+ # === Yields
157
+ #
158
+ # [Magick::Image] additional manipulations to perform
159
+ #
160
+ def resize_to_fit(width, height)
161
+ manipulate! do |img|
162
+ img.resize_to_fit!(width, height)
163
+ img = yield(img) if block_given?
164
+ img
165
+ end
166
+ end
167
+
168
+ ##
169
+ # From the RMagick documentation: "Resize the image to fit within the
170
+ # specified dimensions while retaining the aspect ratio of the original
171
+ # image. If necessary, crop the image in the larger dimension."
172
+ #
173
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
174
+ #
175
+ # === Parameters
176
+ #
177
+ # [width (Integer)] the width to scale the image to
178
+ # [height (Integer)] the height to scale the image to
179
+ #
180
+ # === Yields
181
+ #
182
+ # [Magick::Image] additional manipulations to perform
183
+ #
184
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
185
+ manipulate! do |img|
186
+ img.crop_resized!(width, height, gravity)
187
+ img = yield(img) if block_given?
188
+ img
189
+ end
190
+ end
191
+
192
+ ##
193
+ # Resize the image to fit within the specified dimensions while retaining
194
+ # the original aspect ratio. If necessary, will pad the remaining area
195
+ # with the given color, which defaults to transparent (for gif and png,
196
+ # white for jpeg).
197
+ #
198
+ # === Parameters
199
+ #
200
+ # [width (Integer)] the width to scale the image to
201
+ # [height (Integer)] the height to scale the image to
202
+ # [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
203
+ # [gravity (Magick::GravityType)] how to position the image
204
+ #
205
+ # === Yields
206
+ #
207
+ # [Magick::Image] additional manipulations to perform
208
+ #
209
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
210
+ manipulate! do |img|
211
+ img.resize_to_fit!(width, height)
212
+ new_img = ::Magick::Image.new(width, height)
213
+ if background == :transparent
214
+ filled = new_img.matte_floodfill(1, 1)
215
+ else
216
+ filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background))
217
+ end
218
+ destroy_image(new_img)
219
+ filled.composite!(img, gravity, ::Magick::OverCompositeOp)
220
+ destroy_image(img)
221
+ filled = yield(filled) if block_given?
222
+ filled
223
+ end
224
+ end
225
+
226
+ ##
227
+ # Manipulate the image with RMagick. This method will load up an image
228
+ # and then pass each of its frames to the supplied block. It will then
229
+ # save the image to disk.
230
+ #
231
+ # === Gotcha
232
+ #
233
+ # This method assumes that the object responds to +current_path+.
234
+ # Any class that this module is mixed into must have a +current_path+ method.
235
+ # CarrierWave::Uploader does, so you won't need to worry about this in
236
+ # most cases.
237
+ #
238
+ # === Yields
239
+ #
240
+ # [Magick::Image] manipulations to perform
241
+ #
242
+ # === Raises
243
+ #
244
+ # [CarrierWave::ProcessingError] if manipulation failed.
245
+ #
246
+ def manipulate!(options={})
247
+ cache_stored_file! if !cached?
248
+ image = ::Magick::Image.read(current_path)
249
+
250
+ frames = if image.size > 1
251
+ list = ::Magick::ImageList.new
252
+ image.each do |frame|
253
+ list << yield( frame )
254
+ end
255
+ list
256
+ else
257
+ frame = image.first
258
+ frame = yield( frame ) if block_given?
259
+ frame
260
+ end
261
+
262
+ if options[:format]
263
+ frames.write("#{options[:format]}:#{current_path}")
264
+ else
265
+ frames.write(current_path)
266
+ end
267
+ destroy_image(frames)
268
+ rescue ::Magick::ImageMagickError => e
269
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.rmagick_processing_error", :e => e)
270
+ end
271
+
272
+ private
273
+
274
+ def destroy_image(image)
275
+ image.destroy! if image.respond_to?(:destroy!)
276
+ end
277
+
278
+ end # RMagick
279
+ end # CarrierWave