carrierwave 0.4.1 → 0.4.2

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.

@@ -1,4 +1,11 @@
1
- === Version 0.4.1
1
+ === Version 0.4.2 2009-11-26
2
+
3
+ * [added] RightAWS as an alternative S3 implementation
4
+ * [added] An option to enable/disable processing for tests
5
+ * [added] Mongoid ORM support
6
+ * [fixed] DataMapper now works both with and without dm-validations
7
+
8
+ === Version 0.4.1 2009-10-26
2
9
 
3
10
  * [changed] Major changes to the ImageScience module, it actually works now!
4
11
  * [fixed] Bug in configuration where it complais that it can't dup Symbol
@@ -36,14 +36,17 @@ lib/carrierwave/core_ext/module_setup.rb
36
36
  lib/carrierwave/mount.rb
37
37
  lib/carrierwave/orm/activerecord.rb
38
38
  lib/carrierwave/orm/datamapper.rb
39
+ lib/carrierwave/orm/mongoid.rb
39
40
  lib/carrierwave/orm/mongomapper.rb
40
41
  lib/carrierwave/orm/sequel.rb
41
42
  lib/carrierwave/processing/image_science.rb
43
+ lib/carrierwave/processing/mini_magick.rb
42
44
  lib/carrierwave/processing/rmagick.rb
43
45
  lib/carrierwave/sanitized_file.rb
44
46
  lib/carrierwave/storage/abstract.rb
45
47
  lib/carrierwave/storage/file.rb
46
48
  lib/carrierwave/storage/grid_fs.rb
49
+ lib/carrierwave/storage/right_s3.rb
47
50
  lib/carrierwave/storage/s3.rb
48
51
  lib/carrierwave/test/matchers.rb
49
52
  lib/carrierwave/uploader.rb
@@ -68,16 +71,23 @@ script/destroy
68
71
  script/generate
69
72
  spec/compatibility/paperclip_spec.rb
70
73
  spec/fixtures/bork.txt
74
+ spec/fixtures/landscape.jpg
75
+ spec/fixtures/portrait.jpg
71
76
  spec/fixtures/test.jpeg
72
77
  spec/fixtures/test.jpg
73
78
  spec/mount_spec.rb
74
79
  spec/orm/activerecord_spec.rb
75
80
  spec/orm/datamapper_spec.rb
81
+ spec/orm/mongoid_spec.rb
76
82
  spec/orm/mongomapper_spec.rb
77
83
  spec/orm/sequel_spec.rb
84
+ spec/processing/image_science_spec.rb
85
+ spec/processing/mini_magick_spec.rb
86
+ spec/processing/rmagick_spec.rb
78
87
  spec/sanitized_file_spec.rb
79
88
  spec/spec_helper.rb
80
89
  spec/storage/grid_fs_spec.rb
90
+ spec/storage/right_s3_spec.rb
81
91
  spec/storage/s3_spec.rb
82
92
  spec/uploader/cache_spec.rb
83
93
  spec/uploader/configuration_spec.rb
@@ -199,7 +199,8 @@ this easily by overriding the +default_url+ method in your uploader:
199
199
 
200
200
  == Configuring CarrierWave
201
201
 
202
- CarrierWave has a broad range of configuration options, which you can configure, both globally and on a per-uploader basis:
202
+ CarrierWave has a broad range of configuration options, which you can configure,
203
+ both globally and on a per-uploader basis:
203
204
 
204
205
  CarrierWave.configure do |config|
205
206
  config.permissions = 0666
@@ -212,6 +213,54 @@ Or alternatively:
212
213
  permissions 0777
213
214
  end
214
215
 
