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.

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