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