carrierwave 0.11.2 → 1.0.0.beta
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 +220 -116
- data/lib/carrierwave.rb +8 -10
- 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 +209 -176
- data/lib/carrierwave/mounter.rb +163 -0
- data/lib/carrierwave/orm/activerecord.rb +49 -20
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/processing/mini_magick.rb +64 -13
- data/lib/carrierwave/processing/rmagick.rb +31 -3
- data/lib/carrierwave/sanitized_file.rb +43 -38
- data/lib/carrierwave/storage.rb +0 -9
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +64 -2
- data/lib/carrierwave/storage/fog.rb +100 -27
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader.rb +2 -2
- 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/default_url.rb +3 -5
- data/lib/carrierwave/uploader/download.rb +2 -4
- 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/utilities.rb +0 -3
- data/lib/carrierwave/utilities/uri.rb +4 -7
- data/lib/carrierwave/validations/active_model.rb +0 -2
- data/lib/carrierwave/version.rb +1 -1
- data/lib/generators/templates/uploader.rb +3 -5
- metadata +43 -97
- 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: a4ebc3c2c8a7083953582084c27f7abd61fbbc31
|
4
|
+
data.tar.gz: c1877d8a5780cf897943b43ed124fa40ff669a7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37f4ed1affddf04996b406fcb201731ec1d52fcb46eb7aa212517604844b3163f88ecdd0733c750d0ea6b5e2c5bf7ccaa6f0e70c4cd288ad2974eb39d0057c69
|
7
|
+
data.tar.gz: d2f91768a496e1b7740838f76d1f7c54f84bc10712ff7894e02b1f75eecbc4a8da246887fb3760decc3ea7131aa7b1a281fec2b39921a862f5cd09feebf02461
|
data/README.md
CHANGED
@@ -3,8 +3,15 @@
|
|
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
|
+
|
10
|
+
|
11
|
+
> ## carrierwave version disclaimer
|
12
|
+
> ***This README is for a branch which is still in development.
|
13
|
+
> Please switch to latest `0.x` branch for stable version.***
|
14
|
+
|
8
15
|
|
9
16
|
## Information
|
10
17
|
|
@@ -14,25 +21,27 @@ It works well with Rack based web applications, such as Ruby on Rails.
|
|
14
21
|
|
15
22
|
## Getting Help
|
16
23
|
|
17
|
-
* Please ask the [
|
24
|
+
* 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
25
|
* Please report bugs on the [issue tracker](http://github.com/carrierwaveuploader/carrierwave/issues) but read the "getting help" section in the wiki first.
|
19
26
|
|
20
27
|
## Installation
|
21
28
|
|
22
|
-
Install the latest
|
29
|
+
Install the latest release:
|
23
30
|
|
24
|
-
|
31
|
+
```
|
32
|
+
$ gem install carrierwave -v "1.0.0.beta"
|
33
|
+
```
|
25
34
|
|
26
35
|
In Rails, add it to your Gemfile:
|
27
36
|
|
28
37
|
```ruby
|
29
|
-
gem 'carrierwave'
|
38
|
+
gem 'carrierwave', '>= 1.0.0.beta', '< 2.0'
|
30
39
|
```
|
31
40
|
|
32
41
|
Finally, restart the server to apply the changes.
|
33
42
|
|
34
|
-
|
35
|
-
Rails
|
43
|
+
As of version 1.0.0 (forthcoming), CarrierWave requires Rails 4.0 or higher and Ruby 2.0
|
44
|
+
or higher. If you're on Rails 3, you should use v0.11.0.
|
36
45
|
|
37
46
|
## Getting Started
|
38
47
|
|
@@ -100,24 +109,81 @@ automatically be stored when the record is saved.
|
|
100
109
|
|
101
110
|
```ruby
|
102
111
|
u = User.new
|
103
|
-
u.avatar = params[:file]
|
104
|
-
|
112
|
+
u.avatar = params[:file] # Assign a file like this, or
|
113
|
+
|
114
|
+
# like this
|
115
|
+
File.open('somewhere') do |f|
|
116
|
+
u.avatar = f
|
117
|
+
end
|
118
|
+
|
105
119
|
u.save!
|
106
120
|
u.avatar.url # => '/url/to/file.png'
|
107
121
|
u.avatar.current_path # => 'path/to/file.png'
|
108
|
-
u.
|
122
|
+
u.avatar_identifier # => 'file.png'
|
109
123
|
```
|
110
124
|
|
125
|
+
**Note**: `u.avatar` will never return nil, even if there is no photo associated to it.
|
126
|
+
To check if a photo was saved to the model, use `u.avatar.file.nil?` instead.
|
127
|
+
|
111
128
|
### DataMapper, Mongoid, Sequel
|
112
129
|
|
113
130
|
Other ORM support has been extracted into separate gems:
|
114
131
|
|
115
132
|
* [carrierwave-datamapper](https://github.com/carrierwaveuploader/carrierwave-datamapper)
|
116
133
|
* [carrierwave-mongoid](https://github.com/carrierwaveuploader/carrierwave-mongoid)
|
117
|
-
* [carrierwave-sequel](https://github.com/
|
134
|
+
* [carrierwave-sequel](https://github.com/carrierwaveuploader/carrierwave-sequel)
|
118
135
|
|
119
136
|
There are more extensions listed in [the wiki](https://github.com/carrierwaveuploader/carrierwave/wiki)
|
120
137
|
|
138
|
+
## Multiple file uploads
|
139
|
+
**Note:** You must specify using the master branch to enable this feature:
|
140
|
+
|
141
|
+
`gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'`.
|
142
|
+
|
143
|
+
CarrierWave also has convenient support for multiple file upload fields.
|
144
|
+
|
145
|
+
### ActiveRecord
|
146
|
+
|
147
|
+
Add a column which can store an array. This could be an array column or a JSON
|
148
|
+
column for example. Your choice depends on what your database supports. For
|
149
|
+
example, create a migration like this:
|
150
|
+
|
151
|
+
|
152
|
+
rails g migration add_avatars_to_users avatars:json
|
153
|
+
rake db:migrate
|
154
|
+
|
155
|
+
Open your model file and mount the uploader:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
class User < ActiveRecord::Base
|
159
|
+
mount_uploaders :avatars, AvatarUploader
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
Make sure your file input fields are set up as multiple file fields. For
|
164
|
+
example in Rails you'll want to do something like this:
|
165
|
+
|
166
|
+
```erb
|
167
|
+
<%= form.file_field :avatars, multiple: true %>
|
168
|
+
```
|
169
|
+
|
170
|
+
Also, make sure your upload controller permits the multiple file upload attribute, *pointing to an empty array in a hash*. For example:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
|
174
|
+
```
|
175
|
+
|
176
|
+
Now you can select multiple files in the upload dialog (e.g. SHIFT+SELECT), and they will
|
177
|
+
automatically be stored when the record is saved.
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
u = User.new(params[:user])
|
181
|
+
u.save!
|
182
|
+
u.avatars[0].url # => '/url/to/file.png'
|
183
|
+
u.avatars[0].current_path # => 'path/to/file.png'
|
184
|
+
u.avatars[0].identifier # => 'file.png'
|
185
|
+
```
|
186
|
+
|
121
187
|
## Changing the storage directory
|
122
188
|
|
123
189
|
In order to change where uploaded files are put, just override the `store_dir`
|
@@ -146,85 +212,69 @@ end
|
|
146
212
|
|
147
213
|
## Securing uploads
|
148
214
|
|
149
|
-
Certain
|
150
|
-
files or other script files. CarrierWave allows you to specify a
|
151
|
-
allowed extensions.
|
215
|
+
Certain files might be dangerous if uploaded to the wrong location, such as PHP
|
216
|
+
files or other script files. CarrierWave allows you to specify a whitelist of
|
217
|
+
allowed extensions or content types.
|
152
218
|
|
153
219
|
If you're mounting the uploader, uploading a file with the wrong extension will
|
154
220
|
make the record invalid instead. Otherwise, an error is raised.
|
155
221
|
|
156
222
|
```ruby
|
157
223
|
class MyUploader < CarrierWave::Uploader::Base
|
158
|
-
def
|
224
|
+
def extension_whitelist
|
159
225
|
%w(jpg jpeg gif png)
|
160
226
|
end
|
161
227
|
end
|
162
228
|
```
|
163
229
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
A valid whitelist that will restrict your uploader to images only, and mitigate the CVE is:
|
230
|
+
The same thing could be done using content types.
|
231
|
+
Let's say we need an uploader that accepts only images. This can be done like this
|
168
232
|
|
169
233
|
```ruby
|
170
234
|
class MyUploader < CarrierWave::Uploader::Base
|
171
235
|
def content_type_whitelist
|
172
|
-
|
236
|
+
/image\//
|
173
237
|
end
|
174
238
|
end
|
175
239
|
```
|
176
240
|
|
177
|
-
|
241
|
+
You can use a blacklist to reject content types.
|
242
|
+
Let's say we need an uploader that reject JSON files. This can be done like this
|
178
243
|
|
244
|
+
```ruby
|
245
|
+
class NoJsonUploader < CarrierWave::Uploader::Base
|
246
|
+
def content_type_blacklist
|
247
|
+
['application/text', 'application/json']
|
248
|
+
end
|
249
|
+
end
|
250
|
+
```
|
179
251
|
|
180
252
|
### Filenames and unicode chars
|
181
253
|
|
182
254
|
Another security issue you should care for is the file names (see
|
183
255
|
[Ruby On Rails Security Guide](http://guides.rubyonrails.org/security.html#file-uploads)).
|
184
|
-
By default, CarrierWave provides only English letters, arabic numerals and
|
256
|
+
By default, CarrierWave provides only English letters, arabic numerals and some symbols as
|
185
257
|
white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you
|
186
258
|
have to override `sanitize_regexp` method. It should return regular expression which would match
|
187
259
|
all *non*-allowed symbols.
|
188
260
|
|
189
|
-
With Ruby 1.9 and higher you can simply write (as it has [Oniguruma](http://oniguruma.rubyforge.org/oniguruma/)
|
190
|
-
built-in):
|
191
|
-
|
192
261
|
```ruby
|
193
262
|
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
|
194
263
|
```
|
195
264
|
|
196
|
-
With Ruby 1.8 you have to manually specify all character ranges. For example, for files which may
|
197
|
-
contain Russian letters:
|
198
|
-
|
199
|
-
```ruby
|
200
|
-
CarrierWave::SanitizedFile.sanitize_regexp = /[^a-zA-Zа-яА-ЯёЁ0-9\.\-\+_]/u
|
201
|
-
```
|
202
|
-
|
203
265
|
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
|
204
266
|
plugins or client-side software.
|
205
267
|
|
206
268
|
## Setting the content type
|
207
269
|
|
208
|
-
|
209
|
-
|
210
|
-
This adds a dependency on the [mime-types](http://rubygems.org/gems/mime-types) gem,
|
211
|
-
but is recommended when using fog, and fog already has a dependency on mime-types.
|
212
|
-
|
213
|
-
```ruby
|
214
|
-
require 'carrierwave/processing/mime_types'
|
215
|
-
|
216
|
-
class MyUploader < CarrierWave::Uploader::Base
|
217
|
-
include CarrierWave::MimeTypes
|
218
|
-
|
219
|
-
process :set_content_type
|
220
|
-
end
|
221
|
-
```
|
270
|
+
As of v0.11.0, the `mime-types` gem is a runtime dependency and the content type is set automatically.
|
271
|
+
You no longer need to do this manually.
|
222
272
|
|
223
273
|
## Adding versions
|
224
274
|
|
225
275
|
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*:
|
226
276
|
|
227
|
-
*Note
|
277
|
+
*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.
|
228
278
|
|
229
279
|
Some documentation refers to RMagick instead of MiniMagick but MiniMagick is recommended.
|
230
280
|
|
@@ -238,10 +288,10 @@ $ brew install imagemagick
|
|
238
288
|
class MyUploader < CarrierWave::Uploader::Base
|
239
289
|
include CarrierWave::MiniMagick
|
240
290
|
|
241
|
-
process :
|
291
|
+
process resize_to_fit: [800, 800]
|
242
292
|
|
243
293
|
version :thumb do
|
244
|
-
process :
|
294
|
+
process resize_to_fill: [200,200]
|
245
295
|
end
|
246
296
|
|
247
297
|
end
|
@@ -283,11 +333,11 @@ properties within the model or based on the picture itself.
|
|
283
333
|
```ruby
|
284
334
|
class MyUploader < CarrierWave::Uploader::Base
|
285
335
|
|
286
|
-
version :human, :
|
287
|
-
version :monkey, :
|
288
|
-
version :banner, :
|
336
|
+
version :human, if: :is_human?
|
337
|
+
version :monkey, if: :is_monkey?
|
338
|
+
version :banner, if: :is_landscape?
|
289
339
|
|
290
|
-
|
340
|
+
private
|
291
341
|
|
292
342
|
def is_human? picture
|
293
343
|
model.can_program?(:ruby)
|
@@ -321,7 +371,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
321
371
|
process resize_to_fill: [280, 280]
|
322
372
|
end
|
323
373
|
|
324
|
-
version :small_thumb, :
|
374
|
+
version :small_thumb, from_version: :thumb do
|
325
375
|
process resize_to_fill: [20, 20]
|
326
376
|
end
|
327
377
|
|
@@ -340,7 +390,7 @@ file, just add a hidden field called `avatar_cache` (don't forget to add it to
|
|
340
390
|
the attr_accessible list as necessary). In Rails, this would look like this:
|
341
391
|
|
342
392
|
```erb
|
343
|
-
<%= form_for @user, :
|
393
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
344
394
|
<p>
|
345
395
|
<label>My Avatar</label>
|
346
396
|
<%= f.file_field :avatar %>
|
@@ -353,7 +403,7 @@ It might be a good idea to show the user that a file has been uploaded, in the
|
|
353
403
|
case of images, a small thumbnail would be a good indicator:
|
354
404
|
|
355
405
|
```erb
|
356
|
-
<%= form_for @user, :
|
406
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
357
407
|
<p>
|
358
408
|
<label>My Avatar</label>
|
359
409
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -369,7 +419,7 @@ If you want to remove a previously uploaded file on a mounted uploader, you can
|
|
369
419
|
easily add a checkbox to the form which will remove the file when checked.
|
370
420
|
|
371
421
|
```erb
|
372
|
-
<%= form_for @user, :
|
422
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
373
423
|
<p>
|
374
424
|
<label>My Avatar</label>
|
375
425
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -400,7 +450,7 @@ via a URL. CarrierWave makes this simple, just add the appropriate attribute to
|
|
400
450
|
form and you're good to go:
|
401
451
|
|
402
452
|
```erb
|
403
|
-
<%= form_for @user, :
|
453
|
+
<%= form_for @user, html: { multipart: true } do |f| %>
|
404
454
|
<p>
|
405
455
|
<label>My Avatar URL:</label>
|
406
456
|
<%= image_tag(@user.avatar_url) if @user.avatar? %>
|
@@ -422,7 +472,7 @@ this easily by overriding the `default_url` method in your uploader:
|
|
422
472
|
|
423
473
|
```ruby
|
424
474
|
class MyUploader < CarrierWave::Uploader::Base
|
425
|
-
def default_url
|
475
|
+
def default_url(*args)
|
426
476
|
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
|
427
477
|
end
|
428
478
|
end
|
@@ -432,7 +482,7 @@ Or if you are using the Rails asset pipeline:
|
|
432
482
|
|
433
483
|
```ruby
|
434
484
|
class MyUploader < CarrierWave::Uploader::Base
|
435
|
-
def default_url
|
485
|
+
def default_url(*args)
|
436
486
|
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
|
437
487
|
end
|
438
488
|
end
|
@@ -459,7 +509,7 @@ instance.recreate_versions!(:thumb, :large)
|
|
459
509
|
Or on a mounted uploader:
|
460
510
|
|
461
511
|
```ruby
|
462
|
-
User.
|
512
|
+
User.find_each do |user|
|
463
513
|
user.avatar.recreate_versions!
|
464
514
|
end
|
465
515
|
```
|
@@ -467,7 +517,7 @@ end
|
|
467
517
|
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:
|
468
518
|
|
469
519
|
```ruby
|
470
|
-
User.
|
520
|
+
User.find_each do |user|
|
471
521
|
user.avatar.recreate_versions! if user.avatar?
|
472
522
|
end
|
473
523
|
```
|
@@ -497,6 +547,16 @@ If you're using Rails, create an initializer for this:
|
|
497
547
|
|
498
548
|
config/initializers/carrierwave.rb
|
499
549
|
|
550
|
+
If you want CarrierWave to fail noisily in development, you can change these configs in your environment file:
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
CarrierWave.configure do |config|
|
554
|
+
config.ignore_integrity_errors = false
|
555
|
+
config.ignore_processing_errors = false
|
556
|
+
config.ignore_download_errors = false
|
557
|
+
end
|
558
|
+
```
|
559
|
+
|
500
560
|
|
501
561
|
## Testing with CarrierWave
|
502
562
|
|
@@ -527,31 +587,37 @@ require 'carrierwave/test/matchers'
|
|
527
587
|
describe MyUploader do
|
528
588
|
include CarrierWave::Test::Matchers
|
529
589
|
|
590
|
+
let(:user) { double('user') }
|
591
|
+
let(:uploader) { MyUploader.new(user, :avatar) }
|
592
|
+
|
530
593
|
before do
|
531
594
|
MyUploader.enable_processing = true
|
532
|
-
|
533
|
-
@uploader.store!(File.open(path_to_file))
|
595
|
+
File.open(path_to_file) { |f| uploader.store!(f) }
|
534
596
|
end
|
535
597
|
|
536
598
|
after do
|
537
599
|
MyUploader.enable_processing = false
|
538
|
-
|
600
|
+
uploader.remove!
|
539
601
|
end
|
540
602
|
|
541
603
|
context 'the thumb version' do
|
542
|
-
it "
|
543
|
-
|
604
|
+
it "scales down a landscape image to be exactly 64 by 64 pixels" do
|
605
|
+
expect(uploader.thumb).to have_dimensions(64, 64)
|
544
606
|
end
|
545
607
|
end
|
546
608
|
|
547
609
|
context 'the small version' do
|
548
|
-
it "
|
549
|
-
|
610
|
+
it "scales down a landscape image to fit within 200 by 200 pixels" do
|
611
|
+
expect(uploader.small).to be_no_larger_than(200, 200)
|
550
612
|
end
|
551
613
|
end
|
552
614
|
|
553
|
-
it "
|
554
|
-
|
615
|
+
it "makes the image readable only to the owner and not executable" do
|
616
|
+
expect(uploader).to have_permissions(0600)
|
617
|
+
end
|
618
|
+
|
619
|
+
it "has the correct format" do
|
620
|
+
expect(uploader).to be_format('png')
|
555
621
|
end
|
556
622
|
end
|
557
623
|
```
|
@@ -563,31 +629,42 @@ Processing can be enabled for a single version by setting the processing flag on
|
|
563
629
|
@uploader.thumb.enable_processing = true
|
564
630
|
```
|
565
631
|
|
632
|
+
## Fog
|
633
|
+
|
634
|
+
If you want to use fog you must add in your CarrierWave initializer the
|
635
|
+
following lines
|
636
|
+
|
637
|
+
```ruby
|
638
|
+
config.fog_provider = 'fog' # 'fog/aws' etc. Defaults to 'fog'
|
639
|
+
config.fog_credentials = { ... } # Provider specific credentials
|
640
|
+
```
|
641
|
+
|
566
642
|
## Using Amazon S3
|
567
643
|
|
568
|
-
[Fog](http://github.com/fog/fog) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
644
|
+
[Fog AWS](http://github.com/fog/fog-aws) is used to support Amazon S3. Ensure you have it in your Gemfile:
|
569
645
|
|
570
646
|
```ruby
|
571
|
-
gem "fog"
|
647
|
+
gem "fog-aws"
|
572
648
|
```
|
573
649
|
|
574
650
|
You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.
|
575
|
-
For the sake of performance it is assumed that the directory already exists, so please create it if
|
651
|
+
For the sake of performance it is assumed that the directory already exists, so please create it if it needs to be.
|
576
652
|
You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:
|
577
653
|
|
578
654
|
```ruby
|
579
655
|
CarrierWave.configure do |config|
|
656
|
+
config.fog_provider = 'fog/aws' # required
|
580
657
|
config.fog_credentials = {
|
581
|
-
:
|
582
|
-
:
|
583
|
-
:
|
584
|
-
:
|
585
|
-
:
|
586
|
-
:
|
658
|
+
provider: 'AWS', # required
|
659
|
+
aws_access_key_id: 'xxx', # required
|
660
|
+
aws_secret_access_key: 'yyy', # required
|
661
|
+
region: 'eu-west-1', # optional, defaults to 'us-east-1'
|
662
|
+
host: 's3.example.com', # optional, defaults to nil
|
663
|
+
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
|
587
664
|
}
|
588
|
-
config.fog_directory = 'name_of_directory'
|
589
|
-
config.fog_public = false
|
590
|
-
config.fog_attributes = {'Cache-Control'=>
|
665
|
+
config.fog_directory = 'name_of_directory' # required
|
666
|
+
config.fog_public = false # optional, defaults to true
|
667
|
+
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
|
591
668
|
end
|
592
669
|
```
|
593
670
|
|
@@ -616,11 +693,12 @@ Using a US-based account:
|
|
616
693
|
|
617
694
|
```ruby
|
618
695
|
CarrierWave.configure do |config|
|
696
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
619
697
|
config.fog_credentials = {
|
620
|
-
:
|
621
|
-
:
|
622
|
-
:
|
623
|
-
:
|
698
|
+
provider: 'Rackspace',
|
699
|
+
rackspace_username: 'xxxxxx',
|
700
|
+
rackspace_api_key: 'yyyyyy',
|
701
|
+
rackspace_region: :ord # optional, defaults to :dfw
|
624
702
|
}
|
625
703
|
config.fog_directory = 'name_of_directory'
|
626
704
|
end
|
@@ -630,12 +708,13 @@ Using a UK-based account:
|
|
630
708
|
|
631
709
|
```ruby
|
632
710
|
CarrierWave.configure do |config|
|
711
|
+
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
633
712
|
config.fog_credentials = {
|
634
|
-
:
|
635
|
-
:
|
636
|
-
:
|
637
|
-
:
|
638
|
-
:
|
713
|
+
provider: 'Rackspace',
|
714
|
+
rackspace_username: 'xxxxxx',
|
715
|
+
rackspace_api_key: 'yyyyyy',
|
716
|
+
rackspace_auth_url: Fog::Rackspace::UK_AUTH_ENDPOINT,
|
717
|
+
rackspace_region: :lon
|
639
718
|
}
|
640
719
|
config.fog_directory = 'name_of_directory'
|
641
720
|
end
|
@@ -662,25 +741,27 @@ the url to the file on Rackspace Cloud Files.
|
|
662
741
|
|
663
742
|
## Using Google Storage for Developers
|
664
743
|
|
665
|
-
[Fog](http://github.com/fog/fog) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
744
|
+
[Fog](http://github.com/fog/fog-google) is used to support Google Storage for Developers. Ensure you have it in your Gemfile:
|
666
745
|
|
667
746
|
```ruby
|
668
|
-
gem "fog"
|
747
|
+
gem "fog-google"
|
748
|
+
gem "google-api-client", "< 0.9", ">= 0.6.2"
|
749
|
+
gem "mime-types"
|
669
750
|
```
|
670
751
|
|
671
752
|
You'll need to configure a directory (also known as a bucket), access key id and secret access key in the initializer.
|
672
753
|
For the sake of performance it is assumed that the directory already exists, so please create it if need be.
|
673
754
|
|
674
|
-
|
675
|
-
under the section “Interoperable Access”.
|
755
|
+
Please read the [fog-google README](https://github.com/fog/fog-google/blob/master/README.md) on how to get credentials.
|
676
756
|
|
677
757
|
|
678
758
|
```ruby
|
679
759
|
CarrierWave.configure do |config|
|
760
|
+
config.fog_provider = 'fog/google' # required
|
680
761
|
config.fog_credentials = {
|
681
|
-
:
|
682
|
-
:
|
683
|
-
:
|
762
|
+
provider: 'Google',
|
763
|
+
google_storage_access_key_id: 'xxxxxx',
|
764
|
+
google_storage_secret_access_key: 'yyyyyy'
|
684
765
|
}
|
685
766
|
config.fog_directory = 'name_of_directory'
|
686
767
|
end
|
@@ -697,6 +778,31 @@ end
|
|
697
778
|
That's it! You can still use the `CarrierWave::Uploader#url` method to return
|
698
779
|
the url to the file on Google.
|
699
780
|
|
781
|
+
## Optimized Loading of Fog
|
782
|
+
|
783
|
+
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:
|
784
|
+
|
785
|
+
```ruby
|
786
|
+
gem "fog", "~> 1.27", require: "fog/rackspace/storage"
|
787
|
+
gem "carrierwave"
|
788
|
+
```
|
789
|
+
|
790
|
+
A couple of notes about versions:
|
791
|
+
* This functionality was introduced in Fog v1.20.
|
792
|
+
* This functionality is slated for CarrierWave v1.0.0.
|
793
|
+
|
794
|
+
If you're not relying on Gemfile entries alone and are requiring "carrierwave" anywhere, ensure you require "fog/rackspace/storage" before it. Ex:
|
795
|
+
|
796
|
+
```ruby
|
797
|
+
require "fog/rackspace/storage"
|
798
|
+
require "carrierwave"
|
799
|
+
```
|
800
|
+
|
801
|
+
Beware that this specific require is only needed when working with a fog provider that was not extracted to its own gem yet.
|
802
|
+
A list of the extracted providers can be found in the page of the `fog` organizations [here](https://github.com/fog).
|
803
|
+
|
804
|
+
When in doubt, inspect `Fog.constants` to see what has been loaded.
|
805
|
+
|
700
806
|
## Dynamic Asset Host
|
701
807
|
|
702
808
|
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.
|
@@ -734,8 +840,8 @@ Convert will only work if the file has the same file extension, thus the use of
|
|
734
840
|
class AvatarUploader < CarrierWave::Uploader::Base
|
735
841
|
include CarrierWave::RMagick
|
736
842
|
|
737
|
-
process :
|
738
|
-
process :
|
843
|
+
process resize_to_fill: [200, 200]
|
844
|
+
process convert: 'png'
|
739
845
|
|
740
846
|
def filename
|
741
847
|
super.chomp(File.extname(super)) + '.png' if original_filename.present?
|
@@ -767,7 +873,7 @@ for the RMagick processor.
|
|
767
873
|
class AvatarUploader < CarrierWave::Uploader::Base
|
768
874
|
include CarrierWave::MiniMagick
|
769
875
|
|
770
|
-
process :
|
876
|
+
process resize_to_fill: [200, 200]
|
771
877
|
end
|
772
878
|
```
|
773
879
|
|
@@ -787,14 +893,10 @@ details.
|
|
787
893
|
Be sure to use mount_on to specify the correct column:
|
788
894
|
|
789
895
|
```ruby
|
790
|
-
mount_uploader :avatar, AvatarUploader, :
|
896
|
+
mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
|
791
897
|
```
|
792
898
|
|
793
|
-
|
794
|
-
sensible compatibility mode. Patches for migrating from other solutions will be
|
795
|
-
happily accepted.
|
796
|
-
|
797
|
-
## i18n
|
899
|
+
## I18n
|
798
900
|
|
799
901
|
The Active Record validations use the Rails i18n framework. Add these keys to
|
800
902
|
your translations file:
|
@@ -805,8 +907,8 @@ errors:
|
|
805
907
|
carrierwave_processing_error: "Cannot resize image."
|
806
908
|
carrierwave_integrity_error: "Not an image."
|
807
909
|
carrierwave_download_error: "Couldn't download image."
|
808
|
-
|
809
|
-
|
910
|
+
extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
|
911
|
+
extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
|
810
912
|
```
|
811
913
|
|
812
914
|
## Large files
|
@@ -822,6 +924,7 @@ class MyUploader < CarrierWave::Uploader::Base
|
|
822
924
|
def move_to_cache
|
823
925
|
true
|
824
926
|
end
|
927
|
+
|
825
928
|
def move_to_store
|
826
929
|
true
|
827
930
|
end
|
@@ -848,9 +951,10 @@ Will add these callbacks:
|
|
848
951
|
```ruby
|
849
952
|
after_save :store_avatar!
|
850
953
|
before_save :write_avatar_identifier
|
851
|
-
after_commit :remove_avatar
|
852
|
-
|
853
|
-
after_save :
|
954
|
+
after_commit :remove_avatar!, on: :destroy
|
955
|
+
after_commit :mark_remove_avatar_false, on: :update
|
956
|
+
after_save :store_previous_changes_for_avatar
|
957
|
+
after_commit :remove_previously_stored_avatar, on: :update
|
854
958
|
```
|
855
959
|
|
856
960
|
If you want to skip any of these callbacks (eg. you want to keep the existing
|
@@ -860,7 +964,7 @@ avatar, even after uploading a new one), you can use ActiveRecord’s
|
|
860
964
|
```ruby
|
861
965
|
class User
|
862
966
|
mount_uploader :avatar, AvatarUploader
|
863
|
-
skip_callback :
|
967
|
+
skip_callback :commit, :after, :remove_previously_stored_avatar
|
864
968
|
end
|
865
969
|
```
|
866
970
|
|
@@ -870,7 +974,7 @@ See [CONTRIBUTING.md](https://github.com/carrierwaveuploader/carrierwave/blob/ma
|
|
870
974
|
|
871
975
|
## License
|
872
976
|
|
873
|
-
Copyright (c) 2008-
|
977
|
+
Copyright (c) 2008-2015 Jonas Nicklas
|
874
978
|
|
875
979
|
Permission is hereby granted, free of charge, to any person obtaining
|
876
980
|
a copy of this software and associated documentation files (the
|