carrierwave 0.5.4 → 0.5.5
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.
- data/README.md +716 -0
- data/lib/carrierwave.rb +7 -0
- data/lib/carrierwave/mount.rb +38 -11
- data/lib/carrierwave/orm/activerecord.rb +5 -2
- data/lib/carrierwave/orm/mongoid.rb +40 -5
- data/lib/carrierwave/processing/image_science.rb +0 -4
- data/lib/carrierwave/processing/mime_types.rb +58 -0
- data/lib/carrierwave/processing/mini_magick.rb +7 -16
- data/lib/carrierwave/sanitized_file.rb +21 -4
- data/lib/carrierwave/uploader/cache.rb +13 -4
- data/lib/carrierwave/uploader/configuration.rb +9 -9
- data/lib/carrierwave/uploader/download.rb +1 -1
- data/lib/carrierwave/uploader/processing.rb +20 -14
- data/lib/carrierwave/uploader/proxy.rb +16 -1
- data/lib/carrierwave/uploader/store.rb +11 -0
- data/lib/carrierwave/uploader/versions.rb +79 -21
- data/lib/carrierwave/version.rb +1 -1
- metadata +55 -136
- data/README.rdoc +0 -647
- data/lib/carrierwave/orm/datamapper.rb +0 -37
- data/lib/carrierwave/orm/sequel.rb +0 -45
data/README.md
ADDED
@@ -0,0 +1,716 @@
|
|
1
|
+
# CarrierWave
|
2
|
+
|
3
|
+
This gem provides a simple and extremely flexible way to upload files from Ruby applications.
|
4
|
+
It works well with Rack based web applications, such as Ruby on Rails.
|
5
|
+
|
6
|
+
## Information
|
7
|
+
|
8
|
+
* RDoc documentation [available on RubyDoc.info](http://rubydoc.info/gems/carrierwave/frames)
|
9
|
+
* Source code [available on GitHub](http://github.com/jnicklas/carrierwave)
|
10
|
+
* More information, known limitations, and how-tos [available on the wiki](https://github.com/jnicklas/carrierwave/wiki)
|
11
|
+
|
12
|
+
## Getting Help
|
13
|
+
|
14
|
+
* Please ask the [Google Group](http://groups.google.com/group/carrierwave) for help if you have any questions.
|
15
|
+
* Please report bugs on the [issue tracker](http://github.com/jnicklas/carrierwave/issues) but read the "getting help" section in the wiki first.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Install the latest stable release:
|
20
|
+
|
21
|
+
[sudo] gem install carrierwave
|
22
|
+
|
23
|
+
In Rails, add it to your Gemfile:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
gem 'carrierwave'
|
27
|
+
```
|
28
|
+
|
29
|
+
Note that CarrierWave is not compatible with Rails 2 as of version 0.5. If you want to use
|
30
|
+
Rails 2, please use the 0.4-stable branch on GitHub.
|
31
|
+
|
32
|
+
## Getting Started
|
33
|
+
|
34
|
+
Start off by generating an uploader:
|
35
|
+
|
36
|
+
rails generate uploader Avatar
|
37
|
+
|
38
|
+
this should give you a file in:
|
39
|
+
|
40
|
+
app/uploaders/avatar_uploader.rb
|
41
|
+
|
42
|
+
Check out this file for some hints on how you can customize your uploader. It
|
43
|
+
should look something like this:
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
47
|
+
storage :file
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
You can use your uploader class to store and retrieve files like this:
|
52
|
+
|
53
|
+
``` ruby
|
54
|
+
uploader = AvatarUploader.new
|
55
|
+
|
56
|
+
uploader.store!(my_file)
|
57
|
+
|
58
|
+
uploader.retrieve_from_store!('my_file.png')
|
59
|
+
```
|
60
|
+
|
61
|
+
CarrierWave gives you a `store` for permanent storage, and a `cache` for
|
62
|
+
temporary storage. You can use different stores, at the moment a filesystem
|
63
|
+
store, an Amazon S3 store, a Rackspace Cloud Files store, and a store for
|
64
|
+
MongoDB's GridFS are bundled.
|
65
|
+
|
66
|
+
Most of the time you are going to want to use CarrierWave together with an ORM.
|
67
|
+
It is quite simple to mount uploaders on columns in your model, so you can
|
68
|
+
simply assign files and get going:
|
69
|
+
|
70
|
+
### ActiveRecord, Mongoid
|
71
|
+
|
72
|
+
Make sure you are loading CarrierWave after loading your ORM, otherwise you'll
|
73
|
+
need to require the relevant extension manually, e.g.:
|
74
|
+
|
75
|
+
``` ruby
|
76
|
+
require 'carrierwave/orm/activerecord'
|
77
|
+
```
|
78
|
+
|
79
|
+
Add a string column to the model you want to mount the uploader on:
|
80
|
+
|
81
|
+
``` ruby
|
82
|
+
add_column :users, :avatar, :string
|
83
|
+
```
|
84
|
+
|
85
|
+
Open your model file and mount the uploader:
|
86
|
+
|
87
|
+
``` ruby
|
88
|
+
class User
|
89
|
+
mount_uploader :avatar, AvatarUploader
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
This works the same with all supported ORMs.
|
94
|
+
|
95
|
+
Now you can cache files by assigning them to the attribute, they will
|
96
|
+
automatically be stored when the record is saved.
|
97
|
+
|
98
|
+
``` ruby
|
99
|
+
u = User.new
|
100
|
+
u.avatar = params[:file]
|
101
|
+
u.avatar = File.open('somewhere')
|
102
|
+
u.save!
|
103
|
+
u.avatar.url # => '/url/to/file.png'
|
104
|
+
u.avatar.current_path # => 'path/to/file.png'
|
105
|
+
```
|
106
|
+
|
107
|
+
If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
|
108
|
+
You must explicitly call save on embedded documents in order to save their attached files.
|
109
|
+
You can read more about this [here](https://github.com/jnicklas/carrierwave/issues#issue/81)
|
110
|
+
|
111
|
+
### DataMapper, Sequel
|
112
|
+
|
113
|
+
Other ORM support has been extracted into separate gems. Learn more:
|
114
|
+
|
115
|
+
* [carrierwave-datamapper](https://github.com/jnicklas/carrierwave-datamapper)
|
116
|
+
* [carrierwave-sequel](https://github.com/jnicklas/carrierwave-sequel)
|
117
|
+
|
118
|
+
There are more extensions listed in [the wiki](https://github.com/jnicklas/carrierwave/wiki)
|
119
|
+
|
120
|
+
## Changing the storage directory
|
121
|
+
|
122
|
+
In order to change where uploaded files are put, just override the `store_dir`
|
123
|
+
method:
|
124
|
+
|
125
|
+
``` ruby
|
126
|
+
class MyUploader < CarrierWave::Uploader::Base
|
127
|
+
def store_dir
|
128
|
+
'public/my/upload/directory'
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
This works for the file storage as well as Amazon S3 and Rackspace Cloud Files.
|
134
|
+
Define `store_dir` as `nil` if you'd like to store files at the root level.
|
135
|
+
|
136
|
+
## Securing uploads
|
137
|
+
|
138
|
+
Certain file might be dangerous if uploaded to the wrong location, such as php
|
139
|
+
files or other script files. CarrierWave allows you to specify a white-list of
|
140
|
+
allowed extensions.
|
141
|
+
|
142
|
+
If you're mounting the uploader, uploading a file with the wrong extension will
|
143
|
+
make the record invalid instead. Otherwise, an error is raised.
|
144
|
+
|
145
|
+
``` ruby
|
146
|
+
class MyUploader < CarrierWave::Uploader::Base
|
147
|
+
def extension_white_list
|
148
|
+
%w(jpg jpeg gif png)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
### Filenames and unicode chars
|
154
|
+
|
155
|
+
Another security issue you should care for is the file names (see
|
156
|
+
[Ruby On Rails Security Guide](http://guides.rubyonrails.org/security.html#file-uploads)).
|
157
|
+
By default, CarrierWave provides only English letters, arabic numerals and '-+_.' symbols as
|
158
|
+
white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you
|
159
|
+
have to override `sanitize_regexp` method. It should return regular expression which would match
|
160
|
+
all *non*-allowed symbols.
|
161
|
+
|
162
|
+
With Ruby 1.9 and higher you can simply write (as it has [Oniguruma](http://oniguruma.rubyforge.org/oniguruma/)
|
163
|
+
built-in):
|
164
|
+
|
165
|
+
``` ruby
|
166
|
+
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
|
167
|
+
```
|
168
|
+
|
169
|
+
With Ruby 1.8 you have to manually specify all character ranges. For example, for files which may
|
170
|
+
contain Russian letters:
|
171
|
+
|
172
|
+
``` ruby
|
173
|
+
CarrierWave::SanitizedFile.sanitize_regexp = /[^a-zA-Zа-яА-ЯёЁ0-9\.\-\+_]/u
|
174
|
+
```
|
175
|
+
|
176
|
+
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
|
177
|
+
plugins or client-side software.
|
178
|
+
|
179
|
+
## Adding versions
|
180
|
+
|
181
|
+
Often you'll want to add different versions of the same file. The classic
|
182
|
+
example is image thumbnails. There is built in support for this:
|
183
|
+
|
184
|
+
``` ruby
|
185
|
+
class MyUploader < CarrierWave::Uploader::Base
|
186
|
+
include CarrierWave::RMagick
|
187
|
+
|
188
|
+
process :resize_to_fit => [800, 800]
|
189
|
+
|
190
|
+
version :thumb do
|
191
|
+
process :resize_to_fill => [200,200]
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
```
|
196
|
+
|
197
|
+
When this uploader is used, an uploaded image would be scaled to be no larger
|
198
|
+
than 800 by 800 pixels. A version called thumb is then created, which is scaled
|
199
|
+
and cropped to exactly 200 by 200 pixels. The uploader could be used like this:
|
200
|
+
|
201
|
+
``` ruby
|
202
|
+
uploader = AvatarUploader.new
|
203
|
+
uploader.store!(my_file) # size: 1024x768
|
204
|
+
|
205
|
+
uploader.url # => '/url/to/my_file.png' # size: 800x600
|
206
|
+
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
|
207
|
+
```
|
208
|
+
|
209
|
+
One important thing to remember is that process is called *before* versions are
|
210
|
+
created. This can cut down on processing cost.
|
211
|
+
|
212
|
+
It is possible to nest versions within versions:
|
213
|
+
|
214
|
+
``` ruby
|
215
|
+
class MyUploader < CarrierWave::Uploader::Base
|
216
|
+
|
217
|
+
version :animal do
|
218
|
+
version :human
|
219
|
+
version :monkey
|
220
|
+
version :llama
|
221
|
+
end
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
## Making uploads work across form redisplays
|
226
|
+
|
227
|
+
Often you'll notice that uploaded files disappear when a validation fails.
|
228
|
+
CarrierWave has a feature that makes it easy to remember the uploaded file even
|
229
|
+
in that case. Suppose your `user` model has an uploader mounted on `avatar`
|
230
|
+
file, just add a hidden field called `avatar_cache`. In Rails, this would look
|
231
|
+
like this:
|
232
|
+
|
233
|
+
``` erb
|
234
|
+
<%= form_for @user, :html => {:multipart => true} do |f| %>
|
235
|
+
<p>
|
236
|
+
<label>My Avatar</label>
|
237
|
+
<%= f.file_field :avatar %>
|
238
|
+
<%= f.hidden_field :avatar_cache %>
|
239
|
+
</p>
|
240
|
+
<% end %>
|
241
|
+
````
|
242
|
+
|
243
|
+
It might be a good idea to show the user that a file has been uploaded, in the
|
244
|
+
case of images, a small thumbnail would be a good indicator:
|
245
|
+
|
246
|
+
``` erb
|
247
|
+
<%= form_for @user, :html => {:multipart => true} do |f| %>
|
248
|
+
<p>
|
249
|
+
<label>My Avatar</label>
|
250
|
+
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
251
|
+
<%= f.file_field :avatar %>
|
252
|
+
<%= f.hidden_field :avatar_cache %>
|
253
|
+
</p>
|
254
|
+
<% end %>
|
255
|
+
```
|
256
|
+
|
257
|
+
## Removing uploaded files
|
258
|
+
|
259
|
+
If you want to remove a previously uploaded file on a mounted uploader, you can
|
260
|
+
easily add a checkbox to the form which will remove the file when checked.
|
261
|
+
|
262
|
+
``` erb
|
263
|
+
<%= form_for @user, :html => {:multipart => true} do |f| %>
|
264
|
+
<p>
|
265
|
+
<label>My Avatar</label>
|
266
|
+
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
267
|
+
<%= f.file_field :avatar %>
|
268
|
+
</p>
|
269
|
+
|
270
|
+
<p>
|
271
|
+
<label>
|
272
|
+
<%= f.check_box :remove_avatar %>
|
273
|
+
Remove avatar
|
274
|
+
</label>
|
275
|
+
</p>
|
276
|
+
<% end %>
|
277
|
+
```
|
278
|
+
|
279
|
+
If you want to remove the file manually, you can call <code>remove_avatar!</code>.
|
280
|
+
|
281
|
+
## Uploading files from a remote location
|
282
|
+
|
283
|
+
Your users may find it convenient to upload a file from a location on the Internet
|
284
|
+
via a URL. CarrierWave makes this simple, just add the appropriate attribute to your
|
285
|
+
form and you're good to go:
|
286
|
+
|
287
|
+
``` erb
|
288
|
+
<%= form_for @user, :html => {:multipart => true} do |f| %>
|
289
|
+
<p>
|
290
|
+
<label>My Avatar URL:</label>
|
291
|
+
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
292
|
+
<%= f.text_field :remote_avatar_url %>
|
293
|
+
</p>
|
294
|
+
<% end %>
|
295
|
+
```
|
296
|
+
|
297
|
+
## Providing a default URL
|
298
|
+
|
299
|
+
In many cases, especially when working with images, it might be a good idea to
|
300
|
+
provide a default url, a fallback in case no file has been uploaded. You can do
|
301
|
+
this easily by overriding the `default_url` method in your uploader:
|
302
|
+
|
303
|
+
``` ruby
|
304
|
+
class MyUploader < CarrierWave::Uploader::Base
|
305
|
+
def default_url
|
306
|
+
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
|
307
|
+
end
|
308
|
+
end
|
309
|
+
```
|
310
|
+
|
311
|
+
## Recreating versions
|
312
|
+
|
313
|
+
You might come to a situation where you want to retroactively change a version
|
314
|
+
or add a new one. You can use the recreate_versions! method to recreate the
|
315
|
+
versions from the base file. This uses a naive approach which will re-upload and
|
316
|
+
process all versions.
|
317
|
+
|
318
|
+
``` ruby
|
319
|
+
instance = MyUploader.new
|
320
|
+
instance.recreate_versions!
|
321
|
+
```
|
322
|
+
|
323
|
+
Or on a mounted uploader:
|
324
|
+
|
325
|
+
``` ruby
|
326
|
+
User.all.each do |user|
|
327
|
+
user.avatar.recreate_versions!
|
328
|
+
end
|
329
|
+
```
|
330
|
+
|
331
|
+
## Configuring CarrierWave
|
332
|
+
|
333
|
+
CarrierWave has a broad range of configuration options, which you can configure,
|
334
|
+
both globally and on a per-uploader basis:
|
335
|
+
|
336
|
+
``` ruby
|
337
|
+
CarrierWave.configure do |config|
|
338
|
+
config.permissions = 0666
|
339
|
+
config.storage = :s3
|
340
|
+
end
|
341
|
+
```
|
342
|
+
|
343
|
+
Or alternatively:
|
344
|
+
|
345
|
+
``` ruby
|
346
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
347
|
+
permissions 0777
|
348
|
+
end
|
349
|
+
```
|
350
|
+
|
351
|
+
If you're using Rails, create an initializer for this:
|
352
|
+
|
353
|
+
``` ruby
|
354
|
+
config/initializers/carrierwave.rb
|
355
|
+
```
|
356
|
+
|
357
|
+
## Testing with CarrierWave
|
358
|
+
|
359
|
+
It's a good idea to test you uploaders in isolation. In order to speed up your
|
360
|
+
tests, it's recommended to switch off processing in your tests, and to use the
|
361
|
+
file storage. In Rails you could do that by adding an initializer with:
|
362
|
+
|
363
|
+
``` ruby
|
364
|
+
if Rails.env.test? or Rails.env.cucumber?
|
365
|
+
CarrierWave.configure do |config|
|
366
|
+
config.storage = :file
|
367
|
+
config.enable_processing = false
|
368
|
+
end
|
369
|
+
end
|
370
|
+
```
|
371
|
+
|
372
|
+
If you need to test your processing, you should test it in isolation, and enable
|
373
|
+
processing only for those tests that need it.
|
374
|
+
|
375
|
+
CarrierWave comes with some RSpec matchers which you may find useful:
|
376
|
+
|
377
|
+
``` ruby
|
378
|
+
require 'carrierwave/test/matchers'
|
379
|
+
|
380
|
+
describe MyUploader do
|
381
|
+
include CarrierWave::Test::Matchers
|
382
|
+
|
383
|
+
before do
|
384
|
+
MyUploader.enable_processing = true
|
385
|
+
@uploader = MyUploader.new(@user, :avatar)
|
386
|
+
@uploader.store!(File.open(path_to_file))
|
387
|
+
end
|
388
|
+
|
389
|
+
after do
|
390
|
+
MyUploader.enable_processing = false
|
391
|
+
end
|
392
|
+
|
393
|
+
context 'the thumb version' do
|
394
|
+
it "should scale down a landscape image to be exactly 64 by 64 pixels" do
|
395
|
+
@uploader.thumb.should have_dimensions(64, 64)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
context 'the small version' do
|
400
|
+
it "should scale down a landscape image to fit within 200 by 200 pixels" do
|
401
|
+
@uploader.small.should be_no_larger_than(200, 200)
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
it "should make the image readable only to the owner and not executable" do
|
406
|
+
@uploader.should have_permissions(0600)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
```
|
410
|
+
|
411
|
+
Setting the enable_processing flag on an uploader will prevent any of the versions from processing as well.
|
412
|
+
Processing can be enabled for a single version by setting the processing flag on the version like so:
|
413
|
+
|
414
|
+
``` ruby
|
415
|
+
@uploader.thumb.enable_processing = true
|
416
|
+
```
|
417
|
+
|
418
|
+
## Using Amazon S3
|
419
|
+
|
420
|
+
[Fog](http://github.com/geemus/fog) is used to support Amazon S3. Ensure you have it installed:
|
421
|
+
|
422
|
+
gem install fog
|
423
|
+
|
424
|
+
You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.
|
425
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
426
|
+
You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:
|
427
|
+
|
428
|
+
``` ruby
|
429
|
+
CarrierWave.configure do |config|
|
430
|
+
config.fog_credentials = {
|
431
|
+
:provider => 'AWS', # required
|
432
|
+
:aws_access_key_id => 'xxx', # required
|
433
|
+
:aws_secret_access_key => 'yyy', # required
|
434
|
+
:region => 'eu-west-1' # optional, defaults to 'us-east-1'
|
435
|
+
}
|
436
|
+
config.fog_directory = 'name_of_directory' # required
|
437
|
+
config.fog_host = 'https://assets.example.com' # optional, defaults to nil
|
438
|
+
config.fog_public = false # optional, defaults to true
|
439
|
+
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
|
440
|
+
end
|
441
|
+
```
|
442
|
+
|
443
|
+
In your uploader, set the storage to :fog
|
444
|
+
|
445
|
+
``` ruby
|
446
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
447
|
+
storage :fog
|
448
|
+
end
|
449
|
+
```
|
450
|
+
|
451
|
+
That's it! You can still use the `CarrierWave::Uploader#url` method to return the url to the file on Amazon S3.
|
452
|
+
|
453
|
+
## Using Rackspace Cloud Files
|
454
|
+
|
455
|
+
[Fog](http://github.com/geemus/fog) is used to support Rackspace Cloud Files. Ensure you have it installed:
|
456
|
+
|
457
|
+
gem install fog
|
458
|
+
|
459
|
+
You'll need to configure a directory (also known as a container), username and API key in the initializer.
|
460
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
461
|
+
|
462
|
+
``` ruby
|
463
|
+
CarrierWave.configure do |config|
|
464
|
+
config.fog_credentials = {
|
465
|
+
:provider => 'Rackspace',
|
466
|
+
:rackspace_username => 'xxxxxx',
|
467
|
+
:rackspace_api_key => 'yyyyyy'
|
468
|
+
}
|
469
|
+
config.fog_directory = 'name_of_directory'
|
470
|
+
end
|
471
|
+
```
|
472
|
+
|
473
|
+
You can optionally include your CDN host name in the configuration.
|
474
|
+
This is *highly* recommended, as without it every request requires a lookup
|
475
|
+
of this information.
|
476
|
+
|
477
|
+
``` ruby
|
478
|
+
config.fog_host = "c000000.cdn.rackspacecloud.com"
|
479
|
+
```
|
480
|
+
|
481
|
+
In your uploader, set the storage to :fog
|
482
|
+
|
483
|
+
``` ruby
|
484
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
485
|
+
storage :fog
|
486
|
+
end
|
487
|
+
```
|
488
|
+
|
489
|
+
That's it! You can still use the `CarrierWave::Uploader#url` method to return
|
490
|
+
the url to the file on Rackspace Cloud Files.
|
491
|
+
|
492
|
+
## Using Google Storage for Developers
|
493
|
+
|
494
|
+
[Fog](http://github.com/geemus/fog) is used to support Google Storage for Developers. Ensure you have it installed:
|
495
|
+
|
496
|
+
gem install fog
|
497
|
+
|
498
|
+
You'll need to configure a directory (also known as a bucket), access key id and secret access key in the initializer.
|
499
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
500
|
+
|
501
|
+
``` ruby
|
502
|
+
CarrierWave.configure do |config|
|
503
|
+
config.fog_credentials = {
|
504
|
+
:provider => 'Google',
|
505
|
+
:google_storage_access_key_id => 'xxxxxx',
|
506
|
+
:google_storage_secret_access_key => 'yyyyyy'
|
507
|
+
}
|
508
|
+
config.fog_directory = 'name_of_directory'
|
509
|
+
end
|
510
|
+
```
|
511
|
+
|
512
|
+
In your uploader, set the storage to :fog
|
513
|
+
|
514
|
+
``` ruby
|
515
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
516
|
+
storage :fog
|
517
|
+
end
|
518
|
+
```
|
519
|
+
|
520
|
+
That's it! You can still use the `CarrierWave::Uploader#url` method to return
|
521
|
+
the url to the file on Google.
|
522
|
+
|
523
|
+
## Using MongoDB's GridFS store
|
524
|
+
|
525
|
+
You'll need to configure the database and host to use:
|
526
|
+
|
527
|
+
``` ruby
|
528
|
+
CarrierWave.configure do |config|
|
529
|
+
config.grid_fs_database = 'my_mongo_database'
|
530
|
+
config.grid_fs_host = 'mongo.example.com'
|
531
|
+
end
|
532
|
+
```
|
533
|
+
|
534
|
+
The defaults are 'carrierwave' and 'localhost'.
|
535
|
+
|
536
|
+
And then in your uploader, set the storage to `:grid_fs`:
|
537
|
+
|
538
|
+
``` ruby
|
539
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
540
|
+
storage :grid_fs
|
541
|
+
end
|
542
|
+
```
|
543
|
+
|
544
|
+
Since GridFS doesn't make the files available via HTTP, you'll need to stream
|
545
|
+
them yourself. In Rails for example, you could use the `send_data` method. You
|
546
|
+
can tell CarrierWave the URL you will serve your images from, allowing it to
|
547
|
+
generate the correct URL, by setting eg:
|
548
|
+
|
549
|
+
``` ruby
|
550
|
+
CarrierWave.configure do |config|
|
551
|
+
config.grid_fs_access_url = "/image/show"
|
552
|
+
end
|
553
|
+
```
|
554
|
+
|
555
|
+
## Using RMagick
|
556
|
+
|
557
|
+
If you're uploading images, you'll probably want to manipulate them in some way,
|
558
|
+
you might want to create thumbnail images for example. CarrierWave comes with a
|
559
|
+
small library to make manipulating images with RMagick easier, you'll need to
|
560
|
+
include it in your Uploader:
|
561
|
+
|
562
|
+
``` ruby
|
563
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
564
|
+
include CarrierWave::RMagick
|
565
|
+
end
|
566
|
+
```
|
567
|
+
|
568
|
+
The RMagick module gives you a few methods, like
|
569
|
+
`CarrierWave::RMagick#resize_to_fill` which manipulate the image file in some
|
570
|
+
way. You can set a `process` callback, which will call that method any time a
|
571
|
+
file is uploaded.
|
572
|
+
There is a demonstration of convert here.
|
573
|
+
Convert will only work if the file has the same file extension, thus the use of the filename method.
|
574
|
+
|
575
|
+
``` ruby
|
576
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
577
|
+
include CarrierWave::RMagick
|
578
|
+
|
579
|
+
process :resize_to_fill => [200, 200]
|
580
|
+
process :convert => 'png'
|
581
|
+
|
582
|
+
def filename
|
583
|
+
super.chomp(File.extname(super)) + '.png'
|
584
|
+
end
|
585
|
+
end
|
586
|
+
```
|
587
|
+
|
588
|
+
Check out the manipulate! method, which makes it easy for you to write your own
|
589
|
+
manipulation methods.
|
590
|
+
|
591
|
+
## Using ImageScience
|
592
|
+
|
593
|
+
ImageScience works the same way as RMagick.
|
594
|
+
|
595
|
+
``` ruby
|
596
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
597
|
+
include CarrierWave::ImageScience
|
598
|
+
|
599
|
+
process :resize_to_fill => [200, 200]
|
600
|
+
end
|
601
|
+
```
|
602
|
+
|
603
|
+
## Using MiniMagick
|
604
|
+
|
605
|
+
MiniMagick is similar to RMagick but performs all the operations using the 'mogrify'
|
606
|
+
command which is part of the standard ImageMagick kit. This allows you to have the power
|
607
|
+
of ImageMagick without having to worry about installing all the RMagick libraries.
|
608
|
+
|
609
|
+
See the MiniMagick site for more details:
|
610
|
+
|
611
|
+
http://github.com/probablycorey/mini_magick
|
612
|
+
|
613
|
+
And the ImageMagick command line options for more for whats on offer:
|
614
|
+
|
615
|
+
http://www.imagemagick.org/script/command-line-options.php
|
616
|
+
|
617
|
+
Currently, the MiniMagick carrierwave processor provides exactly the same methods as
|
618
|
+
for the RMagick processor.
|
619
|
+
|
620
|
+
``` ruby
|
621
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
622
|
+
include CarrierWave::MiniMagick
|
623
|
+
|
624
|
+
process :resize_to_fill => [200, 200]
|
625
|
+
end
|
626
|
+
```
|
627
|
+
|
628
|
+
## Migrating from Paperclip
|
629
|
+
|
630
|
+
If you are using Paperclip, you can use the provided compatibility module:
|
631
|
+
|
632
|
+
``` ruby
|
633
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
634
|
+
include CarrierWave::Compatibility::Paperclip
|
635
|
+
end
|
636
|
+
```
|
637
|
+
|
638
|
+
See the documentation for `CarrierWave::Compatibility::Paperclip` for more
|
639
|
+
details.
|
640
|
+
|
641
|
+
Be sure to use mount_on to specify the correct column:
|
642
|
+
|
643
|
+
``` ruby
|
644
|
+
mount_uploader :avatar, AvatarUploader, :mount_on => :avatar_file_name
|
645
|
+
```
|
646
|
+
|
647
|
+
Unfortunately attachment_fu differs too much in philosophy for there to be a
|
648
|
+
sensible compatibility mode. Patches for migrating from other solutions will be
|
649
|
+
happily accepted.
|
650
|
+
|
651
|
+
## i18n
|
652
|
+
|
653
|
+
The Active Record validations use the Rails i18n framework. Add these keys to
|
654
|
+
your translations file:
|
655
|
+
|
656
|
+
``` yaml
|
657
|
+
errors:
|
658
|
+
messages:
|
659
|
+
carrierwave_processing_error: 'Cannot resize image.'
|
660
|
+
carrierwave_integrity_error: 'Not an image.'
|
661
|
+
```
|
662
|
+
|
663
|
+
## Contributing to CarrierWave
|
664
|
+
|
665
|
+
CarrierWave thrives on a large number of [contributors](https://github.com/jnicklas/carrierwave/contributors),
|
666
|
+
and pull requests are very welcome. Before submitting a pull request, please make sure that your changes are well tested.
|
667
|
+
|
668
|
+
You'll need to install bundler and the gem dependencies:
|
669
|
+
|
670
|
+
gem install bundler
|
671
|
+
bundle install
|
672
|
+
|
673
|
+
You should now be able to run the local tests:
|
674
|
+
|
675
|
+
bundle exec rake
|
676
|
+
|
677
|
+
You can also run the remote specs by creating a ~/.fog file:
|
678
|
+
|
679
|
+
``` yaml
|
680
|
+
:carrierwave:
|
681
|
+
:aws_access_key_id: xxx
|
682
|
+
:aws_secret_access_key: yyy
|
683
|
+
:rackspace_username: xxx
|
684
|
+
:rackspace_api_key: yyy
|
685
|
+
:google_storage_access_key_id: xxx
|
686
|
+
:google_storage_secret_access_key: yyy
|
687
|
+
```
|
688
|
+
|
689
|
+
You should now be able to run the remote tests:
|
690
|
+
|
691
|
+
REMOTE=true bundle exec rake
|
692
|
+
|
693
|
+
Please test with the latest Ruby 1.8.x and 1.9.x versions using RVM if possible.
|
694
|
+
|
695
|
+
## License
|
696
|
+
|
697
|
+
Copyright (c) 2008 Jonas Nicklas
|
698
|
+
|
699
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
700
|
+
a copy of this software and associated documentation files (the
|
701
|
+
"Software"), to deal in the Software without restriction, including
|
702
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
703
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
704
|
+
permit persons to whom the Software is furnished to do so, subject to
|
705
|
+
the following conditions:
|
706
|
+
|
707
|
+
The above copyright notice and this permission notice shall be
|
708
|
+
included in all copies or substantial portions of the Software.
|
709
|
+
|
710
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
711
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
712
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
713
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
714
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
715
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
716
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|