216
+ == Testing CarrierWave
217
+
218
+ It's a good idea to test you uploaders in isolation. In order to speed up your
219
+ tests, it's recommended to switch off processing in your tests, and to use the
220
+ file storage. In Rails you could do that by adding an initializer with:
221
+
222
+ if Rails.env.test?
223
+ Carrierwave.configure do |config|
224
+ config.storage = :file
225
+ config.enable_processing = false
226
+ end
227
+ end
228
+
229
+ If you need to test your processing, you should test it in isolation, and enable
230
+ processing only for those tests that need it.
231
+
232
+ CarrierWave comes with some RSpec matchers which you may find useful:
233
+
234
+ require 'carrierwave/test/matchers'
235
+
236
+ describe MyUploader do
237
+ before do
238
+ MyUploader.enable_processing = true
239
+ @uploader = MyUploader.new(@user, :avatar)
240
+ @uploader.store!(File.open(path_to_file))
241
+ end
242
+
243
+ after do
244
+ MyUploader.enable_processing = false
245
+ end
246
+
247
+ context 'the thumb version' do
248
+ it "should scale down a landscape image to be exactly 64 by 64 pixels" do
249
+ @uploader.thumb.should have_dimensions(200, 200)
250
+ end
251
+ end
252
+
253
+ context 'the small version' do
254
+ it "should scale down a landscape image to fit within 200 by 200 pixels" do
255
+ @uploader.small.should be_no_larger_than(200, 200)
256
+ end
257
+ end
258
+
259
+ it "should make the image readable only to the owner and not executable" do
260
+ @uploader.should have_premissions(0600)
261
+ end
262
+ end
263
+
215
264
  == Using Amazon S3
216
265
 
217
266
  You'll need to configure a bucket, access id and secret key like this:
@@ -231,7 +280,18 @@ And then in your uploader, set the storage to :s3
231
280
  end
232
281
 
233
282
  That's it! You can still use the +CarrierWave::Uploader#url+ method to return
234
- the url to the file on Amazon S3
283
+ the url to the file on Amazon S3.
284
+
285
+ Alternatively, and especially if your bucket is located in Europe, you can use the
286
+ RightAWS library by setting the storage to :right_s3
287
+
288
+ class AvatarUploader < CarrierWave::Uploader::Base
289
+ storage :right_s3
290
+ end
291
+
292
+ CarrierWave uses the RightAWS S3 Interface directly, meaning that the performance issues
293
+ mentioned by Jonathan Yurek for paperclip do not apply: http://groups.google.com/group/paperclip-plugin/browse_thread/thread/d4dc166a9a5f0df4#
294
+
235
295
 
236
296
  == Using MongoDB's GridFS store
237
297
 
@@ -362,6 +422,8 @@ These people have contributed their time and effort to CarrierWave:
362
422
  * Scott Motte
363
423
  * Sho Fukamachi
364
424
  * Sam Lown
425
+ * Dave Ott
426
+ * Quin Hoxie
365
427
 
366
428
  == License
367
429
 
data/Rakefile CHANGED
@@ -24,7 +24,9 @@ $hoe = Hoe.spec 'carrierwave' do
24
24
  self.extra_dev_deps << ['do_sqlite3', '>=0.9.11']
25
25
  self.extra_dev_deps << ['sequel', '>=3.2.0']
26
26
  self.extra_dev_deps << ['rmagick', '>=2.10.0']
27
- self.extra_dev_deps << ['mongo_mapper', '>=0.5.4']
27
+ self.extra_dev_deps << ['mini_magick', '>=1.2.5']
28
+ self.extra_dev_deps << ['mongo_mapper', '>=0.5.8']
29
+ self.extra_dev_deps << ['mongoid', '>=0.5.11']
28
30
  self.extra_dev_deps << ['aws-s3', '>=0.6.2']
29
31
  self.extra_rdoc_files << 'README.rdoc'
30
32
  end
@@ -7,7 +7,7 @@ require 'carrierwave/core_ext/inheritable_attributes'
7
7
 
8
8
  module CarrierWave
9
9
 
10
- VERSION = "0.4.1"
10
+ VERSION = "0.4.2"
11
11
 
12
12
  class << self
13
13
  attr_accessor :root
@@ -33,6 +33,7 @@ module CarrierWave
33
33
  autoload :File, 'carrierwave/storage/file'
34
34
  autoload :S3, 'carrierwave/storage/s3'
35
35
  autoload :GridFS, 'carrierwave/storage/grid_fs'
36
+ autoload :RightS3, 'carrierwave/storage/right_s3'
36
37
  end
37
38
 
38
39
  module Uploader
@@ -86,3 +87,4 @@ require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'activerecord')
86
87
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'datamapper') if defined?(DataMapper)
87
88
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'sequel') if defined?(Sequel)
88
89
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongomapper") if defined?(MongoMapper)
90
+ require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongoid") if defined?(Mongoid)
@@ -15,9 +15,9 @@ module CarrierWave
15
15
 
