mini-smart-pkg 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/carrierwave-3.1.3/README.md +1214 -0
  3. data/carrierwave-3.1.3/lib/carrierwave/compatibility/paperclip.rb +105 -0
  4. data/carrierwave-3.1.3/lib/carrierwave/downloader/base.rb +101 -0
  5. data/carrierwave-3.1.3/lib/carrierwave/downloader/remote_file.rb +68 -0
  6. data/carrierwave-3.1.3/lib/carrierwave/error.rb +8 -0
  7. data/carrierwave-3.1.3/lib/carrierwave/locale/en.yml +17 -0
  8. data/carrierwave-3.1.3/lib/carrierwave/mount.rb +446 -0
  9. data/carrierwave-3.1.3/lib/carrierwave/mounter.rb +257 -0
  10. data/carrierwave-3.1.3/lib/carrierwave/orm/activerecord.rb +68 -0
  11. data/carrierwave-3.1.3/lib/carrierwave/processing/mini_magick.rb +362 -0
  12. data/carrierwave-3.1.3/lib/carrierwave/processing/rmagick.rb +433 -0
  13. data/carrierwave-3.1.3/lib/carrierwave/processing/vips.rb +315 -0
  14. data/carrierwave-3.1.3/lib/carrierwave/processing.rb +3 -0
  15. data/carrierwave-3.1.3/lib/carrierwave/sanitized_file.rb +361 -0
  16. data/carrierwave-3.1.3/lib/carrierwave/storage/abstract.rb +43 -0
  17. data/carrierwave-3.1.3/lib/carrierwave/storage/file.rb +124 -0
  18. data/carrierwave-3.1.3/lib/carrierwave/storage/fog.rb +560 -0
  19. data/carrierwave-3.1.3/lib/carrierwave/storage.rb +3 -0
  20. data/carrierwave-3.1.3/lib/carrierwave/test/matchers.rb +398 -0
  21. data/carrierwave-3.1.3/lib/carrierwave/uploader/cache.rb +223 -0
  22. data/carrierwave-3.1.3/lib/carrierwave/uploader/callbacks.rb +33 -0
  23. data/carrierwave-3.1.3/lib/carrierwave/uploader/configuration.rb +229 -0
  24. data/carrierwave-3.1.3/lib/carrierwave/uploader/content_type_allowlist.rb +62 -0
  25. data/carrierwave-3.1.3/lib/carrierwave/uploader/content_type_denylist.rb +65 -0
  26. data/carrierwave-3.1.3/lib/carrierwave/uploader/default_url.rb +17 -0
  27. data/carrierwave-3.1.3/lib/carrierwave/uploader/dimension.rb +66 -0
  28. data/carrierwave-3.1.3/lib/carrierwave/uploader/download.rb +24 -0
  29. data/carrierwave-3.1.3/lib/carrierwave/uploader/extension_allowlist.rb +63 -0
  30. data/carrierwave-3.1.3/lib/carrierwave/uploader/extension_denylist.rb +64 -0
  31. data/carrierwave-3.1.3/lib/carrierwave/uploader/file_size.rb +43 -0
  32. data/carrierwave-3.1.3/lib/carrierwave/uploader/mountable.rb +44 -0
  33. data/carrierwave-3.1.3/lib/carrierwave/uploader/processing.rb +128 -0
  34. data/carrierwave-3.1.3/lib/carrierwave/uploader/proxy.rb +99 -0
  35. data/carrierwave-3.1.3/lib/carrierwave/uploader/remove.rb +21 -0
  36. data/carrierwave-3.1.3/lib/carrierwave/uploader/serialization.rb +28 -0
  37. data/carrierwave-3.1.3/lib/carrierwave/uploader/store.rb +168 -0
  38. data/carrierwave-3.1.3/lib/carrierwave/uploader/url.rb +44 -0
  39. data/carrierwave-3.1.3/lib/carrierwave/uploader/versions.rb +352 -0
  40. data/carrierwave-3.1.3/lib/carrierwave/uploader.rb +69 -0
  41. data/carrierwave-3.1.3/lib/carrierwave/utilities/file_name.rb +47 -0
  42. data/carrierwave-3.1.3/lib/carrierwave/utilities/uri.rb +26 -0
  43. data/carrierwave-3.1.3/lib/carrierwave/utilities.rb +7 -0
  44. data/carrierwave-3.1.3/lib/carrierwave/validations/active_model.rb +76 -0
  45. data/carrierwave-3.1.3/lib/carrierwave/version.rb +3 -0
  46. data/carrierwave-3.1.3/lib/carrierwave.rb +108 -0
  47. data/carrierwave-3.1.3/lib/generators/templates/uploader.rb.erb +56 -0
  48. data/carrierwave-3.1.3/lib/generators/uploader_generator.rb +7 -0
  49. data/mini-smart-pkg.gemspec +12 -0
  50. metadata +89 -0
