locomotive_carrierwave 0.5.0.1 → 0.5.4.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.md +714 -0
  2. data/lib/carrierwave.rb +9 -3
  3. data/lib/carrierwave/compatibility/paperclip.rb +18 -18
  4. data/lib/carrierwave/mount.rb +30 -25
  5. data/lib/carrierwave/orm/activerecord.rb +14 -4
  6. data/lib/carrierwave/orm/mongoid.rb +29 -8
  7. data/lib/carrierwave/processing/image_science.rb +25 -2
  8. data/lib/carrierwave/processing/mini_magick.rb +4 -3
  9. data/lib/carrierwave/processing/rmagick.rb +3 -2
  10. data/lib/carrierwave/sanitized_file.rb +23 -38
  11. data/lib/carrierwave/storage/abstract.rb +0 -3
  12. data/lib/carrierwave/storage/cloud_files.rb +30 -10
  13. data/lib/carrierwave/storage/file.rb +1 -8
  14. data/lib/carrierwave/storage/fog.rb +332 -0
  15. data/lib/carrierwave/storage/grid_fs.rb +6 -6
  16. data/lib/carrierwave/storage/s3.rb +31 -34
  17. data/lib/carrierwave/test/matchers.rb +1 -1
  18. data/lib/carrierwave/uploader.rb +0 -1
  19. data/lib/carrierwave/uploader/cache.rb +28 -7
  20. data/lib/carrierwave/uploader/callbacks.rb +11 -17
  21. data/lib/carrierwave/uploader/configuration.rb +62 -34
  22. data/lib/carrierwave/uploader/download.rb +16 -5
  23. data/lib/carrierwave/uploader/extension_whitelist.rb +14 -3
  24. data/lib/carrierwave/uploader/mountable.rb +1 -1
  25. data/lib/carrierwave/uploader/processing.rb +20 -15
  26. data/lib/carrierwave/uploader/proxy.rb +16 -1
  27. data/lib/carrierwave/uploader/store.rb +3 -9
  28. data/lib/carrierwave/uploader/url.rb +3 -2
  29. data/lib/carrierwave/uploader/versions.rb +78 -25
  30. data/lib/carrierwave/version.rb +1 -1
  31. data/lib/generators/templates/uploader.rb +2 -1
  32. metadata +64 -102
  33. data/README.rdoc +0 -532
  34. data/lib/carrierwave/orm/datamapper.rb +0 -37
  35. data/lib/carrierwave/orm/sequel.rb +0 -45
  36. data/lib/carrierwave/uploader/rename.rb +0 -62
