carrierwave 0.11.0 → 1.0.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 +225 -110
- 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 +217 -176
- data/lib/carrierwave/mounter.rb +164 -0
- data/lib/carrierwave/orm/activerecord.rb +49 -20
- data/lib/carrierwave/processing/mini_magick.rb +64 -13
- data/lib/carrierwave/processing/rmagick.rb +31 -3
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/sanitized_file.rb +37 -17
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +65 -2
- data/lib/carrierwave/storage/fog.rb +101 -29
- data/lib/carrierwave/storage.rb +0 -7
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader/cache.rb +40 -26
- data/lib/carrierwave/uploader/callbacks.rb +0 -2
- data/lib/carrierwave/uploader/configuration.rb +48 -6
- 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 +10 -7
- data/lib/carrierwave/uploader/extension_blacklist.rb +14 -10
- data/lib/carrierwave/uploader/extension_whitelist.rb +12 -10
- data/lib/carrierwave/uploader/file_size.rb +41 -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 +1 -3
- 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 +1 -3
- data/lib/carrierwave/uploader/versions.rb +75 -82
- data/lib/carrierwave/uploader.rb +6 -2
- data/lib/carrierwave/utilities/uri.rb +4 -7
- data/lib/carrierwave/utilities.rb +0 -3
- data/lib/carrierwave/validations/active_model.rb +0 -2
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +8 -10
- data/lib/generators/templates/uploader.rb +3 -5
- metadata +43 -81
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e8aa98fa4de1f1cb517138ae11e7bfbd574638e
|
4
|
+
data.tar.gz: 5456c89662d0ca78e61cc12c8d9305b23175172e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4ba35c722eeef4ccdf925adf951c1323dad0c116e0f4cbf7e567cbeefacb538e7259e5a59c771d17e05ced3a0d182bfb15000ca982edb3bce6f51fa288255eb
|
7
|
+
data.tar.gz: d9dbc8490ce0637ab6aa828eb695170637317141e0b3d901fefb15dfdf8b1f52094e4d7911e34182f607a185b5ebb06d460f047fb7a080f850da5d8a482c9321
|
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](http://stackoverflow.com/) 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,81 @@ 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
|
+
**Note:** You must specify using the master branch to enable this feature:
|
135
|
+
|
136
|
+
`gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'`.
|
137
|
+
|
138
|
+
CarrierWave also has convenient support for multiple file upload fields.
|
139
|
+
|
140
|
+
### ActiveRecord
|
141
|
+
|
142
|
+
Add a column which can store an array. This could be an array column or a JSON
|
143
|
+
column for example. Your choice depends on what your database supports. For
|
144
|
+
example, create a migration like this:
|
145
|
+
|
146
|
+
|
147
|
+
rails g migration add_avatars_to_users avatars:json
|
148
|
+
rake db:migrate
|
149
|
+
|
150
|
+
Open your model file and mount the uploader:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
class User < ActiveRecord::Base
|
154
|
+
mount_uploaders :avatars, AvatarUploader
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
Make sure your file input fields are set up as multiple file fields. For
|
159
|
+
example in Rails you'll want to do something like this:
|
160
|
+
|
161
|
+
```erb
|
162
|
+
<%= form.file_field :avatars, multiple: true %>
|
163
|
+
```
|
164
|
+
|
165
|
+
Also, make sure your upload controller permits the multiple file upload attribute, *pointing to an empty array in a hash*. For example:
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
|
169
|
+
```
|
170
|
+
|
171
|
+
Now you can select multiple files in the upload dialog (e.g. SHIFT+SELECT), and they will
|
172
|
+
automatically be stored when the record is saved.
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
u = User.new(params[:user])
|
176
|
+
u.save!
|
177
|
+
u.avatars[0].url # => '/url/to/file.png'
|
178
|
+
u.avatars[0].current_path # => 'path/to/file.png'
|
179
|
+
u.avatars[0].identifier # => 'file.png'
|
180
|
+
```
|
181
|
+
|
121
182
|
## Changing the storage directory
|
122
183
|
|
123
184
|
In order to change where uploaded files are put, just override the `store_dir`
|
@@ -146,69 +207,69 @@ end
|
|
146
207
|
|
147
208
|
## Securing uploads
|
148
209
|
|
149
|
-
Certain
|
150
|
-
files or other script files. CarrierWave allows you to specify a
|
151
|
-
allowed extensions.
|
210
|
+
Certain files might be dangerous if uploaded to the wrong location, such as PHP
|
211
|
+
files or other script files. CarrierWave allows you to specify a whitelist of
|
212
|
+
allowed extensions or content types.
|
152
213
|
|
153
214
|
If you're mounting the uploader, uploading a file with the wrong extension will
|
154
215
|
make the record invalid instead. Otherwise, an error is raised.
|
155
216
|
|
156
217
|
```ruby
|
157
218
|
class MyUploader < CarrierWave::Uploader::Base
|
158
|
-
def
|
219
|
+
def extension_whitelist
|
159
220
|
%w(jpg jpeg gif png)
|
160
221
|
end
|
161
222
|
end
|
162
223
|
```
|
163
224
|
|
225
|
+
The same thing could be done using content types.
|
226
|
+
Let's say we need an uploader that accepts only images. This can be done like this
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
class MyUploader < CarrierWave::Uploader::Base
|
230
|
+
def content_type_whitelist
|
231
|
+
/image\//
|
232
|
+
end
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
You can use a blacklist to reject content types.
|
237
|
+
Let's say we need an uploader that reject JSON files. This can be done like this
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
class NoJsonUploader < CarrierWave::Uploader::Base
|
241
|
+
def content_type_blacklist
|
242
|
+
['application/text', 'application/json']
|
243
|
+
end
|
244
|
+
end
|
245
|
+
```
|
246
|
+
|
164
247
|
### Filenames and unicode chars
|
165
248
|
|
166
249
|
Another security issue you should care for is the file names (see
|
167
250
|
[Ruby On Rails Security Guide](http://guides.rubyonrails.org/security.html#file-uploads)).
|
168
|
-
By default, CarrierWave provides only English letters, arabic numerals and
|
251
|
+
By default, CarrierWave provides only English letters, arabic numerals and some symbols as
|
169
252
|
white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you
|
170
253
|
have to override `sanitize_regexp` method. It should return regular expression which would match
|
171
254
|
all *non*-allowed symbols.
|
172
255
|
|
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
256
|
```ruby
|
177
257
|
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
|
178
258
|
```
|
179
259
|
|
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
260
|
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
|
188
261
|
plugins or client-side software.
|
189
262
|
|
190
263
|
## Setting the content type
|
191
264
|
|
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
|
-
```
|
265
|
+
As of v0.11.0, the `mime-types` gem is a runtime dependency and the content type is set automatically.
|
266
|
+
You no longer need to do this manually.
|
206
267
|
|
207
268
|
## Adding versions
|
208
269
|
|
209
270
|
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
271
|
|
211
|
-
*Note
|
272
|
+
*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
273
|
|
213
274
|
Some documentation refers to RMagick instead of MiniMagick but MiniMagick is recommended.
|
214
275
|
|
@@ -222,10 +283,10 @@ $ brew install imagemagick
|
|
222
283
|
class MyUploader < CarrierWave::Uploader::Base
|
223
284
|
include CarrierWave::MiniMagick
|
224
285
|
|
225
|
-
process :
|
286
|
+
process resize_to_fit: [800, 800]
|
226
287
|
|
227
288
|
version :thumb do
|
228
|
-
process :
|
289
|
+
process resize_to_fill: [200,200]
|
229
290
|
end
|
230
291
|
|
231
292
|
end
|
@@ -267,11 +328,11 @@ properties within the model or based on the picture itself.
|
|
267
328
|
```ruby
|
268
329
|
class MyUploader < CarrierWave::Uploader::Base
|
269
330
|
|
270
|
-
version :human, :
|
271
|
-
version :monkey, :
|
272
|
-
version :banner, :
|
331
|
+
version :human, if: :is_human?
|
332
|
+
version :monkey, if: :is_monkey?
|
333
|
+
version :banner, if: :is_landscape?
|
273
334
|
|
274
|
-
|
335
|
+
private
|
275
336
|
|
276
337
|
def is_human? picture
|
277
338
|
model.can_program?(:ruby)
|
@@ -305,7 +366,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
305
366
|
process resize_to_fill: [280, 280]
|
306
367
|
end
|
307
368
|
|
308
|
-
version :small_thumb, :
|
369
|
+
version :small_thumb, from_version: :thumb do
|
309
370
|
process resize_to_fill: [20, 20]
|
310
371
|
end
|
311
372
|
|
@@ -324,7 +385,7 @@ file, just add a hidden field called `avatar_cache` (don't forget to add it to
|
|
324
385
|
the attr_accessible list as necessary). In Rails, this would look like this:
|
325
386
|
|
326
387
|
```erb
|
327
|
-
<%= form_for @user, :
|
388
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
328
389
|
<p>
|
329
390
|
<label>My Avatar</label>
|
330
391
|
<%= f.file_field :avatar %>
|
@@ -337,7 +398,7 @@ It might be a good idea to show the user that a file has been uploaded, in the
|
|
337
398
|
case of images, a small thumbnail would be a good indicator:
|
338
399
|
|
339
400
|
```erb
|
340
|
-
<%= form_for @user, :
|
401
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
341
402
|
<p>
|
342
403
|
<label>My Avatar</label>
|
343
404
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -353,7 +414,7 @@ If you want to remove a previously uploaded file on a mounted uploader, you can
|
|
353
414
|
easily add a checkbox to the form which will remove the file when checked.
|
354
415
|
|
355
416
|
```erb
|
356
|
-
<%= form_for @user, :
|
417
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
357
418
|
<p>
|
358
419
|
<label>My Avatar</label>
|
359
420
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -384,7 +445,7 @@ via a URL. CarrierWave makes this simple, just add the appropriate attribute to
|
|
384
445
|
form and you're good to go:
|
385
446
|
|
386
447
|
```erb
|
387
|
-
<%= form_for @user, :
|
448
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
388
449
|
<p>
|
389
450
|
<label>My Avatar URL:</label>
|
390
451
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -406,7 +467,7 @@ this easily by overriding the `default_url` method in your uploader:
|
|
406
467
|
|
407
468
|
```ruby
|
408
469
|
class MyUploader < CarrierWave::Uploader::Base
|
409
|
-
def default_url
|
470
|
+
def default_url(*args)
|
410
471
|
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
|
411
472
|
end
|
412
473
|
end
|
@@ -416,7 +477,7 @@ Or if you are using the Rails asset pipeline:
|
|
416
477
|
|
417
478
|
```ruby
|
418
479
|
class MyUploader < CarrierWave::Uploader::Base
|
419
|
-
def default_url
|
480
|
+
def default_url(*args)
|
420
481
|
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
421
482
|
end
|
422
483
|
end
|
@@ -443,7 +504,7 @@ instance.recreate_versions!(:thumb, :large)
|
|
443
504
|
Or on a mounted uploader:
|
444
505
|
|
445
506
|
```ruby
|
446
|
-
User.
|
507
|
+
User.find_each do |user|
|
447
508
|
user.avatar.recreate_versions!
|
448
509
|
end
|
449
510
|
```
|
@@ -451,7 +512,7 @@ end
|
|
451
512
|
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
513
|
|
453
514
|
```ruby
|
454
|
-
User.
|
515
|
+
User.find_each do |user|
|
455
516
|
user.avatar.recreate_versions! if user.avatar?
|
456
517
|
end
|
457
518
|
```
|
@@ -481,6 +542,16 @@ If you're using Rails, create an initializer for this:
|
|
481
542
|
|
482
543
|
config/initializers/carrierwave.rb
|
483
544
|
|
545
|
+
If you want CarrierWave to fail noisily in development, you can change these configs in your environment file:
|
546
|
+
|
547
|
+
```ruby
|
548
|
+
CarrierWave.configure do |config|
|
549
|
+
config.ignore_integrity_errors = false
|
550
|
+
config.ignore_processing_errors = false
|
551
|
+
config.ignore_download_errors = false
|
552
|
+
end
|
553
|
+
```
|
554
|
+
|
484
555
|
|
485
556
|
## Testing with CarrierWave
|
486
557
|
|
@@ -511,31 +582,37 @@ require 'carrierwave/test/matchers'
|
|
511
582
|
describe MyUploader do
|
512
583
|
include CarrierWave::Test::Matchers
|
513
584
|
|
585
|
+
let(:user) { double('user') }
|
586
|
+
let(:uploader) { MyUploader.new(user, :avatar) }
|
587
|
+
|
514
588
|
before do
|
515
589
|
MyUploader.enable_processing = true
|
516
|
-
|
517
|
-
@uploader.store!(File.open(path_to_file))
|
590
|
+
File.open(path_to_file) { |f| uploader.store!(f) }
|
518
591
|
end
|
519
592
|
|
520
593
|
after do
|
521
594
|
MyUploader.enable_processing = false
|
522
|
-
|
595
|
+
uploader.remove!
|
523
596
|
end
|
524
597
|
|
525
598
|
context 'the thumb version' do
|
526
|
-
it "
|
527
|
-
|
599
|
+
it "scales down a landscape image to be exactly 64 by 64 pixels" do
|
600
|
+
expect(uploader.thumb).to have_dimensions(64, 64)
|
528
601
|
end
|
529
602
|
end
|
530
603
|
|
531
604
|
context 'the small version' do
|
532
|
-
it "
|
533
|
-
|
605
|
+
it "scales down a landscape image to fit within 200 by 200 pixels" do
|
606
|
+
expect(uploader.small).to be_no_larger_than(200, 200)
|
534
607
|
end
|
535
608
|
end
|
536
609
|
|
537
|
-
it "
|
538
|
-
|
610
|
+
it "makes the image readable only to the owner and not executable" do
|
611
|
+
expect(uploader).to have_permissions(0600)
|
612
|
+
end
|
613
|
+
|
614
|
+
it "has the correct format" do
|
615
|
+
expect(uploader).to be_format('png')
|
539
616
|
end
|
540
617
|
end
|
541
618
|
```
|
@@ -547,31 +624,42 @@ Processing can be enabled for a single version by setting the processing flag on
|
|
547
624
|
@uploader.thumb.enable_processing = true
|
548
625
|
```
|
549
626
|
|
627
|
+
## Fog
|
628
|
+
|
629
|
+
If you want to use fog you must add in your CarrierWave initializer the
|
630
|
+
following lines
|
631
|
+
|
632
|
+
```ruby
|
633
|
+
config.fog_provider = 'fog' # 'fog/aws' etc. Defaults to 'fog'
|
634
|
+
config.fog_credentials = { ... } # Provider specific credentials
|
635
|
+
```
|
636
|
+
|
550
637
|
## Using Amazon S3
|
551
638
|
|
552
|
-
[Fog](http://github.com/fog/fog) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
639
|
+
[Fog AWS](http://github.com/fog/fog-aws) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
553
640
|
|
554
641
|
```ruby
|
555
|
-
gem "fog"
|
642
|
+
gem "fog-aws"
|
556
643
|
```
|
557
644
|
|
558
645
|
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
|
646
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if it needs to be.
|
560
647
|
You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:
|
561
648
|
|
562
649
|
```ruby
|
563
650
|
CarrierWave.configure do |config|
|
651
|
+
config.fog_provider = 'fog/aws' # required
|
564
652
|
config.fog_credentials = {
|
565
|
-
:
|
566
|
-
:
|
567
|
-
:
|
568
|
-
:
|
569
|
-
:
|
570
|
-
:
|
653
|
+
provider: 'AWS', # required
|
654
|
+
aws_access_key_id: 'xxx', # required
|
655
|
+
aws_secret_access_key: 'yyy', # required
|
656
|
+
region: 'eu-west-1', # optional, defaults to 'us-east-1'
|
657
|
+
host: 's3.example.com', # optional, defaults to nil
|
658
|
+
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
|
571
659
|
}
|
572
|
-
config.fog_directory = 'name_of_directory'
|
573
|
-
config.fog_public = false
|
574
|
-
config.fog_attributes = {'Cache-Control'=>
|
660
|
+
config.fog_directory = 'name_of_directory' # required
|
661
|
+
config.fog_public = false # optional, defaults to true
|
662
|
+
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
|
575
663
|
end
|
576
664
|
```
|
577
665
|
|
@@ -600,11 +688,12 @@ Using a US-based account:
|
|
600
688
|
|
601
689
|
```ruby
|
602
690
|
CarrierWave.configure do |config|
|
691
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
603
692
|
config.fog_credentials = {
|
604
|
-
:
|
605
|
-
:
|
606
|
-
:
|
607
|
-
:
|
693
|
+
provider: 'Rackspace',
|
694
|
+
rackspace_username: 'xxxxxx',
|
695
|
+
rackspace_api_key: 'yyyyyy',
|
696
|
+
rackspace_region: :ord # optional, defaults to :dfw
|
608
697
|
}
|
609
698
|
config.fog_directory = 'name_of_directory'
|
610
699
|
end
|
@@ -614,12 +703,13 @@ Using a UK-based account:
|
|
614
703
|
|
615
704
|
```ruby
|
616
705
|
CarrierWave.configure do |config|
|
706
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
617
707
|
config.fog_credentials = {
|
618
|
-
:
|
619
|
-
:
|
620
|
-
:
|
621
|
-
:
|
622
|
-
:
|
708
|
+
provider: 'Rackspace',
|
709
|
+
rackspace_username: 'xxxxxx',
|
710
|
+
rackspace_api_key: 'yyyyyy',
|
711
|
+
rackspace_auth_url: Fog::Rackspace::UK_AUTH_ENDPOINT,
|
712
|
+
rackspace_region: :lon
|
623
713
|
}
|
624
714
|
config.fog_directory = 'name_of_directory'
|
625
715
|
end
|
@@ -646,25 +736,27 @@ the url to the file on Rackspace Cloud Files.
|
|
646
736
|
|
647
737
|
## Using Google Storage for Developers
|
648
738
|
|
649
|
-
[Fog](http://github.com/fog/fog) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
739
|
+
[Fog](http://github.com/fog/fog-google) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
650
740
|
|
651
741
|
```ruby
|
652
|
-
gem "fog"
|
742
|
+
gem "fog-google"
|
743
|
+
gem "google-api-client", ">= 0.6.2", "< 0.9"
|
744
|
+
gem "mime-types"
|
653
745
|
```
|
654
746
|
|
655
747
|
You'll need to configure a directory (also known as a bucket), access key id and secret access key in the initializer.
|
656
748
|
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
657
749
|
|
658
|
-
|
659
|
-
under the section “Interoperable Access”.
|
750
|
+
Please read the [fog-google README](https://github.com/fog/fog-google/blob/master/README.md) on how to get credentials.
|
660
751
|
|
661
752
|
|
662
753
|
```ruby
|
663
754
|
CarrierWave.configure do |config|
|
755
|
+
config.fog_provider = 'fog/google' # required
|
664
756
|
config.fog_credentials = {
|
665
|
-
:
|
666
|
-
:
|
667
|
-
:
|
757
|
+
provider: 'Google',
|
758
|
+
google_storage_access_key_id: 'xxxxxx',
|
759
|
+
google_storage_secret_access_key: 'yyyyyy'
|
668
760
|
}
|
669
761
|
config.fog_directory = 'name_of_directory'
|
670
762
|
end
|
@@ -681,6 +773,31 @@ end
|
|
681
773
|
That's it! You can still use the `CarrierWave::Uploader#url` method to return
|
682
774
|
the url to the file on Google.
|
683
775
|
|
776
|
+
## Optimized Loading of Fog
|
777
|
+
|
778
|
+
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:
|
779
|
+
|
780
|
+
```ruby
|
781
|
+
gem "fog", "~> 1.27", require: "fog/rackspace/storage"
|
782
|
+
gem "carrierwave"
|
783
|
+
```
|
784
|
+
|
785
|
+
A couple of notes about versions:
|
786
|
+
* This functionality was introduced in Fog v1.20.
|
787
|
+
* This functionality is slated for CarrierWave v1.0.0.
|
788
|
+
|
789
|
+
If you're not relying on Gemfile entries alone and are requiring "carrierwave" anywhere, ensure you require "fog/rackspace/storage" before it. Ex:
|
790
|
+
|
791
|
+
```ruby
|
792
|
+
require "fog/rackspace/storage"
|
793
|
+
require "carrierwave"
|
794
|
+
```
|
795
|
+
|
796
|
+
Beware that this specific require is only needed when working with a fog provider that was not extracted to its own gem yet.
|
797
|
+
A list of the extracted providers can be found in the page of the `fog` organizations [here](https://github.com/fog).
|
798
|
+
|
799
|
+
When in doubt, inspect `Fog.constants` to see what has been loaded.
|
800
|
+
|
684
801
|
## Dynamic Asset Host
|
685
802
|
|
686
803
|
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 +835,8 @@ Convert will only work if the file has the same file extension, thus the use of
|
|
718
835
|
class AvatarUploader < CarrierWave::Uploader::Base
|
719
836
|
include CarrierWave::RMagick
|
720
837
|
|
721
|
-
process :
|
722
|
-
process :
|
838
|
+
process resize_to_fill: [200, 200]
|
839
|
+
process convert: 'png'
|
723
840
|
|
724
841
|
def filename
|
725
842
|
super.chomp(File.extname(super)) + '.png' if original_filename.present?
|
@@ -751,7 +868,7 @@ for the RMagick processor.
|
|
751
868
|
class AvatarUploader < CarrierWave::Uploader::Base
|
752
869
|
include CarrierWave::MiniMagick
|
753
870
|
|
754
|
-
process :
|
871
|
+
process resize_to_fill: [200, 200]
|
755
872
|
end
|
756
873
|
```
|
757
874
|
|
@@ -771,14 +888,10 @@ details.
|
|
771
888
|
Be sure to use mount_on to specify the correct column:
|
772
889
|
|
773
890
|
```ruby
|
774
|
-
mount_uploader :avatar, AvatarUploader, :
|
891
|
+
mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
|
775
892
|
```
|
776
893
|
|
777
|
-
|
778
|
-
sensible compatibility mode. Patches for migrating from other solutions will be
|
779
|
-
happily accepted.
|
780
|
-
|
781
|
-
## i18n
|
894
|
+
## I18n
|
782
895
|
|
783
896
|
The Active Record validations use the Rails i18n framework. Add these keys to
|
784
897
|
your translations file:
|
@@ -789,8 +902,8 @@ errors:
|
|
789
902
|
carrierwave_processing_error: "Cannot resize image."
|
790
903
|
carrierwave_integrity_error: "Not an image."
|
791
904
|
carrierwave_download_error: "Couldn't download image."
|
792
|
-
|
793
|
-
|
905
|
+
extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
|
906
|
+
extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
|
794
907
|
```
|
795
908
|
|
796
909
|
## Large files
|
@@ -806,6 +919,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
806
919
|
def move_to_cache
|
807
920
|
true
|
808
921
|
end
|
922
|
+
|
809
923
|
def move_to_store
|
810
924
|
true
|
811
925
|
end
|
@@ -832,9 +946,10 @@ Will add these callbacks:
|
|
832
946
|
```ruby
|
833
947
|
after_save :store_avatar!
|
834
948
|
before_save :write_avatar_identifier
|
835
|
-
after_commit :remove_avatar
|
836
|
-
|
837
|
-
after_save :
|
949
|
+
after_commit :remove_avatar!, on: :destroy
|
950
|
+
after_commit :mark_remove_avatar_false, on: :update
|
951
|
+
after_save :store_previous_changes_for_avatar
|
952
|
+
after_commit :remove_previously_stored_avatar, on: :update
|
838
953
|
```
|
839
954
|
|
840
955
|
If you want to skip any of these callbacks (eg. you want to keep the existing
|
@@ -844,7 +959,7 @@ avatar, even after uploading a new one), you can use ActiveRecord’s
|
|
844
959
|
```ruby
|
845
960
|
class User
|
846
961
|
mount_uploader :avatar, AvatarUploader
|
847
|
-
skip_callback :
|
962
|
+
skip_callback :commit, :after, :remove_previously_stored_avatar
|
848
963
|
end
|
849
964
|
```
|
850
965
|
|
@@ -854,7 +969,7 @@ See [CONTRIBUTING.md](https://github.com/carrierwaveuploader/carrierwave/blob/ma
|
|
854
969
|
|
855
970
|
## License
|
856
971
|
|
857
|
-
Copyright (c) 2008-
|
972
|
+
Copyright (c) 2008-2015 Jonas Nicklas
|
858
973
|
|
859
974
|
Permission is hereby granted, free of charge, to any person obtaining
|
860
975
|
a copy of this software and associated documentation files (the
|