@@ -0,0 +1,1214 @@
1
+ # CarrierWave
2
+
3
+ This gem provides a simple and extremely flexible way to upload files from Ruby applications.
4
+ It works well with Rack based web applications, such as Ruby on Rails.
5
+
6
+ [![Build Status](https://github.com/carrierwaveuploader/carrierwave/workflows/Test/badge.svg)](https://github.com/carrierwaveuploader/carrierwave/actions)
7
+ [![Code Climate](https://codeclimate.com/github/carrierwaveuploader/carrierwave.svg)](https://codeclimate.com/github/carrierwaveuploader/carrierwave)
8
+ [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=carrierwave&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=carrierwave&package-manager=bundler&version-scheme=semver)
9
+
10
+
11
+ ## Information
12
+
13
+ * RDoc documentation [available on RubyDoc.info](https://rubydoc.info/gems/carrierwave)
14
+ * Source code [available on GitHub](http://github.com/carrierwaveuploader/carrierwave)
15
+ * More information, known limitations, and how-tos [available on the wiki](https://github.com/carrierwaveuploader/carrierwave/wiki)
16
+
17
+ ## Getting Help
18
+
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.
20
+ * Please report bugs on the [issue tracker](http://github.com/carrierwaveuploader/carrierwave/issues) but read the "getting help" section in the wiki first.
21
+
22
+ ## Installation
23
+
24
+ Install the latest release:
25
+
26
+ ```
27
+ $ gem install carrierwave
28
+ ```
29
+
30
+ In Rails, add it to your Gemfile:
31
+
32
+ ```ruby
33
+ gem 'carrierwave', '~> 3.0'
34
+ ```
35
+
36
+ Finally, restart the server to apply the changes.
37
+
38
+ ## Upgrading from 2.x or earlier
39
+
40
+ CarrierWave 3.0 comes with a change in the way of handling the file extension on conversion. This results in following issues if you use `process convert: :format` to change the file format:
41
+
42
+ - If you have it on the uploader itself (not within a version), the file extension of the cached file will change. That means if you serve both CarrierWave 2.x and 3.x simultaneously on the same workload (e.g. using blue-green deployment), a cache file stored by 2.x can't be retrieved by 3.x and vice versa.
43
+ - If you have it within a version, the file extension of the stored file will change. You need to perform `#recreate_versions!` to make it usable again.
44
+
45
+ To preserve the 2.x behavior, you can set `force_extension false` right after calling `process convert: :format`. See [#2659](https://github.com/carrierwaveuploader/carrierwave/pull/2659) for the detail.
46
+
47
+ ## Getting Started
48
+
49
+ Start off by generating an uploader:
50
+
51
+ rails generate uploader Avatar
52
+
53
+ this should give you a file in:
54
+
55
+ app/uploaders/avatar_uploader.rb
56
+
57
+ Check out this file for some hints on how you can customize your uploader. It
58
+ should look something like this:
59
+
60
+ ```ruby
61
+ class AvatarUploader < CarrierWave::Uploader::Base
62
+ storage :file
63
+ end
64
+ ```
65
+
66
+ You can use your uploader class to store and retrieve files like this:
67
+
68
+ ```ruby
69
+ uploader = AvatarUploader.new
70
+
71
+ uploader.store!(my_file)
72
+
73
+ uploader.retrieve_from_store!('my_file.png')
74
+ ```
75
+
76
+ CarrierWave gives you a `store` for permanent storage, and a `cache` for
77
+ temporary storage. You can use different stores, including filesystem
78
+ and cloud storage.
79
+
80
+ Most of the time you are going to want to use CarrierWave together with an ORM.
81
+ It is quite simple to mount uploaders on columns in your model, so you can
82
+ simply assign files and get going:
83
+
84
+ ### ActiveRecord
85
+
86
+ Make sure you are loading CarrierWave after loading your ORM, otherwise you'll
87
+ need to require the relevant extension manually, e.g.:
88
+
89
+ ```ruby
90
+ require 'carrierwave/orm/activerecord'
91
+ ```
92
+
93
+ Add a string column to the model you want to mount the uploader by creating
94
+ a migration:
95
+
96
+
97
+ rails g migration add_avatar_to_users avatar:string
98
+ rails db:migrate
99
+
100
+ Open your model file and mount the uploader:
101
+
102
+ ```ruby
103
+ class User < ApplicationRecord
104
+ mount_uploader :avatar, AvatarUploader
105
+ end
106
+ ```
107
+
108
+ Now you can cache files by assigning them to the attribute, they will
109
+ automatically be stored when the record is saved.
110
+
111
+ ```ruby
112
+ u = User.new
113
+ u.avatar = params[:file] # Assign a file like this, or
114
+
115
+ # like this
116
+ File.open('somewhere') do |f|
117
+ u.avatar = f
118
+ end
119
+
120
+ u.save!
121
+ u.avatar.url # => '/url/to/file.png'
122
+ u.avatar.current_path # => 'path/to/file.png'
123
+ u.avatar_identifier # => 'file.png'
124
+ ```
125
+
126
+ **Note**: `u.avatar` will never return nil, even if there is no photo associated to it.
127
+ To check if a photo was saved to the model, use `u.avatar.file.nil?` instead.
128
+
129
+ ### DataMapper, Mongoid, Sequel
130
+
131
+ Other ORM support has been extracted into separate gems:
132
+
133
+ * [carrierwave-datamapper](https://github.com/carrierwaveuploader/carrierwave-datamapper)
134
+ * [carrierwave-mongoid](https://github.com/carrierwaveuploader/carrierwave-mongoid)
135
+ * [carrierwave-sequel](https://github.com/carrierwaveuploader/carrierwave-sequel)
136
+
137
+ There are more extensions listed in [the wiki](https://github.com/carrierwaveuploader/carrierwave/wiki)
138
+
139
+ ## Multiple file uploads
140
+
141
+ CarrierWave also has convenient support for multiple file upload fields.
142
+
143
+ ### ActiveRecord
144
+
145
+ Add a column which can store an array. This could be an array column or a JSON
146
+ column for example. Your choice depends on what your database supports. For
147
+ example, create a migration like this:
148
+
149
+
150
+ #### For databases with ActiveRecord json data type support (e.g. PostgreSQL, MySQL)
151
+
152
+ rails g migration add_avatars_to_users avatars:json
153
+ rails db:migrate
154
+
155
+ #### For database without ActiveRecord json data type support (e.g. SQLite)
156
+
157
+ rails g migration add_avatars_to_users avatars:string
158
+ rails db:migrate
159
+
160
+ __Note__: JSON datatype doesn't exists in SQLite adapter, that's why you can use a string datatype which will be serialized in model.
161
+
162
+ Open your model file and mount the uploader:
163
+
164
+
165
+ ```ruby
166
+ class User < ApplicationRecord
167
+ mount_uploaders :avatars, AvatarUploader
168
+ serialize :avatars, JSON # If you use SQLite, add this line.
169
+ end
170
+ ```
171
+
172
+ Make sure that you mount the uploader with write (mount_uploaders) with `s` not (mount_uploader)
173
+ in order to avoid errors when uploading multiple files
174
+
175
+ Make sure your file input fields are set up as multiple file fields. For
176
+ example in Rails you'll want to do something like this:
177
+
178
+ ```erb
179
+ <%= form.file_field :avatars, multiple: true %>
180
+ ```
181
+
182
+ Also, make sure your upload controller permits the multiple file upload attribute, *pointing to an empty array in a hash*. For example:
183
+
184
+ ```ruby
185
+ params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
186
+ ```
187
+
188
+ Now you can select multiple files in the upload dialog (e.g. SHIFT+SELECT), and they will
189
+ automatically be stored when the record is saved.
190
+
191
+ ```ruby
192
+ u = User.new(params[:user])
193
+ u.save!
194
+ u.avatars[0].url # => '/url/to/file.png'
195
+ u.avatars[0].current_path # => 'path/to/file.png'
196
+ u.avatars[0].identifier # => 'file.png'
197
+ ```
198
+
199
+ If you want to preserve existing files on uploading new one, you can go like:
200
+
201
+ ```erb
202
+ <% user.avatars.each do |avatar| %>
203
+ <%= hidden_field :user, :avatars, multiple: true, value: avatar.identifier %>
204
+ <% end %>
205
+ <%= form.file_field :avatars, multiple: true %>
206
+ ```
207
+
208
+ Sorting avatars is supported as well by reordering `hidden_field`, an example using jQuery UI Sortable is available [here](https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Add%2C-remove-and-reorder-images-using-multiple-file-upload).
209
+
210
+ ## Changing the storage directory
211
+
212
+ In order to change where uploaded files are put, just override the `store_dir`
213
+ method:
214
+
215
+ ```ruby
216
+ class MyUploader < CarrierWave::Uploader::Base
217
+ def store_dir
218
+ 'public/my/upload/directory'
219
+ end
220
+ end
221
+ ```
222
+
223
+ This works for the file storage as well as Amazon S3 and Rackspace Cloud Files.
224
+ Define `store_dir` as `nil` if you'd like to store files at the root level.
225
+
226
+ If you store files outside the project root folder, you may want to define `cache_dir` in the same way:
227
+
228
+ ```ruby
229
+ class MyUploader < CarrierWave::Uploader::Base
230
+ def cache_dir
231
+ '/tmp/projectname-cache'
232
+ end
233
+ end
234
+ ```
235
+
236
+ ## Changing the filename
237
+
238
+ To change the filename of uploaded files, you can override `#filename` method in the uploader.
239
+
240
+ ```ruby
241
+ class MyUploader < CarrierWave::Uploader::Base
242
+ def filename
243
+ "image.#{file.extension}" # If you upload 'file.jpg', you'll get 'image.jpg'
244
+ end
245
+ end
246
+ ```
247
+
248
+ Some old documentations (like [this](https://stackoverflow.com/a/5865117)) may instruct you to safeguard the filename value with `if original_filename`, but it's no longer necessary with CarrierWave 3.0 or later.
249
+
250
+ ## Securing uploads
251
+
252
+ Certain files might be dangerous if uploaded to the wrong location, such as PHP
253
+ files or other script files. CarrierWave allows you to specify an allowlist of
254
+ allowed extensions or content types.
255
+
256
+ If you're mounting the uploader, uploading a file with the wrong extension will
257
+ make the record invalid instead. Otherwise, an error is raised.
258
+
259
+ ```ruby
260
+ class MyUploader < CarrierWave::Uploader::Base
261
+ def extension_allowlist
262
+ %w(jpg jpeg gif png)
263
+ end
264
+ end
265
+ ```
266
+
267
+ The same thing could be done using content types.
268
+ Let's say we need an uploader that accepts only images. This can be done like this
269
+
270
+ ```ruby
271
+ class MyUploader < CarrierWave::Uploader::Base
272
+ def content_type_allowlist
273
+ /image\//
274
+ end
275
+ end
276
+ ```
277
+
278
+ You can use a denylist to reject content types.
279
+ Let's say we need an uploader that reject JSON files. This can be done like this
280
+
281
+ ```ruby
282
+ class NoJsonUploader < CarrierWave::Uploader::Base
283
+ def content_type_denylist
284
+ ['application/text', 'application/json']
285
+ end
286
+ end
287
+ ```
288
+
289
+ ### CVE-2016-3714 (ImageTragick)
290
+ This version of CarrierWave has the ability to mitigate CVE-2016-3714. However, you **MUST** set a content_type_allowlist in your uploaders for this protection to be effective, and you **MUST** either disable ImageMagick's default SVG delegate or use the RSVG delegate for SVG processing.
291
+
292
+
293
+ A valid allowlist that will restrict your uploader to images only, and mitigate the CVE is:
294
+
295
+ ```ruby
296
+ class MyUploader < CarrierWave::Uploader::Base
297
+ def content_type_allowlist
298
+ [/image\//]
299
+ end
300
+ end
301
+ ```
302
+
303
+ **WARNING**: A `content_type_allowlist` is the only form of allowlist or denylist supported by CarrierWave that can effectively mitigate against CVE-2016-3714. Use of `extension_allowlist` will not inspect the file headers, and thus still leaves your application open to the vulnerability.
304
+
305
+ ### Filenames and unicode chars
306
+
307
+ Another security issue you should care for is the file names (see
308
+ [Ruby On Rails Security Guide](http://guides.rubyonrails.org/security.html#file-uploads)).
309
+ By default, CarrierWave provides only English letters, arabic numerals and some symbols as
310
+ allowlisted characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you
311
+ have to override `sanitize_regexp` method. It should return regular expression which would match
312
+ all *non*-allowed symbols.
313
+
314
+ ```ruby
315
+ CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
316
+ ```
317
+
318
+ Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party
319
+ plugins or client-side software.
320
+
321
+ ## Setting the content type
322
+
323
+ As of v0.11.0, the `mime-types` gem is a runtime dependency and the content type is set automatically.
324
+ You no longer need to do this manually.
325
+
326
+ ## Adding versions
327
+
328
+ Often you'll want to add different versions of the same file. The classic example is generating image thumbnails while preserving the original file to be used for high-quality representation.
329
+ In this section we'll explore how CarrierWave supports working with multiple versions. The image manipulation itself is covered in [another section](#manipulating-images).
330
+
331
+ ```ruby
332
+ class MyUploader < CarrierWave::Uploader::Base
333
+ include CarrierWave::MiniMagick
334
+
335
+ process resize_to_fit: [800, 800]
336
+
337
+ version :thumb do
338
+ process resize_to_fill: [200,200]
339
+ end
340
+
341
+ end
342
+ ```
343
+
344
+ When this uploader is used, an uploaded image would be scaled to be no larger
345
+ than 800 by 800 pixels. The original aspect ratio will be kept.
346
+
347
+ A version called `:thumb` is then created, which is scaled
348
+ to exactly 200 by 200 pixels. The thumbnail uses `resize_to_fill` which makes sure
349
+ that the width and height specified are filled, only cropping
350
+ if the aspect ratio requires it.
351
+
352
+ The above uploader could be used like this:
353
+
354
+ ```ruby
355
+ uploader = AvatarUploader.new
356
+ uploader.store!(my_file) # size: 1024x768
357
+
358
+ uploader.url # => '/url/to/my_file.png' # size: 800x800
359
+ uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
360
+ ```
361
+
362
+ One important thing to remember is that process is called *before* versions are
363
+ created. This can cut down on processing cost.
364
+
365
+ ### Conditional processing
366
+
367
+ If you want to use conditional process, you can only use `if` statement.
368
+
369
+ See `carrierwave/uploader/processing.rb` for details.
370
+
371
+ ```ruby
372
+ class MyUploader < CarrierWave::Uploader::Base
373
+ process :scale => [200, 200], :if => :image?
374
+
375
+ def image?(carrier_wave_sanitized_file)
376
+ true
377
+ end
378
+ end
379
+ ```
380
+
381
+ ### Nested versions
382
+
383
+ It is possible to nest versions within versions:
384
+
385
+ ```ruby
386
+ class MyUploader < CarrierWave::Uploader::Base
387
+
388
+ version :animal do
389
+ version :human
390
+ version :monkey
391
+ version :llama
392
+ end
393
+ end
394
+ ```
395
+
396
+ ### Conditional versions
397
+
398
+ Occasionally you want to restrict the creation of versions on certain
399
+ properties within the model or based on the picture itself.
400
+
401
+ ```ruby
402
+ class MyUploader < CarrierWave::Uploader::Base
403
+
404
+ version :human, if: :is_human?
405
+ version :monkey, if: :is_monkey?
406
+ version :banner, if: :is_landscape?
407
+
408
+ private
409
+
410
+ def is_human? picture
411
+ model.can_program?(:ruby)
412
+ end
413
+
414
+ def is_monkey? picture
415
+ model.favorite_food == 'banana'
416
+ end
417
+
418
+ def is_landscape? picture
419
+ image = MiniMagick::Image.new(picture.path)
420
+ image[:width] > image[:height]
421
+ end
422
+
423
+ end
424
+ ```
425
+
426
+ The `model` variable points to the instance object the uploader is attached to.
427
+
428
+ ### Create versions from existing versions
429
+
430
+ For performance reasons, it is often useful to create versions from existing ones
431
+ instead of using the original file. If your uploader generates several versions
432
+ where the next is smaller than the last, it will take less time to generate from
433
+ a smaller, already processed image.
434
+
435
+ ```ruby
436
+ class MyUploader < CarrierWave::Uploader::Base
437
+
438
+ version :thumb do
439
+ process resize_to_fill: [280, 280]
440
+ end
441
+
442
+ version :small_thumb, from_version: :thumb do
443
+ process resize_to_fill: [20, 20]
444
+ end
445
+
446
+ end
447
+ ```
448
+
449
+ ### Customizing version filenames
450
+
451
+ CarrierWave supports [customization of filename](#changing-the-filename) by overriding an uploader's
452
+ #filename method, but this doesn't work for versions because of the limitation on how CarrierWave
453
+ re-constructs the filename on retrieval of the stored file.
454
+ Instead, you can override `#full_filename` with providing a version-aware name.
455
+
456
+ ```ruby
457
+ class MyUploader < CarrierWave::Uploader::Base
458
+ version :thumb do
459
+ def full_filename(for_file)
460
+ 'thumb.png'
461
+ end
462
+ process convert: 'png'
463
+ end
464
+ end
465
+ ```
466
+
467
+ Please note that `#full_filename` mustn't be constructed based on a dynamic value
468
+ that can change from the time of store and time of retrieval, since it will result in
469
+ being unable to retrieve a file previously stored.
470
+
471
+ ## Making uploads work across form redisplays
472
+
473
+ Often you'll notice that uploaded files disappear when a validation fails.
474
+ CarrierWave has a feature that makes it easy to remember the uploaded file even
475
+ in that case. Suppose your `user` model has an uploader mounted on `avatar`
476
+ file, just add a hidden field called `avatar_cache` (don't forget to add it to
477
+ the attr_accessible list as necessary). In Rails, this would look like this:
478
+
479
+ ```erb
480
+ <%= form_for @user, html: { multipart: true } do |f| %>
481
+ <p>
482
+ <label>My Avatar</label>
483
+ <%= f.file_field :avatar %>
484
+ <%= f.hidden_field :avatar_cache %>
485
+ </p>
486
+ <% end %>
487
+ ````
488
+
489
+ It might be a good idea to show the user that a file has been uploaded, in the
490
+ case of images, a small thumbnail would be a good indicator:
491
+
492
+ ```erb
493
+ <%= form_for @user, html: { multipart: true } do |f| %>
494
+ <p>
495
+ <label>My Avatar</label>
496
+ <%= image_tag(@user.avatar_url) if @user.avatar? %>
497
+ <%= f.file_field :avatar %>
498
+ <%= f.hidden_field :avatar_cache %>
499
+ </p>
500
+ <% end %>
501
+ ```
502
+
503
+ ## Removing uploaded files
504
+
505
+ If you want to remove a previously uploaded file on a mounted uploader, you can
506
+ easily add a checkbox to the form which will remove the file when checked.
507
+
508
+ ```erb
509
+ <%= form_for @user, html: { multipart: true } do |f| %>
510
+ <p>
511
+ <label>My Avatar</label>
512
+ <%= image_tag(@user.avatar_url) if @user.avatar? %>
513
+ <%= f.file_field :avatar %>
514
+ </p>
515
+
516
+ <p>
517
+ <label>
518
+ <%= f.check_box :remove_avatar %>
519
+ Remove avatar
520
+ </label>
521
+ </p>
522
+ <% end %>
523
+ ```
524
+
525
+ If you want to remove the file manually, you can call <code>remove_avatar!</code>, then save the object.
526
+
527
+ ```erb
528
+ @user.remove_avatar!
529
+ @user.save
530
+ #=> true
531
+ ```
532
+
533
+ ## Uploading files from a remote location
534
+
535
+ Your users may find it convenient to upload a file from a location on the Internet
536
+ via a URL. CarrierWave makes this simple, just add the appropriate attribute to your
537
+ form and you're good to go:
538
+
539
+ ```erb
540
+ <%= form_for @user, html: { multipart: true } do |f| %>
541
+ <p>
542
+ <label>My Avatar URL:</label>
543
+ <%= image_tag(@user.avatar_url) if @user.avatar? %>
544
+ <%= f.text_field :remote_avatar_url %>
545
+ </p>
546
+ <% end %>
547
+ ```
548
+
549
+ If you're using ActiveRecord, CarrierWave will indicate invalid URLs and download
550
+ failures automatically with attribute validation errors. If you aren't, or you
551
+ disable CarrierWave's `validate_download` option, you'll need to handle those
552
+ errors yourself.
553
+
554
+ ### Retry option for download from remote location
555
+ If you want to retry the download from the Remote URL, enable the download_retry_count option, an error occurs during download, it will try to execute the specified number of times.
556
+ This option is effective when the remote destination is unstable.
557
+
558
+ ```rb
559
+ CarrierWave.configure do |config|
560
+ config.download_retry_count = 3 # Default 0
561
+ config.download_retry_wait_time = 3 # Default 5
562
+ end
563
+ ```
564
+
565
+ ## Providing a default URL
566
+
567
+ In many cases, especially when working with images, it might be a good idea to
568
+ provide a default url, a fallback in case no file has been uploaded. You can do
569
+ this easily by overriding the `default_url` method in your uploader:
570
+
571
+ ```ruby
572
+ class MyUploader < CarrierWave::Uploader::Base
573
+ def default_url(*args)
574
+ "/images/fallback/" + [version_name, "default.png"].compact.join('_')
575
+ end
576
+ end
577
+ ```
578
+
579
+ Or if you are using the Rails asset pipeline:
580
+
581
+ ```ruby
582
+ class MyUploader < CarrierWave::Uploader::Base
583
+ def default_url(*args)
584
+ ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
585
+ end
586
+ end
587
+ ```
588
+
589
+ ## Recreating versions
590
+
591
+ You might come to a situation where you want to retroactively change a version
592
+ or add a new one. You can use the `recreate_versions!` method to recreate the
593
+ versions from the base file. This uses a naive approach which will re-upload and
594
+ process the specified version or all versions, if none is passed as an argument.
595
+
596
+ When you are generating random unique filenames you have to call `save!` on
597
+ the model after using `recreate_versions!`. This is necessary because
598
+ `recreate_versions!` doesn't save the new filename to the database. Calling
599
+ `save!` yourself will prevent that the database and file system are running
600
+ out of sync.
601
+
602
+ ```ruby
603
+ instance = MyUploader.new
604
+ instance.recreate_versions!(:thumb, :large)
605
+ ```
606
+
607
+ Or on a mounted uploader:
608
+
609
+ ```ruby
610
+ User.find_each do |user|
611
+ user.avatar.recreate_versions!
612
+ end
613
+ ```
614
+
615
+ 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:
616
+
617
+ ```ruby
618
+ User.find_each do |user|
619
+ user.avatar.recreate_versions! if user.avatar?
620
+ end
621
+ ```
622
+
623
+ ## Configuring CarrierWave
624
+
625
+ CarrierWave has a broad range of configuration options, which you can configure,
626
+ both globally and on a per-uploader basis:
627
+
628
+ ```ruby
629
+ CarrierWave.configure do |config|
630
+ config.permissions = 0666
631
+ config.directory_permissions = 0777
632
+ config.storage = :file
633
+ end
634
+ ```
635
+
636
+ Or alternatively:
637
+
638
+ ```ruby
639
+ class AvatarUploader < CarrierWave::Uploader::Base
640
+ permissions 0777
641
+ end
642
+ ```
643
+
644
+ If you're using Rails, create an initializer for this:
645
+
646
+ config/initializers/carrierwave.rb
647
+
648
+ If you want CarrierWave to fail noisily in development, you can change these configs in your environment file:
649
+
650
+ ```ruby
651
+ CarrierWave.configure do |config|
652
+ config.ignore_integrity_errors = false
653
+ config.ignore_processing_errors = false
654
+ config.ignore_download_errors = false
655
+ end
656
+ ```
657
+
658
+
659
+ ## Testing with CarrierWave
660
+
661
+ It's a good idea to test your uploaders in isolation. In order to speed up your
662
+ tests, it's recommended to switch off processing in your tests, and to use the file storage.
663
+ Also, you can disable SSRF protection at your own risk using the `skip_ssrf_protection` configuration.
664
+
665
+ In Rails you could do that by adding an initializer with:
666
+
667
+ ```ruby
668
+ if Rails.env.test? or Rails.env.cucumber?
669
+ CarrierWave.configure do |config|
670
+ config.storage = :file
671
+ config.enable_processing = false
672
+ config.skip_ssrf_protection = true
673
+ end
674
+ end
675
+ ```
676
+
677
+ Remember, if you have already set `storage :something` in your uploader, the `storage`
678
+ setting from this initializer will be ignored.
679
+
680
+ If you need to test your processing, you should test it in isolation, and enable
681
+ processing only for those tests that need it.
682
+
683
+ CarrierWave comes with some RSpec matchers which you may find useful:
684
+
685
+ ```ruby
686
+ require 'carrierwave/test/matchers'
687
+
688
+ describe MyUploader do
689
+ include CarrierWave::Test::Matchers
690
+
691
+ let(:user) { double('user') }
692
+ let(:uploader) { MyUploader.new(user, :avatar) }
693
+
694
+ before do
695
+ MyUploader.enable_processing = true
696
+ File.open(path_to_file) { |f| uploader.store!(f) }
697
+ end
698
+
699
+ after do
700
+ MyUploader.enable_processing = false
701
+ uploader.remove!
702
+ end
703
+
704
+ context 'the thumb version' do
705
+ it "scales down a landscape image to be exactly 64 by 64 pixels" do
706
+ expect(uploader.thumb).to have_dimensions(64, 64)
707
+ end
708
+ end
709
+
710
+ context 'the small version' do
711
+ it "scales down a landscape image to fit within 200 by 200 pixels" do
712
+ expect(uploader.small).to be_no_larger_than(200, 200)
713
+ end
714
+ end
715
+
716
+ it "makes the image readable only to the owner and not executable" do
717
+ expect(uploader).to have_permissions(0600)
718
+ end
719
+
720
+ it "has the correct format" do
721
+ expect(uploader).to be_format('png')
722
+ end
723
+ end
724
+ ```
725
+
726
+ If you're looking for minitest asserts, checkout [carrierwave_asserts](https://github.com/hcfairbanks/carrierwave_asserts).
727
+
728
+ Setting the enable_processing flag on an uploader will prevent any of the versions from processing as well.
729
+ Processing can be enabled for a single version by setting the processing flag on the version like so:
730
+
731
+ ```ruby
732
+ @uploader.thumb.enable_processing = true
733
+ ```
734
+
735
+ ## Fog
736
+
737
+ If you want to use fog you must add in your CarrierWave initializer the
738
+ following lines
739
+
740
+ ```ruby
741
+ config.fog_credentials = { ... } # Provider specific credentials
742
+ ```
743
+
744
+ ## Using Amazon S3
745
+
746
+ [Fog AWS](http://github.com/fog/fog-aws) is used to support Amazon S3. Ensure you have it in your Gemfile:
747
+
748
+ ```ruby
749
+ gem "fog-aws"
750
+ ```
751
+
752
+ You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.
753
+ For the sake of performance it is assumed that the directory already exists, so please create it if it needs to be.
754
+ You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:
755
+
756
+ ```ruby
757
+ CarrierWave.configure do |config|
758
+ config.fog_credentials = {
759
+ provider: 'AWS', # required
760
+ aws_access_key_id: 'xxx', # required unless using use_iam_profile
761
+ aws_secret_access_key: 'yyy', # required unless using use_iam_profile
762
+ use_iam_profile: true, # optional, defaults to false
763
+ region: 'eu-west-1', # optional, defaults to 'us-east-1'
764
+ host: 's3.example.com', # optional, defaults to nil
765
+ endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
766
+ }
767
+ config.fog_directory = 'name_of_bucket' # required
768
+ config.fog_public = false # optional, defaults to true
769
+ config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
770
+ # Use this if you have AWS S3 ACLs disabled.
771
+ # config.fog_attributes = { 'x-amz-acl' => 'bucket-owner-full-control' }
772
+ # Use this if you have Google Cloud Storage uniform bucket-level access enabled.
773
+ # config.fog_attributes = { uniform: true }
774
+ # For an application which utilizes multiple servers but does not need caches persisted across requests,
775
+ # uncomment the line :file instead of the default :storage. Otherwise, it will use AWS as the temp cache store.
776
+ # config.cache_storage = :file
777
+ end
778
+ ```
779
+
780
+ In your uploader, set the storage to :fog
781
+
782
+ ```ruby
783
+ class AvatarUploader < CarrierWave::Uploader::Base
784
+ storage :fog
785
+ end
786
+ ```
787
+
788
+ That's it! You can still use the `CarrierWave::Uploader#url` method to return the url to the file on Amazon S3.
789
+
790
+ **Note**: for Carrierwave to work properly it needs credentials with the following permissions:
791
+
792
+ * `s3:ListBucket`
793
+ * `s3:PutObject`
794
+ * `s3:GetObject`
795
+ * `s3:DeleteObject`
796
+ * `s3:PutObjectAcl`
797
+
798
+ ## Using Rackspace Cloud Files
799
+
800
+ [Fog](http://github.com/fog/fog) is used to support Rackspace Cloud Files. Ensure you have it in your Gemfile:
801
+
802
+ ```ruby
803
+ gem "fog"
804
+ ```
805
+
806
+ You'll need to configure a directory (also known as a container), username and API key in the initializer.
807
+ For the sake of performance it is assumed that the directory already exists, so please create it if needs to be.
808
+
809
+ Using a US-based account:
810
+
811
+ ```ruby
812
+ CarrierWave.configure do |config|
813
+ config.fog_credentials = {
814
+ provider: 'Rackspace',
815
+ rackspace_username: 'xxxxxx',
816
+ rackspace_api_key: 'yyyyyy',
817
+ rackspace_region: :ord # optional, defaults to :dfw
818
+ }
819
+ config.fog_directory = 'name_of_directory'
820
+ end
821
+ ```
822
+
823
+ Using a UK-based account:
824
+
825
+ ```ruby
826
+ CarrierWave.configure do |config|
827
+ config.fog_credentials = {
828
+ provider: 'Rackspace',
829
+ rackspace_username: 'xxxxxx',
830
+ rackspace_api_key: 'yyyyyy',
831
+ rackspace_auth_url: Fog::Rackspace::UK_AUTH_ENDPOINT,
832
+ rackspace_region: :lon
833
+ }
834
+ config.fog_directory = 'name_of_directory'
835
+ end
836
+ ```
837
+
838
+ You can optionally include your CDN host name in the configuration.
839
+ This is *highly* recommended, as without it every request requires a lookup
840
+ of this information.
841
+
842
+ ```ruby
843
+ config.asset_host = "http://c000000.cdn.rackspacecloud.com"
844
+ ```
845
+
846
+ In your uploader, set the storage to :fog
847
+
848
+ ```ruby
849
+ class AvatarUploader < CarrierWave::Uploader::Base
850
+ storage :fog
851
+ end
852
+ ```
853
+
854
+ That's it! You can still use the `CarrierWave::Uploader#url` method to return
855
+ the url to the file on Rackspace Cloud Files.
856
+
857
+ ## Using Google Cloud Storage
858
+
859
+ [Fog](http://github.com/fog/fog-google) is used to support Google Cloud Storage. Ensure you have it in your Gemfile:
860
+
861
+ ```ruby
862
+ gem "fog-google"
863
+ ```
864
+
865
+ You'll need to configure a directory (also known as a bucket) and the credentials in the initializer.
866
+ For the sake of performance it is assumed that the directory already exists, so please create it if needs to be.
867
+
868
+ Please read the [fog-google README](https://github.com/fog/fog-google/blob/master/README.md) on how to get credentials.
869
+
870
+ For Google Storage JSON API (recommended):
871
+ ```ruby
872
+ CarrierWave.configure do |config|
873
+ config.fog_credentials = {
874
+ provider: 'Google',
875
+ google_project: 'my-project',
876
+ google_json_key_string: 'xxxxxx'
877
+ # or use google_json_key_location if using an actual file
878
+ }
879
+ config.fog_directory = 'google_cloud_storage_bucket_name'
880
+ end
881
+ ```
882
+
883
+ For Google Storage XML API:
884
+ ```ruby
885
+ CarrierWave.configure do |config|
886
+ config.fog_credentials = {
887
+ provider: 'Google',
888
+ google_storage_access_key_id: 'xxxxxx',
889
+ google_storage_secret_access_key: 'yyyyyy'
890
+ }
891
+ config.fog_directory = 'google_cloud_storage_bucket_name'
892
+ end
893
+ ```
894
+
895
+ In your uploader, set the storage to :fog
896
+
897
+ ```ruby
898
+ class AvatarUploader < CarrierWave::Uploader::Base
899
+ storage :fog
900
+ end
901
+ ```
902
+
903
+ That's it! You can still use the `CarrierWave::Uploader#url` method to return
904
+ the url to the file on Google.
905
+
906
+ ## Optimized Loading of Fog
907
+
908
+ 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:
909
+
910
+ ```ruby
911
+ gem "fog", "~> 1.27", require: "fog/rackspace/storage"
912
+ gem "carrierwave"
913
+ ```
914
+
915
+ A couple of notes about versions:
916
+ * This functionality was introduced in Fog v1.20.
917
+ * This functionality is slated for CarrierWave v1.0.0.
918
+
919
+ If you're not relying on Gemfile entries alone and are requiring "carrierwave" anywhere, ensure you require "fog/rackspace/storage" before it. Ex:
920
+
921
+ ```ruby
922
+ require "fog/rackspace/storage"
923
+ require "carrierwave"
924
+ ```
925
+
926
+ Beware that this specific require is only needed when working with a fog provider that was not extracted to its own gem yet.
927
+ A list of the extracted providers can be found in the page of the `fog` organizations [here](https://github.com/fog).
928
+
929
+ When in doubt, inspect `Fog.constants` to see what has been loaded.
930
+
931
+ ## Dynamic Asset Host
932
+
933
+ 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.
934
+
935
+ ```ruby
936
+ CarrierWave.configure do |config|
937
+ config.asset_host = proc do |file|
938
+ identifier = # some logic
939
+ "http://#{identifier}.cdn.rackspacecloud.com"
940
+ end
941
+ end
942
+ ```
943
+
944
+ ## Manipulating images
945
+
946
+ If you're uploading images, you'll probably want to manipulate them in some way,
947
+ you might want to create thumbnail images for example.
948
+
949
+ ### Using MiniMagick
950
+
951
+ MiniMagick performs all the operations using the 'convert' CLI which is part of the standard ImageMagick kit.
952
+ This allows you to have the power of ImageMagick without having to worry about installing
953
+ all the RMagick libraries, it often results in higher memory footprint.
954
+
955
+ See the MiniMagick site for more details:
956
+
957
+ https://github.com/minimagick/minimagick
958
+
959
+ To install Imagemagick on OSX with homebrew type the following:
960
+
961
+ ```
962
+ $ brew install imagemagick
963
+ ```
964
+
965
+ And the ImageMagick command line options for more for what's on offer:
966
+
967
+ http://www.imagemagick.org/script/command-line-options.php
968
+
969
+ Currently, the MiniMagick carrierwave processor provides exactly the same methods as
970
+ for the RMagick processor.
971
+
972
+ ```ruby
973
+ class AvatarUploader < CarrierWave::Uploader::Base
974
+ include CarrierWave::MiniMagick
975
+
976
+ process resize_to_fill: [200, 200]
977
+ end
978
+ ```
979
+
980
+ See `carrierwave/processing/mini_magick.rb` for details.
981
+
982
+ ### Using RMagick
983
+
984
+ CarrierWave also comes with support for RMagick, a well-known image processing library.
985
+ To use it, you'll need to include this in your Uploader:
986
+
987
+ ```ruby
988
+ class AvatarUploader < CarrierWave::Uploader::Base
989
+ include CarrierWave::RMagick
990
+ end
991
+ ```
992
+
993
+ The RMagick module gives you a few methods, like
994
+ `CarrierWave::RMagick#resize_to_fill` which manipulate the image file in some
995
+ way. You can set a `process` callback, which will call that method any time a
996
+ file is uploaded.
997
+ There is a demonstration of convert here.
998
+
999
+ ```ruby
1000
+ class AvatarUploader < CarrierWave::Uploader::Base
1001
+ include CarrierWave::RMagick
1002
+
1003
+ process resize_to_fill: [200, 200]
1004
+ process convert: 'png'
1005
+ end
1006
+ ```
1007
+
1008
+ Check out the manipulate! method, which makes it easy for you to write your own
1009
+ manipulation methods.
1010
+
1011
+ ### Using Vips
1012
+
1013
+ CarrierWave version 2.2.0 added support for the `libvips` image processing library, through [ImageProcessing::Vips](https://github.com/janko/image_processing/blob/master/doc/vips.md). Its functionality matches that of the RMagick and MiniMagick processors, but it uses less memory and offers [faster processing](https://github.com/libvips/libvips/wiki/Speed-and-memory-use). To use the Vips processing module you must first install `libvips`, for example:
1014
+
1015
+ ````bash
1016
+ $ sudo apt install libvips
1017
+ ````
1018
+
1019
+ You also need to tell your uploader to use Vips:
1020
+
1021
+ ````ruby
1022
+ class ImageFileUploader < CarrierWave::Uploader::Base
1023
+ include CarrierWave::Vips
1024
+ end
1025
+ ````
1026
+
1027
+ ### List of available processing methods:
1028
+
1029
+ > [!NOTE]
1030
+ > While the intention is to provide uniform interfaces to all three processing libraries the availability and implementation of processing methods can <a href="supported-processing-methods">vary slightly between them</a>.
1031
+
1032
+ - `convert` - Changes the image encoding format to the given format (eg. jpg). This operation is treated specially to trigger the change of the file extension, so it matches with the format of the resulting file.
1033
+ - `resize_to_limit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
1034
+ - `resize_to_fit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
1035
+ - `resize_to_fill` - Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension. Optionally, a "gravity" may be specified, for example "Center", or "NorthEast".
1036
+ - `resize_and_pad` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg). Optionally, a "gravity" may be specified, as above.
1037
+ - `crop` - Crop the image to the contents of a box with the specified height and width, positioned a given number of pixels from the top and left. The original image edge will be retained should the bottom and/or right edge of the box fall outside the image bounds.
1038
+
1039
+ #### Supported processing methods
1040
+
1041
+ The following table shows which processing methods are supported by each processing library, and which parameters they accept:
1042
+
1043
+ Method|RMagick|MiniMagick|Vips
1044
+ ------|-----------------|-----------------|-----------------|
1045
+ `convert`|`format`|`format`, `page`<sup>1</sup>|`format`, `page`<sup>1</sup>
1046
+ `resize_to_limit`|`width`, `height`|`width`, `height`|`width`, `height`
1047
+ `resize_to_fit`|`width`, `height`|`width`, `height`|`width`, `height`
1048
+ `resize_to_fill`|`width`, `height`, `gravity`<sup>2</sup>|`width`, `height`, `gravity`<sup>2</sup>|`width`, `height`
1049
+ `resize_and_pad`|`width`, `height`, `background`, `gravity`<sup>2</sup>|`width`, `height`, `background`, `gravity`<sup>2</sup>|`width`, `height`, `background`, `gravity`<sup>2</sup>
1050
+ `resize_to_geometry_string`|`geometry_string`<sup>3</sup>|*not implemented*|*not implemented*
1051
+ `crop`|`left`, `top`, `width`, `height`|`left`, `top`, `width`, `height`|`left`, `top`, `width`, `height`
1052
+
1053
+ <sup>1</sup>`page` refers to the page number when converting from PDF, frame number when converting from GIF, and layer number when converting from PSD.
1054
+
1055
+ <sup>2</sup>`gravity` refers to an image position given as one of `Center`, `North`, `NorthWest`, `West`, `SouthWest`, `South`, `SouthEast`, `East`, or `NorthEast`.
1056
+
1057
+ <sup>3</sup>`geometry_string` is an [ImageMagick geometry string](https://rmagick.github.io/imusage.html#geometry).
1058
+
1059
+ ## Migrating from Paperclip
1060
+
1061
+ If you are using Paperclip, you can use the provided compatibility module:
1062
+
1063
+ ```ruby
1064
+ class AvatarUploader < CarrierWave::Uploader::Base
1065
+ include CarrierWave::Compatibility::Paperclip
1066
+ end
1067
+ ```
1068
+
1069
+ See the documentation for `CarrierWave::Compatibility::Paperclip` for more
1070
+ details.
1071
+
1072
+ Be sure to use mount_on to specify the correct column:
1073
+
1074
+ ```ruby
1075
+ mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
1076
+ ```
1077
+
1078
+ ## I18n
1079
+
1080
+ The Active Record validations use the Rails `i18n` framework. Add these keys to
1081
+ your translations file:
1082
+
1083
+ ```yaml
1084
+ errors:
1085
+ messages:
1086
+ carrierwave_processing_error: failed to be processed
1087
+ carrierwave_integrity_error: is not of an allowed file type
1088
+ carrierwave_download_error: could not be downloaded
1089
+ extension_allowlist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
1090
+ extension_denylist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
1091
+ content_type_allowlist_error: "You are not allowed to upload %{content_type} files, allowed types: %{allowed_types}"
1092
+ content_type_denylist_error: "You are not allowed to upload %{content_type} files"
1093
+ processing_error: "Failed to manipulate, maybe it is not an image?"
1094
+ min_size_error: "File size should be greater than %{min_size}"
1095
+ max_size_error: "File size should be less than %{max_size}"
1096
+ min_width_error: "Image width should be greater than %{min_width}px"
1097
+ max_width_error: "Image width should be less than %{max_width}px"
1098
+ min_height_error: "Image height should be greater than %{min_height}px"
1099
+ max_height_error: "Image height should be less than %{max_height}px"
1100
+ ```
1101
+
1102
+ The [`carrierwave-i18n`](https://github.com/carrierwaveuploader/carrierwave-i18n)
1103
+ library adds support for additional locales.
1104
+
1105
+ ## Large files
1106
+
1107
+ By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then
1108
+ copying the file into the store. For large files, this can be prohibitively time consuming.
1109
+
1110
+ You may change this behavior by overriding either or both of the `move_to_cache` and
1111
+ `move_to_store` methods:
1112
+
1113
+ ```ruby
1114
+ class MyUploader < CarrierWave::Uploader::Base
1115
+ def move_to_cache
1116
+ true
1117
+ end
1118
+
1119
+ def move_to_store
1120
+ true
1121
+ end
1122
+ end
1123
+ ```
1124
+
1125
+ When the `move_to_cache` and/or `move_to_store` methods return true, files will be moved (instead of copied) to the cache and store respectively.
1126
+
1127
+ This has only been tested with the local filesystem store.
1128
+
1129
+ ## Skipping ActiveRecord callbacks
1130
+
1131
+ By default, mounting an uploader into an ActiveRecord model will add a few
1132
+ callbacks. For example, this code:
1133
+
1134
+ ```ruby
1135
+ class User
1136
+ mount_uploader :avatar, AvatarUploader
1137
+ end
1138
+ ```
1139
+
1140
+ Will add these callbacks:
1141
+
1142
+ ```ruby
1143
+ before_save :write_avatar_identifier
1144
+ after_save :store_previous_changes_for_avatar
1145
+ after_commit :remove_avatar!, on: :destroy
1146
+ after_commit :mark_remove_avatar_false, on: :update
1147
+ after_commit :remove_previously_stored_avatar, on: :update
1148
+ after_commit :store_avatar!, on: [:create, :update]
1149
+ ```
1150
+
1151
+ If you want to skip any of these callbacks (eg. you want to keep the existing
1152
+ avatar, even after uploading a new one), you can use ActiveRecord’s
1153
+ `skip_callback` method.
1154
+
1155
+ ```ruby
1156
+ class User
1157
+ mount_uploader :avatar, AvatarUploader
1158
+ skip_callback :commit, :after, :remove_previously_stored_avatar
1159
+ end
1160
+ ```
1161
+
1162
+ ## Uploader Callbacks
1163
+
1164
+ In addition to the ActiveRecord callbacks described above, uploaders also have callbacks.
1165
+
1166
+ ```ruby
1167
+ class MyUploader < ::CarrierWave::Uploader::Base
1168
+ before :remove, :log_removal
1169
+ private
1170
+ def log_removal
1171
+ ::Rails.logger.info(format('Deleting file on S3: %s', @file))
1172
+ end
1173
+ end
1174
+ ```
1175
+
1176
+ Uploader callbacks can be `before` or `after` the following events:
1177
+
1178
+ ```
1179
+ cache
1180
+ process
1181
+ remove
1182
+ retrieve_from_cache
1183
+ retrieve_from_store
1184
+ store
1185
+ ```
1186
+
1187
+ ## Contributing to CarrierWave
1188
+
1189
+ See [CONTRIBUTING.md](https://github.com/carrierwaveuploader/carrierwave/blob/master/CONTRIBUTING.md)
1190
+
1191
+ ## License
1192
+
1193
+ The MIT License (MIT)
1194
+
1195
+ Copyright (c) 2008 Jonas Nicklas
1196
+
1197
+ Permission is hereby granted, free of charge, to any person obtaining
1198
+ a copy of this software and associated documentation files (the
1199
+ "Software"), to deal in the Software without restriction, including
1200
+ without limitation the rights to use, copy, modify, merge, publish,
1201
+ distribute, sublicense, and/or sell copies of the Software, and to
1202
+ permit persons to whom the Software is furnished to do so, subject to
1203
+ the following conditions:
1204
+
1205
+ The above copyright notice and this permission notice shall be
1206
+ included in all copies or substantial portions of the Software.
1207
+
1208
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1209
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1210
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1211
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1212
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1213
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1214
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.