carrierwave 0.10.0 → 1.2.0
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.
- checksums.yaml +4 -4
- data/README.md +248 -116
- data/lib/carrierwave/compatibility/paperclip.rb +0 -2
- data/lib/carrierwave/error.rb +1 -0
- data/lib/carrierwave/locale/en.yml +7 -4
- data/lib/carrierwave/mount.rb +219 -176
- data/lib/carrierwave/mounter.rb +165 -0
- data/lib/carrierwave/orm/activerecord.rb +50 -21
- data/lib/carrierwave/processing/mini_magick.rb +82 -15
- data/lib/carrierwave/processing/rmagick.rb +47 -4
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/sanitized_file.rb +38 -18
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +65 -2
- data/lib/carrierwave/storage/fog.rb +106 -31
- data/lib/carrierwave/storage.rb +0 -7
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader/cache.rb +58 -30
- data/lib/carrierwave/uploader/callbacks.rb +0 -2
- data/lib/carrierwave/uploader/configuration.rb +51 -8
- data/lib/carrierwave/uploader/content_type_blacklist.rb +48 -0
- data/lib/carrierwave/uploader/content_type_whitelist.rb +48 -0
- data/lib/carrierwave/uploader/default_url.rb +3 -5
- data/lib/carrierwave/uploader/download.rb +15 -8
- data/lib/carrierwave/uploader/extension_blacklist.rb +14 -10
- data/lib/carrierwave/uploader/extension_whitelist.rb +13 -10
- data/lib/carrierwave/uploader/file_size.rb +43 -0
- data/lib/carrierwave/uploader/magic_mime_blacklist.rb +94 -0
- data/lib/carrierwave/uploader/magic_mime_whitelist.rb +94 -0
- data/lib/carrierwave/uploader/mountable.rb +7 -8
- data/lib/carrierwave/uploader/processing.rb +10 -10
- data/lib/carrierwave/uploader/proxy.rb +5 -7
- data/lib/carrierwave/uploader/remove.rb +0 -2
- data/lib/carrierwave/uploader/serialization.rb +1 -3
- data/lib/carrierwave/uploader/store.rb +5 -23
- data/lib/carrierwave/uploader/url.rb +3 -5
- data/lib/carrierwave/uploader/versions.rb +75 -82
- data/lib/carrierwave/uploader.rb +6 -2
- data/lib/carrierwave/utilities/uri.rb +5 -6
- data/lib/carrierwave/utilities.rb +0 -3
- data/lib/carrierwave/validations/active_model.rb +3 -5
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +12 -10
- data/lib/generators/templates/uploader.rb +4 -6
- metadata +37 -61
- data/lib/carrierwave/locale/cs.yml +0 -11
- data/lib/carrierwave/locale/de.yml +0 -11
- data/lib/carrierwave/locale/el.yml +0 -11
- data/lib/carrierwave/locale/es.yml +0 -11
- data/lib/carrierwave/locale/fr.yml +0 -11
- data/lib/carrierwave/locale/ja.yml +0 -11
- data/lib/carrierwave/locale/nb.yml +0 -11
- data/lib/carrierwave/locale/nl.yml +0 -11
- data/lib/carrierwave/locale/pl.yml +0 -11
- data/lib/carrierwave/locale/pt-BR.yml +0 -11
- data/lib/carrierwave/locale/pt-PT.yml +0 -11
- data/lib/carrierwave/locale/ru.yml +0 -11
- data/lib/carrierwave/locale/sk.yml +0 -11
- data/lib/carrierwave/locale/tr.yml +0 -11
- data/lib/carrierwave/processing/mime_types.rb +0 -74
- data/lib/carrierwave/utilities/deprecation.rb +0 -18
data/README.md
CHANGED
@@ -3,8 +3,10 @@
|
|
3
3
|
This gem provides a simple and extremely flexible way to upload files from Ruby applications.
|
4
4
|
It works well with Rack based web applications, such as Ruby on Rails.
|
5
5
|
|
6
|
-
[![Build Status](https://travis-ci.org/carrierwaveuploader/carrierwave.
|
7
|
-
[![Code Climate](
|
6
|
+
[![Build Status](https://travis-ci.org/carrierwaveuploader/carrierwave.svg?branch=master)](http://travis-ci.org/carrierwaveuploader/carrierwave)
|
7
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/carrierwaveuploader/carrierwave.svg)](https://codeclimate.com/github/carrierwaveuploader/carrierwave)
|
8
|
+
[![git.legal](https://git.legal/projects/1363/badge.svg "Number of libraries approved")](https://git.legal/projects/1363)
|
9
|
+
|
8
10
|
|
9
11
|
## Information
|
10
12
|
|
@@ -14,25 +16,27 @@ It works well with Rack based web applications, such as Ruby on Rails.
|
|
14
16
|
|
15
17
|
## Getting Help
|
16
18
|
|
17
|
-
* Please ask the [
|
19
|
+
* Please ask the community on [Stack Overflow](https://stackoverflow.com/questions/tagged/carrierwave) for help if you have any questions. Please do not post usage questions on the issue tracker.
|
18
20
|
* Please report bugs on the [issue tracker](http://github.com/carrierwaveuploader/carrierwave/issues) but read the "getting help" section in the wiki first.
|
19
21
|
|
20
22
|
## Installation
|
21
23
|
|
22
|
-
Install the latest
|
24
|
+
Install the latest release:
|
23
25
|
|
24
|
-
|
26
|
+
```
|
27
|
+
$ gem install carrierwave -v "1.0.0"
|
28
|
+
```
|
25
29
|
|
26
30
|
In Rails, add it to your Gemfile:
|
27
31
|
|
28
32
|
```ruby
|
29
|
-
gem 'carrierwave'
|
33
|
+
gem 'carrierwave', '~> 1.0'
|
30
34
|
```
|
31
35
|
|
32
36
|
Finally, restart the server to apply the changes.
|
33
37
|
|
34
|
-
|
35
|
-
Rails
|
38
|
+
As of version 1.0, CarrierWave requires Rails 4.0 or higher and Ruby 2.0
|
39
|
+
or higher. If you're on Rails 3, you should use v0.11.0.
|
36
40
|
|
37
41
|
## Getting Started
|
38
42
|
|
@@ -100,24 +104,89 @@ automatically be stored when the record is saved.
|
|
100
104
|
|
101
105
|
```ruby
|
102
106
|
u = User.new
|
103
|
-
u.avatar = params[:file]
|
104
|
-
|
107
|
+
u.avatar = params[:file] # Assign a file like this, or
|
108
|
+
|
109
|
+
# like this
|
110
|
+
File.open('somewhere') do |f|
|
111
|
+
u.avatar = f
|
112
|
+
end
|
113
|
+
|
105
114
|
u.save!
|
106
115
|
u.avatar.url # => '/url/to/file.png'
|
107
116
|
u.avatar.current_path # => 'path/to/file.png'
|
108
|
-
u.
|
117
|
+
u.avatar_identifier # => 'file.png'
|
109
118
|
```
|
110
119
|
|
120
|
+
**Note**: `u.avatar` will never return nil, even if there is no photo associated to it.
|
121
|
+
To check if a photo was saved to the model, use `u.avatar.file.nil?` instead.
|
122
|
+
|
111
123
|
### DataMapper, Mongoid, Sequel
|
112
124
|
|
113
125
|
Other ORM support has been extracted into separate gems:
|
114
126
|
|
115
127
|
* [carrierwave-datamapper](https://github.com/carrierwaveuploader/carrierwave-datamapper)
|
116
128
|
* [carrierwave-mongoid](https://github.com/carrierwaveuploader/carrierwave-mongoid)
|
117
|
-
* [carrierwave-sequel](https://github.com/
|
129
|
+
* [carrierwave-sequel](https://github.com/carrierwaveuploader/carrierwave-sequel)
|
118
130
|
|
119
131
|
There are more extensions listed in [the wiki](https://github.com/carrierwaveuploader/carrierwave/wiki)
|
120
132
|
|
133
|
+
## Multiple file uploads
|
134
|
+
|
135
|
+
CarrierWave also has convenient support for multiple file upload fields.
|
136
|
+
|
137
|
+
### ActiveRecord
|
138
|
+
|
139
|
+
Add a column which can store an array. This could be an array column or a JSON
|
140
|
+
column for example. Your choice depends on what your database supports. For
|
141
|
+
example, create a migration like this:
|
142
|
+
|
143
|
+
|
144
|
+
#### For databases with ActiveRecord json data type support (e.g. PostgreSQL, MySQL)
|
145
|
+
|
146
|
+
rails g migration add_avatars_to_users avatars:json
|
147
|
+
rake db:migrate
|
148
|
+
|
149
|
+
#### For database without ActiveRecord json data type support (e.g. SQLite)
|
150
|
+
|
151
|
+
rails g migration add_avatars_to_users avatars:string
|
152
|
+
rake db:migrate
|
153
|
+
|
154
|
+
__Note__: JSON datatype doesn't exists in SQLite adapter, that's why you can use a string datatype which will be serialized in model.
|
155
|
+
|
156
|
+
Open your model file and mount the uploader:
|
157
|
+
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
class User < ActiveRecord::Base
|
161
|
+
mount_uploaders :avatars, AvatarUploader
|
162
|
+
serialize :avatars, JSON # If you use SQLite, add this line.
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
Make sure your file input fields are set up as multiple file fields. For
|
167
|
+
example in Rails you'll want to do something like this:
|
168
|
+
|
169
|
+
```erb
|
170
|
+
<%= form.file_field :avatars, multiple: true %>
|
171
|
+
```
|
172
|
+
|
173
|
+
Also, make sure your upload controller permits the multiple file upload attribute, *pointing to an empty array in a hash*. For example:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
|
177
|
+
```
|
178
|
+
|
179
|
+
Now you can select multiple files in the upload dialog (e.g. SHIFT+SELECT), and they will
|
180
|
+
automatically be stored when the record is saved.
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
u = User.new(params[:user])
|
184
|
+
u.save!
|
185
|
+
u.avatars[0].url # => '/url/to/file.png'
|
186
|
+
u.avatars[0].current_path # => 'path/to/file.png'
|
187
|
+
u.avatars[0].identifier # => 'file.png'
|
188
|
+
```
|
189
|
+
|
121
190
|
## Changing the storage directory
|
122
191
|
|
123
192
|
In order to change where uploaded files are put, just override the `store_dir`
|
@@ -146,69 +215,69 @@ end
|
|
146
215
|
|
147
216
|
## Securing uploads
|
148
217
|
|
149
|
-
Certain
|
150
|
-
files or other script files. CarrierWave allows you to specify a
|
151
|
-
allowed extensions.
|
218
|
+
Certain files might be dangerous if uploaded to the wrong location, such as PHP
|
219
|
+
files or other script files. CarrierWave allows you to specify a whitelist of
|
220
|
+
allowed extensions or content types.
|
152
221
|
|
153
222
|
If you're mounting the uploader, uploading a file with the wrong extension will
|
154
223
|
make the record invalid instead. Otherwise, an error is raised.
|
155
224
|
|
156
225
|
```ruby
|
157
226
|
class MyUploader < CarrierWave::Uploader::Base
|
158
|
-
def
|
227
|
+
def extension_whitelist
|
159
228
|
%w(jpg jpeg gif png)
|
160
229
|
end
|
161
230
|
end
|
162
231
|
```
|
163
232
|
|
233
|
+
The same thing could be done using content types.
|
234
|
+
Let's say we need an uploader that accepts only images. This can be done like this
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
class MyUploader < CarrierWave::Uploader::Base
|
238
|
+
def content_type_whitelist
|
239
|
+
/image\//
|
240
|
+
end
|
241
|
+
end
|
242
|
+
```
|
243
|
+
|
244
|
+
You can use a blacklist to reject content types.
|
245
|
+
Let's say we need an uploader that reject JSON files. This can be done like this
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
class NoJsonUploader < CarrierWave::Uploader::Base
|
249
|
+
def content_type_blacklist
|
250
|
+
['application/text', 'application/json']
|
251
|
+
end
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
164
255
|
### Filenames and unicode chars
|
165
256
|
|
166
257
|
Another security issue you should care for is the file names (see
|
167
258
|
[Ruby On Rails Security Guide](http://guides.rubyonrails.org/security.html#file-uploads)).
|
168
|
-
By default, CarrierWave provides only English letters, arabic numerals and
|
259
|
+
By default, CarrierWave provides only English letters, arabic numerals and some symbols as
|
169
260
|
white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you
|
170
261
|
have to override `sanitize_regexp` method. It should return regular expression which would match
|
171
262
|
all *non*-allowed symbols.
|
172
263
|
|
173
|
-
With Ruby 1.9 and higher you can simply write (as it has [Oniguruma](http://oniguruma.rubyforge.org/oniguruma/)
|
174
|
-
built-in):
|
175
|
-
|
176
264
|
```ruby
|
177
265
|
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
|
178
266
|
```
|
179
267
|
|
180
|
-
With Ruby 1.8 you have to manually specify all character ranges. For example, for files which may
|
181
|
-
contain Russian letters:
|
182
|
-
|
183
|
-
```ruby
|
184
|
-
CarrierWave::SanitizedFile.sanitize_regexp = /[^a-zA-Zа-яА-ЯёЁ0-9\.\-\+_]/u
|
185
|
-
```
|
186
|
-
|
187
268
|
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
|
188
269
|
plugins or client-side software.
|
189
270
|
|
190
271
|
## Setting the content type
|
191
272
|
|
192
|
-
|
193
|
-
|
194
|
-
This adds a dependency on the [mime-types](http://rubygems.org/gems/mime-types) gem,
|
195
|
-
but is recommended when using fog, and fog already has a dependency on mime-types.
|
196
|
-
|
197
|
-
```ruby
|
198
|
-
require 'carrierwave/processing/mime_types'
|
199
|
-
|
200
|
-
class MyUploader < CarrierWave::Uploader::Base
|
201
|
-
include CarrierWave::MimeTypes
|
202
|
-
|
203
|
-
process :set_content_type
|
204
|
-
end
|
205
|
-
```
|
273
|
+
As of v0.11.0, the `mime-types` gem is a runtime dependency and the content type is set automatically.
|
274
|
+
You no longer need to do this manually.
|
206
275
|
|
207
276
|
## Adding versions
|
208
277
|
|
209
278
|
Often you'll want to add different versions of the same file. The classic example is image thumbnails. There is built in support for this*:
|
210
279
|
|
211
|
-
*Note
|
280
|
+
*Note:* You must have Imagemagick and MiniMagick installed to do image resizing. MiniMagick is a Ruby interface for Imagemagick which is a C program. This is why MiniMagick fails on 'bundle install' without Imagemagick installed.
|
212
281
|
|
213
282
|
Some documentation refers to RMagick instead of MiniMagick but MiniMagick is recommended.
|
214
283
|
|
@@ -222,10 +291,10 @@ $ brew install imagemagick
|
|
222
291
|
class MyUploader < CarrierWave::Uploader::Base
|
223
292
|
include CarrierWave::MiniMagick
|
224
293
|
|
225
|
-
process :
|
294
|
+
process resize_to_fit: [800, 800]
|
226
295
|
|
227
296
|
version :thumb do
|
228
|
-
process :
|
297
|
+
process resize_to_fill: [200,200]
|
229
298
|
end
|
230
299
|
|
231
300
|
end
|
@@ -239,7 +308,7 @@ and cropped to exactly 200 by 200 pixels. The uploader could be used like this:
|
|
239
308
|
uploader = AvatarUploader.new
|
240
309
|
uploader.store!(my_file) # size: 1024x768
|
241
310
|
|
242
|
-
uploader.url # => '/url/to/my_file.png' # size:
|
311
|
+
uploader.url # => '/url/to/my_file.png' # size: 800x800
|
243
312
|
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
|
244
313
|
```
|
245
314
|
|
@@ -267,11 +336,11 @@ properties within the model or based on the picture itself.
|
|
267
336
|
```ruby
|
268
337
|
class MyUploader < CarrierWave::Uploader::Base
|
269
338
|
|
270
|
-
version :human, :
|
271
|
-
version :monkey, :
|
272
|
-
version :banner, :
|
339
|
+
version :human, if: :is_human?
|
340
|
+
version :monkey, if: :is_monkey?
|
341
|
+
version :banner, if: :is_landscape?
|
273
342
|
|
274
|
-
|
343
|
+
private
|
275
344
|
|
276
345
|
def is_human? picture
|
277
346
|
model.can_program?(:ruby)
|
@@ -305,7 +374,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
305
374
|
process resize_to_fill: [280, 280]
|
306
375
|
end
|
307
376
|
|
308
|
-
version :small_thumb, :
|
377
|
+
version :small_thumb, from_version: :thumb do
|
309
378
|
process resize_to_fill: [20, 20]
|
310
379
|
end
|
311
380
|
|
@@ -324,7 +393,7 @@ file, just add a hidden field called `avatar_cache` (don't forget to add it to
|
|
324
393
|
the attr_accessible list as necessary). In Rails, this would look like this:
|
325
394
|
|
326
395
|
```erb
|
327
|
-
<%= form_for @user, :
|
396
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
328
397
|
<p>
|
329
398
|
<label>My Avatar</label>
|
330
399
|
<%= f.file_field :avatar %>
|
@@ -337,7 +406,7 @@ It might be a good idea to show the user that a file has been uploaded, in the
|
|
337
406
|
case of images, a small thumbnail would be a good indicator:
|
338
407
|
|
339
408
|
```erb
|
340
|
-
<%= form_for @user, :
|
409
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
341
410
|
<p>
|
342
411
|
<label>My Avatar</label>
|
343
412
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -353,7 +422,7 @@ If you want to remove a previously uploaded file on a mounted uploader, you can
|
|
353
422
|
easily add a checkbox to the form which will remove the file when checked.
|
354
423
|
|
355
424
|
```erb
|
356
|
-
<%= form_for @user, :
|
425
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
357
426
|
<p>
|
358
427
|
<label>My Avatar</label>
|
359
428
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -384,7 +453,7 @@ via a URL. CarrierWave makes this simple, just add the appropriate attribute to
|
|
384
453
|
form and you're good to go:
|
385
454
|
|
386
455
|
```erb
|
387
|
-
<%= form_for @user, :
|
456
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
388
457
|
<p>
|
389
458
|
<label>My Avatar URL:</label>
|
390
459
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -406,7 +475,7 @@ this easily by overriding the `default_url` method in your uploader:
|
|
406
475
|
|
407
476
|
```ruby
|
408
477
|
class MyUploader < CarrierWave::Uploader::Base
|
409
|
-
def default_url
|
478
|
+
def default_url(*args)
|
410
479
|
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
|
411
480
|
end
|
412
481
|
end
|
@@ -416,7 +485,7 @@ Or if you are using the Rails asset pipeline:
|
|
416
485
|
|
417
486
|
```ruby
|
418
487
|
class MyUploader < CarrierWave::Uploader::Base
|
419
|
-
def default_url
|
488
|
+
def default_url(*args)
|
420
489
|
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
421
490
|
end
|
422
491
|
end
|
@@ -443,7 +512,7 @@ instance.recreate_versions!(:thumb, :large)
|
|
443
512
|
Or on a mounted uploader:
|
444
513
|
|
445
514
|
```ruby
|
446
|
-
User.
|
515
|
+
User.find_each do |user|
|
447
516
|
user.avatar.recreate_versions!
|
448
517
|
end
|
449
518
|
```
|
@@ -451,7 +520,7 @@ end
|
|
451
520
|
Note: `recreate_versions!` will throw an exception on records without an image. To avoid this, scope the records to those with images or check if an image exists within the block. If you're using ActiveRecord, recreating versions for a user avatar might look like this:
|
452
521
|
|
453
522
|
```ruby
|
454
|
-
User.
|
523
|
+
User.find_each do |user|
|
455
524
|
user.avatar.recreate_versions! if user.avatar?
|
456
525
|
end
|
457
526
|
```
|
@@ -481,6 +550,16 @@ If you're using Rails, create an initializer for this:
|
|
481
550
|
|
482
551
|
config/initializers/carrierwave.rb
|
483
552
|
|
553
|
+
If you want CarrierWave to fail noisily in development, you can change these configs in your environment file:
|
554
|
+
|
555
|
+
```ruby
|
556
|
+
CarrierWave.configure do |config|
|
557
|
+
config.ignore_integrity_errors = false
|
558
|
+
config.ignore_processing_errors = false
|
559
|
+
config.ignore_download_errors = false
|
560
|
+
end
|
561
|
+
```
|
562
|
+
|
484
563
|
|
485
564
|
## Testing with CarrierWave
|
486
565
|
|
@@ -511,31 +590,37 @@ require 'carrierwave/test/matchers'
|
|
511
590
|
describe MyUploader do
|
512
591
|
include CarrierWave::Test::Matchers
|
513
592
|
|
593
|
+
let(:user) { double('user') }
|
594
|
+
let(:uploader) { MyUploader.new(user, :avatar) }
|
595
|
+
|
514
596
|
before do
|
515
597
|
MyUploader.enable_processing = true
|
516
|
-
|
517
|
-
@uploader.store!(File.open(path_to_file))
|
598
|
+
File.open(path_to_file) { |f| uploader.store!(f) }
|
518
599
|
end
|
519
600
|
|
520
601
|
after do
|
521
602
|
MyUploader.enable_processing = false
|
522
|
-
|
603
|
+
uploader.remove!
|
523
604
|
end
|
524
605
|
|
525
606
|
context 'the thumb version' do
|
526
|
-
it "
|
527
|
-
|
607
|
+
it "scales down a landscape image to be exactly 64 by 64 pixels" do
|
608
|
+
expect(uploader.thumb).to have_dimensions(64, 64)
|
528
609
|
end
|
529
610
|
end
|
530
611
|
|
531
612
|
context 'the small version' do
|
532
|
-
it "
|
533
|
-
|
613
|
+
it "scales down a landscape image to fit within 200 by 200 pixels" do
|
614
|
+
expect(uploader.small).to be_no_larger_than(200, 200)
|
534
615
|
end
|
535
616
|
end
|
536
617
|
|
537
|
-
it "
|
538
|
-
|
618
|
+
it "makes the image readable only to the owner and not executable" do
|
619
|
+
expect(uploader).to have_permissions(0600)
|
620
|
+
end
|
621
|
+
|
622
|
+
it "has the correct format" do
|
623
|
+
expect(uploader).to be_format('png')
|
539
624
|
end
|
540
625
|
end
|
541
626
|
```
|
@@ -547,31 +632,42 @@ Processing can be enabled for a single version by setting the processing flag on
|
|
547
632
|
@uploader.thumb.enable_processing = true
|
548
633
|
```
|
549
634
|
|
635
|
+
## Fog
|
636
|
+
|
637
|
+
If you want to use fog you must add in your CarrierWave initializer the
|
638
|
+
following lines
|
639
|
+
|
640
|
+
```ruby
|
641
|
+
config.fog_provider = 'fog' # 'fog/aws' etc. Defaults to 'fog'
|
642
|
+
config.fog_credentials = { ... } # Provider specific credentials
|
643
|
+
```
|
644
|
+
|
550
645
|
## Using Amazon S3
|
551
646
|
|
552
|
-
[Fog](http://github.com/fog/fog) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
647
|
+
[Fog AWS](http://github.com/fog/fog-aws) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
553
648
|
|
554
649
|
```ruby
|
555
|
-
gem "fog"
|
650
|
+
gem "fog-aws"
|
556
651
|
```
|
557
652
|
|
558
653
|
You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.
|
559
|
-
For the sake of performance it is assumed that the directory already exists, so please create it if
|
654
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if it needs to be.
|
560
655
|
You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:
|
561
656
|
|
562
657
|
```ruby
|
563
658
|
CarrierWave.configure do |config|
|
659
|
+
config.fog_provider = 'fog/aws' # required
|
564
660
|
config.fog_credentials = {
|
565
|
-
:
|
566
|
-
:
|
567
|
-
:
|
568
|
-
:
|
569
|
-
:
|
570
|
-
:
|
661
|
+
provider: 'AWS', # required
|
662
|
+
aws_access_key_id: 'xxx', # required
|
663
|
+
aws_secret_access_key: 'yyy', # required
|
664
|
+
region: 'eu-west-1', # optional, defaults to 'us-east-1'
|
665
|
+
host: 's3.example.com', # optional, defaults to nil
|
666
|
+
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
|
571
667
|
}
|
572
|
-
config.fog_directory = 'name_of_directory'
|
573
|
-
config.fog_public = false
|
574
|
-
config.fog_attributes = {
|
668
|
+
config.fog_directory = 'name_of_directory' # required
|
669
|
+
config.fog_public = false # optional, defaults to true
|
670
|
+
config.fog_attributes = { cache_control: "public, max-age=#{365.day.to_i}" } # optional, defaults to {}
|
575
671
|
end
|
576
672
|
```
|
577
673
|
|
@@ -600,11 +696,12 @@ Using a US-based account:
|
|
600
696
|
|
601
697
|
```ruby
|
602
698
|
CarrierWave.configure do |config|
|
699
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
603
700
|
config.fog_credentials = {
|
604
|
-
:
|
605
|
-
:
|
606
|
-
:
|
607
|
-
:
|
701
|
+
provider: 'Rackspace',
|
702
|
+
rackspace_username: 'xxxxxx',
|
703
|
+
rackspace_api_key: 'yyyyyy',
|
704
|
+
rackspace_region: :ord # optional, defaults to :dfw
|
608
705
|
}
|
609
706
|
config.fog_directory = 'name_of_directory'
|
610
707
|
end
|
@@ -614,12 +711,13 @@ Using a UK-based account:
|
|
614
711
|
|
615
712
|
```ruby
|
616
713
|
CarrierWave.configure do |config|
|
714
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
617
715
|
config.fog_credentials = {
|
618
|
-
:
|
619
|
-
:
|
620
|
-
:
|
621
|
-
:
|
622
|
-
:
|
716
|
+
provider: 'Rackspace',
|
717
|
+
rackspace_username: 'xxxxxx',
|
718
|
+
rackspace_api_key: 'yyyyyy',
|
719
|
+
rackspace_auth_url: Fog::Rackspace::UK_AUTH_ENDPOINT,
|
720
|
+
rackspace_region: :lon
|
623
721
|
}
|
624
722
|
config.fog_directory = 'name_of_directory'
|
625
723
|
end
|
@@ -646,25 +744,27 @@ the url to the file on Rackspace Cloud Files.
|
|
646
744
|
|
647
745
|
## Using Google Storage for Developers
|
648
746
|
|
649
|
-
[Fog](http://github.com/fog/fog) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
747
|
+
[Fog](http://github.com/fog/fog-google) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
650
748
|
|
651
749
|
```ruby
|
652
|
-
gem "fog"
|
750
|
+
gem "fog-google"
|
751
|
+
gem "google-api-client", "> 0.8.5", "< 0.9"
|
752
|
+
gem "mime-types"
|
653
753
|
```
|
654
754
|
|
655
755
|
You'll need to configure a directory (also known as a bucket), access key id and secret access key in the initializer.
|
656
756
|
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
657
757
|
|
658
|
-
|
659
|
-
under the section “Interoperable Access”.
|
758
|
+
Please read the [fog-google README](https://github.com/fog/fog-google/blob/master/README.md) on how to get credentials.
|
660
759
|
|
661
760
|
|
662
761
|
```ruby
|
663
762
|
CarrierWave.configure do |config|
|
763
|
+
config.fog_provider = 'fog/google' # required
|
664
764
|
config.fog_credentials = {
|
665
|
-
:
|
666
|
-
:
|
667
|
-
:
|
765
|
+
provider: 'Google',
|
766
|
+
google_storage_access_key_id: 'xxxxxx',
|
767
|
+
google_storage_secret_access_key: 'yyyyyy'
|
668
768
|
}
|
669
769
|
config.fog_directory = 'name_of_directory'
|
670
770
|
end
|
@@ -681,6 +781,31 @@ end
|
|
681
781
|
That's it! You can still use the `CarrierWave::Uploader#url` method to return
|
682
782
|
the url to the file on Google.
|
683
783
|
|
784
|
+
## Optimized Loading of Fog
|
785
|
+
|
786
|
+
Since Carrierwave doesn't know which parts of Fog you intend to use, it will just load the entire library (unless you use e.g. [`fog-aws`, `fog-google`] instead of fog proper). If you prefer to load fewer classes into your application, you need to load those parts of Fog yourself *before* loading CarrierWave in your Gemfile. Ex:
|
787
|
+
|
788
|
+
```ruby
|
789
|
+
gem "fog", "~> 1.27", require: "fog/rackspace/storage"
|
790
|
+
gem "carrierwave"
|
791
|
+
```
|
792
|
+
|
793
|
+
A couple of notes about versions:
|
794
|
+
* This functionality was introduced in Fog v1.20.
|
795
|
+
* This functionality is slated for CarrierWave v1.0.0.
|
796
|
+
|
797
|
+
If you're not relying on Gemfile entries alone and are requiring "carrierwave" anywhere, ensure you require "fog/rackspace/storage" before it. Ex:
|
798
|
+
|
799
|
+
```ruby
|
800
|
+
require "fog/rackspace/storage"
|
801
|
+
require "carrierwave"
|
802
|
+
```
|
803
|
+
|
804
|
+
Beware that this specific require is only needed when working with a fog provider that was not extracted to its own gem yet.
|
805
|
+
A list of the extracted providers can be found in the page of the `fog` organizations [here](https://github.com/fog).
|
806
|
+
|
807
|
+
When in doubt, inspect `Fog.constants` to see what has been loaded.
|
808
|
+
|
684
809
|
## Dynamic Asset Host
|
685
810
|
|
686
811
|
The `asset_host` config property can be assigned a proc (or anything that responds to `call`) for generating the host dynamically. The proc-compliant object gets an instance of the current `CarrierWave::Storage::Fog::File` or `CarrierWave::SanitizedFile` as its only argument.
|
@@ -718,8 +843,8 @@ Convert will only work if the file has the same file extension, thus the use of
|
|
718
843
|
class AvatarUploader < CarrierWave::Uploader::Base
|
719
844
|
include CarrierWave::RMagick
|
720
845
|
|
721
|
-
process :
|
722
|
-
process :
|
846
|
+
process resize_to_fill: [200, 200]
|
847
|
+
process convert: 'png'
|
723
848
|
|
724
849
|
def filename
|
725
850
|
super.chomp(File.extname(super)) + '.png' if original_filename.present?
|
@@ -751,7 +876,7 @@ for the RMagick processor.
|
|
751
876
|
class AvatarUploader < CarrierWave::Uploader::Base
|
752
877
|
include CarrierWave::MiniMagick
|
753
878
|
|
754
|
-
process :
|
879
|
+
process resize_to_fill: [200, 200]
|
755
880
|
end
|
756
881
|
```
|
757
882
|
|
@@ -771,27 +896,32 @@ details.
|
|
771
896
|
Be sure to use mount_on to specify the correct column:
|
772
897
|
|
773
898
|
```ruby
|
774
|
-
mount_uploader :avatar, AvatarUploader, :
|
899
|
+
mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
|
775
900
|
```
|
776
901
|
|
777
|
-
|
778
|
-
sensible compatibility mode. Patches for migrating from other solutions will be
|
779
|
-
happily accepted.
|
902
|
+
## I18n
|
780
903
|
|
781
|
-
|
782
|
-
|
783
|
-
The Active Record validations use the Rails i18n framework. Add these keys to
|
904
|
+
The Active Record validations use the Rails `i18n` framework. Add these keys to
|
784
905
|
your translations file:
|
785
906
|
|
786
907
|
```yaml
|
787
908
|
errors:
|
788
909
|
messages:
|
789
|
-
carrierwave_processing_error:
|
790
|
-
carrierwave_integrity_error:
|
791
|
-
carrierwave_download_error:
|
792
|
-
|
793
|
-
|
794
|
-
|
910
|
+
carrierwave_processing_error: failed to be processed
|
911
|
+
carrierwave_integrity_error: is not of an allowed file type
|
912
|
+
carrierwave_download_error: could not be downloaded
|
913
|
+
extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
|
914
|
+
extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
|
915
|
+
content_type_whitelist_error: "You are not allowed to upload %{content_type} files"
|
916
|
+
content_type_blacklist_error: "You are not allowed to upload %{content_type} files"
|
917
|
+
rmagick_processing_error: "Failed to manipulate with rmagick, maybe it is not an image?"
|
918
|
+
mini_magick_processing_error: "Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
|
919
|
+
min_size_error: "File size should be greater than %{min_size}"
|
920
|
+
max_size_error: "File size should be less than %{max_size}"
|
921
|
+
```
|
922
|
+
|
923
|
+
The [`carrierwave-i18n`](https://github.com/carrierwaveuploader/carrierwave-i18n)
|
924
|
+
library adds support for additional locales.
|
795
925
|
|
796
926
|
## Large files
|
797
927
|
|
@@ -806,6 +936,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
806
936
|
def move_to_cache
|
807
937
|
true
|
808
938
|
end
|
939
|
+
|
809
940
|
def move_to_store
|
810
941
|
true
|
811
942
|
end
|
@@ -832,9 +963,10 @@ Will add these callbacks:
|
|
832
963
|
```ruby
|
833
964
|
after_save :store_avatar!
|
834
965
|
before_save :write_avatar_identifier
|
835
|
-
after_commit :remove_avatar
|
836
|
-
|
837
|
-
after_save :
|
966
|
+
after_commit :remove_avatar!, on: :destroy
|
967
|
+
after_commit :mark_remove_avatar_false, on: :update
|
968
|
+
after_save :store_previous_changes_for_avatar
|
969
|
+
after_commit :remove_previously_stored_avatar, on: :update
|
838
970
|
```
|
839
971
|
|
840
972
|
If you want to skip any of these callbacks (eg. you want to keep the existing
|
@@ -844,7 +976,7 @@ avatar, even after uploading a new one), you can use ActiveRecord’s
|
|
844
976
|
```ruby
|
845
977
|
class User
|
846
978
|
mount_uploader :avatar, AvatarUploader
|
847
|
-
skip_callback :
|
979
|
+
skip_callback :commit, :after, :remove_previously_stored_avatar
|
848
980
|
end
|
849
981
|
```
|
850
982
|
|
@@ -854,7 +986,7 @@ See [CONTRIBUTING.md](https://github.com/carrierwaveuploader/carrierwave/blob/ma
|
|
854
986
|
|
855
987
|
## License
|
856
988
|
|
857
|
-
Copyright (c) 2008-
|
989
|
+
Copyright (c) 2008-2015 Jonas Nicklas
|
858
990
|
|
859
991
|
Permission is hereby granted, free of charge, to any person obtaining
|
860
992
|
a copy of this software and associated documentation files (the
|