data/README.rdoc DELETED
@@ -1,532 +0,0 @@
1
- = CarrierWave
2
-
3
- http://carrierwave.rubyforge.org
4
-
5
- == Summary
6
-
7
- This gem provides a simple and extremely flexible way to upload files from Ruby applications.
8
- It works well with Rack based web applications, such as Ruby on Rails.
9
-
10
- == Description
11
-
12
- * RDoc Documentation {available at Rubyforge}[http://carrierwave.rubyforge.org/rdoc].
13
- * Source code {hosted at GitHub}[http://github.com/jnicklas/carrierwave]
14
- * Please {report any issues}[http://github.com/jnicklas/carrierwave/issues] on GitHub
15
- * Please direct any questions at the {mailing list}[http://groups.google.com/group/carrierwave]
16
- * Check out the {example app}[http://github.com/jnicklas/carrierwave-example-app]
17
- * Instructions for setting up a development environment are at the bottom of this file
18
-
19
- == Getting Started
20
-
21
- Install the latest stable release:
22
-
23
- [sudo] gem install carrierwave
24
-
25
- In Rails, add it to your Gemfile:
26
-
27
- gem 'carrierwave'
28
-
29
- CarrierWave is only compatible with Rails 3 and later as of version 0.5. If you want to use
30
- Rails 2, please use the latest gem in the 0.4.X series.
31
-
32
- == Quick Start
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
- class AvatarUploader < CarrierWave::Uploader::Base
46
- storage :file
47
- end
48
-
49
- You can use your uploader class to store and retrieve files like this:
50
-
51
- uploader = AvatarUploader.new
52
-
53
- uploader.store!(my_file)
54
-
55
- uploader.retrieve_from_store!('my_file.png')
56
-
57
- CarrierWave gives you a +store+ for permanent storage, and a +cache+ for
58
- temporary storage. You can use different stores, at the moment a filesystem
59
- store, an Amazon S3 store, a Rackspace Cloud Files store, and a store for
60
- MongoDB's GridFS are bundled.
61
-
62
- Most of the time you are going to want to use CarrierWave together with an ORM.
63
- It is quite simple to mount uploaders on columns in your model, so you can
64
- simply assign files and get going:
65
-
66
- === ActiveRecord, DataMapper, Sequel, Mongoid
67
-
68
- Make sure you are loading CarrierWave after loading your ORM, otherwise you'll
69
- need to require the relevant extension manually, e.g.:
70
-
71
- require 'carrierwave/orm/activerecord'
72
-
73
- Add a string column to the model you want to mount the uploader on:
74
-
75
- add_column :users, :avatar, :string
76
-
77
- Open your model file and mount the uploader:
78
-
79
- class User
80
- mount_uploader :avatar, AvatarUploader
81
- end
82
-
83
- This works the same with all supported ORMs.
84
-
85
- Now you can cache files by assigning them to the attribute, they will
86
- automatically be stored when the record is saved.
87
-
88
- u = User.new
89
- u.avatar = params[:file]
90
- u.avatar = File.open('somewhere')
91
- u.save!
92
- u.avatar.url # => '/url/to/file.png'
93
- u.avatar.current_path # => 'path/to/file.png'
94
-
95
- == Changing the storage directory
96
-
97
- In order to change where uploaded files are put, just override the +store_dir+
98
- method:
99
-
100
- class MyUploader < CarrierWave::Uploader::Base
101
- def store_dir
102
- 'public/my/upload/directory'
103
- end
104
- end
105
-
106
- This works for the file storage as well as Amazon S3 and Rackspace Cloud Files.
107
- Define +store_dir+ as +nil+ if you'd like to store files at the root level.
108
-
109
- == Securing uploads
110
-
111
- Certain file might be dangerous if uploaded to the wrong location, such as php
112
- files or other script files. CarrierWave allows you to specify a white-list of
113
- allowed extensions.
114
-
115
- If you're mounting the uploader, uploading a file with the wrong extension will
116
- make the record invalid instead. Otherwise, an error is raised.
117
-
118
- class MyUploader < CarrierWave::Uploader::Base
119
- def extension_white_list
120
- %w(jpg jpeg gif png)
121
- end
122
- end
123
-
124
- == Adding versions
125
-
126
- Often you'll want to add different versions of the same file. The classic
127
- example is image thumbnails. There is built in support for this:
128
-
129
- class MyUploader < CarrierWave::Uploader::Base
130
- include CarrierWave::RMagick
131
-
132
- process :resize_to_fit => [800, 800]
133
-
134
- version :thumb do
135
- process :resize_to_fill => [200,200]
136
- end
137
-
138
- end
139
-
140
- When this uploader is used, an uploaded image would be scaled to be no larger
141
- than 800 by 800 pixels. A version called thumb is then created, which is scaled
142
- and cropped to exactly 200 by 200 pixels. The uploader could be used like this:
143
-
144
- uploader = AvatarUploader.new
145
- uploader.store!(my_file) # size: 1024x768
146
-
147
- uploader.url # => '/url/to/my_file.png' # size: 800x600
148
- uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
149
-
150
- One important thing to remember is that process is called *before* versions are
151
- created. This can cut down on processing cost.
152
-
153
- It is possible to nest versions within versions:
154
-
155
- class MyUploader < CarrierWave::Uploader::Base
156
-
157
- version :animal do
158
- version :human
159
- version :monkey
160
- version :llama
161
- end
162
- end
163
-
164
- == Making uploads work across form redisplays
165
-
166
- Often you'll notice that uploaded files disappear when a validation fails.
167
- CarrierWave has a feature that makes it easy to remember the uploaded file even
168
- in that case. Suppose your +user+ model has an uploader mounted on +avatar+
169
- file, just add a hidden field called +avatar_cache+. In Rails, this would look
170
- like this:
171
-
172
- <%= form_for @user, :html => {:multipart => true} do |f| %>
173
- <p>
174
- <label>My Avatar</label>
175
- <%= f.file_field :avatar %>
176
- <%= f.hidden_field :avatar_cache %>
177
- </p>
178
- <% end %>
179
-
180
- It might be a good idea to show the user that a file has been uploaded, in the
181
- case of images, a small thumbnail would be a good indicator:
182
-
183
- <%= form_for @user, :html => {:multipart => true} do |f| %>
184
- <p>
185
- <label>My Avatar</label>
186
- <%= image_tag(@user.avatar_url) if @user.avatar? %>
187
- <%= f.file_field :avatar %>
188
- <%= f.hidden_field :avatar_cache %>
189
- </p>
190
- <% end %>
191
-
192
- == Removing uploaded files
193
-
194
- If you want to remove a previously uploaded file on a mounted uploader, you can
195
- easily add a checkbox to the form which will remove the file when checked.
196
-
197
- <%= form_for @user, :html => {:multipart => true} do |f| %>
198
- <p>
199
- <label>My Avatar</label>
200
- <%= image_tag(@user.avatar_url) if @user.avatar? %>
201
- <%= f.file_field :avatar %>
202
- </p>
203
-
204
- <p>
205
- <label>
206
- <%= f.check_box :remove_avatar %>
207
- Remove avatar
208
- </label>
209
- </p>
210
- <% end %>
211
-
212
- If you want to remove the file manually, you can call <code>remove_avatar!</code>.
213
-
214
- == Uploading files from a remote location
215
-
216
- Your users may find it convenient to upload a file from a location on the Internet
217
- via a URL. CarrierWave makes this simple, just add the appropriate attribute to your
218
- form and you're good to go:
219
-
220
- <%= form_for @user, :html => {:multipart => true} do |f| %>
221
- <p>
222
- <label>My Avatar URL:</label>
223
- <%= image_tag(@user.avatar_url) if @user.avatar? %>
224
- <%= f.text_field :remote_avatar_url %>
225
- </p>
226
- <% end %>
227
-
228
- == Providing a default URL
229
-
230
- In many cases, especially when working with images, it might be a good idea to
231
- provide a default url, a fallback in case no file has been uploaded. You can do
232
- this easily by overriding the +default_url+ method in your uploader:
233
-
234
- class MyUploader < CarrierWave::Uploader::Base
235
- def default_url
236
- "/images/fallback/" + [version_name, "default.png"].compact.join('_')
237
- end
238
- end
239
-
240
- == Recreating versions
241
-
242
- You might come to a situation where you want to retroactively change a version
243
- or add a new one. You can use the recreate_versions! method to recreate the
244
- versions from the base file. This uses a naive approach which will reupload and
245
- process all versions.
246
-
247
- instance = MyUploader.new
248
- instance.recreate_versions!
249
-
250
- Or on a mounted uploader:
251
-
252
- User.all.each do |user|
253
- user.avatar.recreate_versions!
254
- end
255
-
256
- == Configuring CarrierWave
257
-
258
- CarrierWave has a broad range of configuration options, which you can configure,
259
- both globally and on a per-uploader basis:
260
-
261
- CarrierWave.configure do |config|
262
- config.permissions = 0666
263
- config.storage = :s3
264
- end
265
-
266
- Or alternatively:
267
-
268
- class AvatarUploader < CarrierWave::Uploader::Base
269
- permissions 0777
270
- end
271
-
272
- If you're using Rails, create an initializer for this:
273
-
274
- config/initializers/carrierwave.rb
275
-
276
- == Testing CarrierWave
277
-
278
- It's a good idea to test you uploaders in isolation. In order to speed up your
279
- tests, it's recommended to switch off processing in your tests, and to use the
280
- file storage. In Rails you could do that by adding an initializer with:
281
-
282
- if Rails.env.test? or Rails.env.cucumber?
283
- CarrierWave.configure do |config|
284
- config.storage = :file
285
- config.enable_processing = false
286
- end
287
- end
288
-
289
- If you need to test your processing, you should test it in isolation, and enable
290
- processing only for those tests that need it.
291
-
292
- CarrierWave comes with some RSpec matchers which you may find useful:
293
-
294
- require 'carrierwave/test/matchers'
295
-
296
- describe MyUploader do
297
- include CarrierWave::Test::Matchers
298
-
299
- before do
300
- MyUploader.enable_processing = true
301
- @uploader = MyUploader.new(@user, :avatar)
302
- @uploader.store!(File.open(path_to_file))
303
- end
304
-
305
- after do
306
- MyUploader.enable_processing = false
307
- end
308
-
309
- context 'the thumb version' do
310
- it "should scale down a landscape image to be exactly 64 by 64 pixels" do
311
- @uploader.thumb.should have_dimensions(64, 64)
312
- end
313
- end
314
-
315
- context 'the small version' do
316
- it "should scale down a landscape image to fit within 200 by 200 pixels" do
317
- @uploader.small.should be_no_larger_than(200, 200)
318
- end
319
- end
320
-
321
- it "should make the image readable only to the owner and not executable" do
322
- @uploader.should have_permissions(0600)
323
- end
324
- end
325
-
326
- == Using Amazon S3
327
-
328
- Older versions of CarrierWave used the +aws+, +aws-s3+, and/or +right_aws+ libraries.
329
- Now fog[http://github.com/geemus/fog] is used instead. Ensure you have it installed:
330
-
331
- gem install fog
332
-
333
- You'll need to configure a bucket, access id and secret key like this in an initializer:
334
-
335
- CarrierWave.configure do |config|
336
- config.s3_access_key_id = 'xxxxxx'
337
- config.s3_secret_access_key = 'xxxxxx'
338
- config.s3_bucket = 'name_of_bucket'
339
- end
340
-
341
- In your uploader, set the storage to :s3
342
-
343
- class AvatarUploader < CarrierWave::Uploader::Base
344
- storage :s3
345
- end
346
-
347
- That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return
348
- the url to the file on Amazon S3.
349
-
350
- == Using Rackspace Cloud Files
351
-
352
- Cloud Files support requires a {Rackspace Cloud}[http://rackspacecloud.com] username and API key.
353
- You must also create a container for Carrierwave to use, and mark it public (publish it to the CDN)
354
-
355
- CarrierWave.configure do |config|
356
- config.cloud_files_username = 'xxxxxx'
357
- config.cloud_files_api_key = 'xxxxxxxxxxxxxxxxxxxxx'
358
- config.cloud_files_container = 'name_of_bucket'
359
- end
360
-
361
- You can optionally include your CDN host name in the configuration.
362
- This is *highly* recommended, as without it every request requires a lookup
363
- of this information.
364
-
365
- config.cloud_files_cdn_host = "c000000.cdn.rackspacecloud.com"
366
-
367
- In your uploader, set the storage to :cloud_files
368
-
369
- class AvatarUploader < CarrierWave::Uploader::Base
370
- storage :cloud_files
371
- end
372
-
373
- That's it! You can still use the <code>CarrierWave::Uploader#url</code> method to return
374
- the url to the file on the Cloud Files CDN.
375
-
376
- == Using MongoDB's GridFS store
377
-
378
- You'll need to configure the database and host to use:
379
-
380
- CarrierWave.configure do |config|
381
- config.grid_fs_database = 'my_mongo_database'
382
- config.grid_fs_host = 'mongo.example.com'
383
- end
384
-
385
- The defaults are 'carrierwave' and 'localhost'.
386
-
387
- And then in your uploader, set the storage to <code>:grid_fs</code>:
388
-
389
- class AvatarUploader < CarrierWave::Uploader::Base
390
- storage :grid_fs
391
- end
392
-
393
- Since GridFS doesn't make the files available via HTTP, you'll need to stream
394
- them yourself. In Rails for example, you could use the +send_data+ method. You
395
- can tell CarrierWave the URL you will serve your images from, allowing it to
396
- generate the correct URL, by setting eg:
397
-
398
- CarrierWave.configure do |config|
399
- config.grid_fs_access_url = "/image/show"
400
- end
401
-
402
- == Using RMagick
403
-
404
- If you're uploading images, you'll probably want to manipulate them in some way,
405
- you might want to create thumbnail images for example. CarrierWave comes with a
406
- small library to make manipulating images with RMagick easier, you'll need to
407
- include it in your Uploader:
408
-
409
- class AvatarUploader < CarrierWave::Uploader::Base
410
- include CarrierWave::RMagick
411
- end
412
-
413
- The RMagick module gives you a few methods, like
414
- <code>CarrierWave::RMagick#resize_to_fill</code> which manipulate the image file in some
415
- way. You can set a +process+ callback, which will call that method any time a
416
- file is uploaded.
417
-
418
- class AvatarUploader < CarrierWave::Uploader::Base
419
- include CarrierWave::RMagick
420
-
421
- process :resize_to_fill => [200, 200]
422
- process :convert => 'png'
423
-
424
- def filename
425
- super + '.png'
426
- end
427
- end
428
-
429
- Check out the manipulate! method, which makes it easy for you to write your own
430
- manipulation methods.
431
-
432
- == Using ImageScience
433
-
434
- ImageScience works the same way as RMagick.
435
-
436
- class AvatarUploader < CarrierWave::Uploader::Base
437
- include CarrierWave::ImageScience
438
-
439
- process :resize_to_fill => [200, 200]
440
- end
441
-
442
- == Using MiniMagick
443
-
444
- MiniMagick is similar to RMagick but performs all the operations using the 'mogrify'
445
- command which is part of the standard ImageMagick kit. This allows you to have the power
446
- of ImageMagick without having to worry about installing all the RMagick libraries.
447
-
448
- See the MiniMagick site for more details:
449
-
450
- http://github.com/probablycorey/mini_magick
451
-
452
- And the ImageMagick command line options for more for whats on offer:
453
-
454
- http://www.imagemagick.org/script/command-line-options.php
455
-
456
- Currently, the MiniMagick carrierwave processor provides exactly the same methods as
457
- for the RMagick processor.
458
-
459
- class AvatarUploader < CarrierWave::Uploader::Base
460
- include CarrierWave::MiniMagick
461
-
462
- process :resize_to_fill => [200, 200]
463
- end
464
-
465
- == Migrating
466
-
467
- If you are using Paperclip, you can use the provided compatibility module:
468
-
469
- class AvatarUploader < CarrierWave::Uploader::Base
470
- include CarrierWave::Compatibility::Paperclip
471
- end
472
-
473
- See the documentation for <code>CarrierWave::Compatibility::Paperclip</code> for more
474
- detaills.
475
-
476
- Be sure to use mount_on to specify the correct column:
477
-
478
- mount_uploader :avatar, AvatarUploader, :mount_on => :avatar_file_name
479
-
480
- Unfortunately AttachmentFoo differs too much in philosophy for there to be a
481
- sensible compatibility mode. Patches for migrating from other solutions will be
482
- happily accepted.
483
-
484
- == i18n
485
-
486
- The activerecord validations use the Rails i18n framework. Add these keys to
487
- your translations file:
488
-
489
- carrierwave:
490
- errors:
491
- integrity: 'Not an image.'
492
- processing: 'Cannot resize image.'
493
-
494
- == License
495
-
496
- Copyright (c) 2008 Jonas Nicklas
497
-
498
- Permission is hereby granted, free of charge, to any person obtaining
499
- a copy of this software and associated documentation files (the
500
- "Software"), to deal in the Software without restriction, including
501
- without limitation the rights to use, copy, modify, merge, publish,
502
- distribute, sublicense, and/or sell copies of the Software, and to
503
- permit persons to whom the Software is furnished to do so, subject to
504
- the following conditions:
505
-
506
- The above copyright notice and this permission notice shall be
507
- included in all copies or substantial portions of the Software.
508
-
509
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
510
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
511
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
512
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
513
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
514
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
515
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
516
-
517
- == Development
518
-
519
- In order to setup a development environment and run the specs, you'll
520
- need to install bundler:
521
-
522
- gem install bundler
523
-
524
- And then install the dependencies:
525
-
526
- bundle install
527
-
528
- You should now be able to run the tests:
529
-
530
- bundle exec rake spec
531
-
532
- Issues are reported on GitHub, pull requests are very welcome!