16
16
  alias_method :read_uploader, :attribute_get
17
17
  alias_method :write_uploader, :attribute_set
18
-
19
18
  after :save, "store_#{column}!".to_sym
20
- before :save, "write_#{column}_identifier".to_sym
19
+ pre_hook = ::DataMapper.const_defined?(:Validate) ? :valid? : :save
20
+ before pre_hook, "write_#{column}_identifier".to_sym
21
21
  after :destroy, "remove_#{column}!".to_sym
22
22
  end
23
23
 
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require 'mongoid'
3
+
4
+ module CarrierWave
5
+ module Mongoid
6
+ include CarrierWave::Mount
7
+ ##
8
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
9
+ #
10
+ def mount_uploader(column, uploader, options={}, &block)
11
+ options[:mount_on] ||= "#{column}_filename"
12
+ field options[:mount_on]
13
+ super
14
+ alias_method :read_uploader, :read_attribute
15
+ alias_method :write_uploader, :write_attribute
16
+ after_save "store_#{column}!".to_sym
17
+ before_save "write_#{column}_identifier".to_sym
18
+ after_destroy "remove_#{column}!".to_sym
19
+ end
20
+ end # Mongoid
21
+ end # CarrierWave
22
+
23
+ Mongoid::Document.send(:extend, CarrierWave::Mongoid)
@@ -0,0 +1,269 @@
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 :do_stuff => 10.0
36
+ #
37
+ # def do_stuff(blur_factor)
38
+ # manipulate! do |img|
39
+ # img = img.sepiatone
40
+ # img = img.auto_orient
41
+ # img = img.radial_blur blur_factor
42
+ # end
43
+ # end
44
+ # end
45
+ #
46
+ # === Note
47
+ #
48
+ # MiniMagick is a mini replacement for RMagick that uses the command line
49
+ # tool "mogrify" for image manipulation.
50
+ #
51
+ # You can find more information here:
52
+ #
53
+ # http://mini_magick.rubyforge.org/
54
+ # and
55
+ # http://github.com/probablycorey/mini_magick/
56
+ #
57
+ #
58
+ module MiniMagick
59
+
60
+ def self.included(base)
61
+ super
62
+ base.extend(ClassMethods)
63
+ end
64
+
65
+ module ClassMethods
66
+ def convert(format)
67
+ process :resize_to_limit => format
68
+ end
69
+
70
+ def resize_to_limit(width, height)
71
+ process :resize_to_limit => [width, height]
72
+ end
73
+
74
+ def resize_to_fit(width, height)
75
+ process :resize_to_fit => [width, height]
76
+ end
77
+
78
+ def resize_to_fill(width, height)
79
+ process :resize_to_fill => [width, height]
80
+ end
81
+
82
+ def resize_and_pad(width, height)
83
+ process :resize_to_fit => [width, height]
84
+ end
85
+
86
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
87
+ process :resize_and_pad => [width, height, background, gravity]
88
+ end
89
+ end
90
+
91
+ ##
92
+ # Changes the image encoding format to the given format
93
+ #
94
+ # See http://www.imagemagick.org/script/command-line-options.php#format
95
+ #
96
+ # === Parameters
97
+ #
98
+ # [format (#to_s)] an abreviation of the format
99
+ #
100
+ # === Yields
101
+ #
102
+ # [MiniMagick::Image] additional manipulations to perform
103
+ #
104
+ # === Examples
105
+ #
106
+ # image.convert(:png)
107
+ #
108
+ def convert(format)
109
+ manipulate! do |img|
110
+ img.format(format.to_s.upcase)
111
+ img = yield(img) if block_given?
112
+ img
113
+ end
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
+ # [MiniMagick::Image] additional manipulations to perform
130
+ #
131
+ def resize_to_limit(width, height)
132
+ manipulate! do |img|
133
+ img.resize "#{width}x#{height}>"
134
+ img = yield(img) if block_given?
135
+ img
136
+ end
137
+ end
138
+
139
+ ##
140
+ # From the RMagick documentation: "Resize the image to fit within the
141
+ # specified dimensions while retaining the original aspect ratio. The
142
+ # image may be shorter or narrower than specified in the smaller dimension
143
+ # but will not be larger than the specified values."
144
+ #
145
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
146
+ #
147
+ # === Parameters
148
+ #
149
+ # [width (Integer)] the width to scale the image to
150
+ # [height (Integer)] the height to scale the image to
151
+ #
152
+ # === Yields
153
+ #
154
+ # [MiniMagick::Image] additional manipulations to perform
155
+ #
156
+ def resize_to_fit(width, height)
157
+ manipulate! do |img|
158
+ img.resize "#{width}x#{height}"
159
+ img = yield(img) if block_given?
160
+ img
161
+ end
162
+ end
163
+
164
+ ##
165
+ # From the RMagick documentation: "Resize the image to fit within the
166
+ # specified dimensions while retaining the aspect ratio of the original
167
+ # image. If necessary, crop the image in the larger dimension."
168
+ #
169
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
170
+ #
171
+ # and
172
+ #
173
+ # http://www.clipclip.org/clips/detail/4365/jerrett-net-»-crop_resized-in-rmagick
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
+ # [MiniMagick::Image] additional manipulations to perform
183
+ #
184
+ def resize_to_fill(width, height, gravity = 'Center')
185
+ manipulate! do |img|
186
+ cols, rows = img[:dimensions]
187
+ img.combine_options do |cmd|
188
+ if width != cols || height != rows
189
+ scale = [width/cols.to_f, height/rows.to_f].max
190
+ cols = (scale * (cols + 0.5)).round
191
+ rows = (scale * (rows + 0.5)).round
192
+ cmd.resize "#{cols}x#{rows}"
193
+ end
194
+ cmd.gravity gravity
195
+ cmd.extent "#{width}x#{height}" if cols != width || rows != height
196
+ end
197
+ img = yield(img) if block_given?
198
+ img
199
+ end
200
+ end
201
+
202
+ ##
203
+ # Resize the image to fit within the specified dimensions while retaining
204
+ # the original aspect ratio. If necessary, will pad the remaining area
205
+ # with the given color, which defaults to transparent (for gif and png,
206
+ # white for jpeg).
207
+ #
208
+ # See http://www.imagemagick.org/script/command-line-options.php#gravity
209
+ # for gravity options.
210
+ #
211
+ # === Parameters
212
+ #
213
+ # [width (Integer)] the width to scale the image to
214
+ # [height (Integer)] the height to scale the image to
215
+ # [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
216
+ # [gravity (String)] how to position the image
217
+ #
218
+ # === Yields
219
+ #
220
+ # [MiniMagick::Image] additional manipulations to perform
221
+ #
222
+ def resize_and_pad(width, height, background=:transparent, gravity='Center')
223
+ manipulate! do |img|
224
+ img.combine_options do |cmd|
225
+ cmd.thumbnail "#{width}x#{height}>"
226
+ if background == :transparent
227
+ cmd.background "rgba(0, 0, 0, 0.0)"
228
+ else
229
+ cmd.background background
230
+ end
231
+ cmd.gravity gravity
232
+ cmd.extent "#{width}x#{height}"
233
+ end
234
+ img = yield(img) if block_given?
235
+ img
236
+ end
237
+ end
238
+
239
+ ##
240
+ # Manipulate the image with RMagick. This method will load up an image
241
+ # and then pass each of its frames to the supplied block. It will then
242
+ # save the image to disk.
243
+ #
244
+ # === Gotcha
245
+ #
246
+ # This method assumes that the object responds to +current_path+.
247
+ # Any class that this module is mixed into must have a +current_path+ method.
248
+ # CarrierWave::Uploader does, so you won't need to worry about this in
249
+ # most cases.
250
+ #
251
+ # === Yields
252
+ #
253
+ # [MiniMagick::Image] manipulations to perform
254
+ #
255
+ # === Raises
256
+ #
257
+ # [CarrierWave::ProcessingError] if manipulation failed.
258
+ #
259
+ def manipulate!
260
+ image = ::MiniMagick::Image.from_file(current_path)
261
+ image = yield(image)
262
+ image.write(current_path)
263
+ ::MiniMagick::Image.from_file(current_path)
264
+ rescue ::MiniMagick::MiniMagickError => e
265
+ raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
266
+ end
267
+
268
+ end # MiniMagick
269
+ end # CarrierWave