carrierwave 1.0.0 → 2.0.0
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.
- checksums.yaml +5 -5
- data/README.md +95 -34
- data/lib/carrierwave/downloader/base.rb +50 -0
- data/lib/carrierwave/downloader/remote_file.rb +42 -0
- data/lib/carrierwave/locale/en.yml +1 -1
- data/lib/carrierwave/mount.rb +29 -21
- data/lib/carrierwave/mounter.rb +70 -47
- data/lib/carrierwave/orm/activerecord.rb +18 -12
- data/lib/carrierwave/processing/mini_magick.rb +102 -99
- data/lib/carrierwave/processing/rmagick.rb +24 -3
- data/lib/carrierwave/sanitized_file.rb +39 -24
- data/lib/carrierwave/storage/file.rb +7 -3
- data/lib/carrierwave/storage/fog.rb +77 -19
- data/lib/carrierwave/storage.rb +1 -0
- data/lib/carrierwave/uploader/cache.rb +24 -16
- data/lib/carrierwave/uploader/configuration.rb +32 -16
- data/lib/carrierwave/uploader/content_type_whitelist.rb +1 -1
- data/lib/carrierwave/uploader/download.rb +2 -75
- data/lib/carrierwave/uploader/extension_whitelist.rb +2 -1
- data/lib/carrierwave/uploader/file_size.rb +4 -2
- data/lib/carrierwave/uploader/mountable.rb +6 -0
- data/lib/carrierwave/uploader/processing.rb +9 -7
- data/lib/carrierwave/uploader/proxy.rb +2 -2
- data/lib/carrierwave/uploader/serialization.rb +1 -1
- data/lib/carrierwave/uploader/store.rb +13 -2
- data/lib/carrierwave/uploader/url.rb +2 -2
- data/lib/carrierwave/uploader/versions.rb +50 -13
- data/lib/carrierwave/utilities/uri.rb +2 -0
- data/lib/carrierwave/validations/active_model.rb +3 -3
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +28 -0
- data/lib/generators/templates/uploader.rb +1 -3
- metadata +103 -34
- data/lib/carrierwave/uploader/magic_mime_blacklist.rb +0 -94
- data/lib/carrierwave/uploader/magic_mime_whitelist.rb +0 -94
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 85a5c93a317b317dcf6bf2a6f6ae520ddc9a6788df8552aa7339b85e2e86f8ba
|
|
4
|
+
data.tar.gz: d8eae4edcbbbabda5ad0ae70d5e36a425677a34dc2e1b014ca4f3641ba2914c4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0f6a3f1bbbc1aa7137383756b45d2d337254253932b70f2f1cf68440c7bcb37e2607650e58d38174d6487f31f1d7a0ddce1736971396e6107101193d5e84049a
|
|
7
|
+
data.tar.gz: b2b6bc7e114fcc5f2194cbff2e03048ea669e9cfea1631f36bcf196566f70580da8d2fcc82c4a92b8497b1841073ae19629a61f8635573ceb32bb0430586bab8
|
data/README.md
CHANGED
|
@@ -4,8 +4,8 @@ This gem provides a simple and extremely flexible way to upload files from Ruby
|
|
|
4
4
|
It works well with Rack based web applications, such as Ruby on Rails.
|
|
5
5
|
|
|
6
6
|
[](http://travis-ci.org/carrierwaveuploader/carrierwave)
|
|
7
|
-
[](https://codeclimate.com/github/carrierwaveuploader/carrierwave)
|
|
8
|
+
[](https://dependabot.com/compatibility-score.html?dependency-name=carrierwave&package-manager=bundler&version-scheme=semver)
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
## Information
|
|
@@ -16,7 +16,7 @@ It works well with Rack based web applications, such as Ruby on Rails.
|
|
|
16
16
|
|
|
17
17
|
## Getting Help
|
|
18
18
|
|
|
19
|
-
* Please ask the community on [Stack Overflow](
|
|
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
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
21
|
|
|
22
22
|
## Installation
|
|
@@ -24,19 +24,19 @@ It works well with Rack based web applications, such as Ruby on Rails.
|
|
|
24
24
|
Install the latest release:
|
|
25
25
|
|
|
26
26
|
```
|
|
27
|
-
$ gem install carrierwave
|
|
27
|
+
$ gem install carrierwave
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
In Rails, add it to your Gemfile:
|
|
31
31
|
|
|
32
32
|
```ruby
|
|
33
|
-
gem 'carrierwave', '~>
|
|
33
|
+
gem 'carrierwave', '~> 2.0'
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
Finally, restart the server to apply the changes.
|
|
37
37
|
|
|
38
|
-
As of version
|
|
39
|
-
or higher. If you're on Rails
|
|
38
|
+
As of version 2.0, CarrierWave requires Rails 5.0 or higher and Ruby 2.2
|
|
39
|
+
or higher. If you're on Rails 4, you should use 1.x.
|
|
40
40
|
|
|
41
41
|
## Getting Started
|
|
42
42
|
|
|
@@ -89,7 +89,7 @@ a migration:
|
|
|
89
89
|
|
|
90
90
|
|
|
91
91
|
rails g migration add_avatar_to_users avatar:string
|
|
92
|
-
|
|
92
|
+
rails db:migrate
|
|
93
93
|
|
|
94
94
|
Open your model file and mount the uploader:
|
|
95
95
|
|
|
@@ -131,9 +131,6 @@ Other ORM support has been extracted into separate gems:
|
|
|
131
131
|
There are more extensions listed in [the wiki](https://github.com/carrierwaveuploader/carrierwave/wiki)
|
|
132
132
|
|
|
133
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
134
|
|
|
138
135
|
CarrierWave also has convenient support for multiple file upload fields.
|
|
139
136
|
|
|
@@ -144,17 +141,31 @@ column for example. Your choice depends on what your database supports. For
|
|
|
144
141
|
example, create a migration like this:
|
|
145
142
|
|
|
146
143
|
|
|
144
|
+
#### For databases with ActiveRecord json data type support (e.g. PostgreSQL, MySQL)
|
|
145
|
+
|
|
147
146
|
rails g migration add_avatars_to_users avatars:json
|
|
148
|
-
|
|
147
|
+
rails db:migrate
|
|
148
|
+
|
|
149
|
+
#### For database without ActiveRecord json data type support (e.g. SQLite)
|
|
150
|
+
|
|
151
|
+
rails g migration add_avatars_to_users avatars:string
|
|
152
|
+
rails db:migrate
|
|
153
|
+
|
|
154
|
+
__Note__: JSON datatype doesn't exists in SQLite adapter, that's why you can use a string datatype which will be serialized in model.
|
|
149
155
|
|
|
150
156
|
Open your model file and mount the uploader:
|
|
151
157
|
|
|
158
|
+
|
|
152
159
|
```ruby
|
|
153
160
|
class User < ActiveRecord::Base
|
|
154
161
|
mount_uploaders :avatars, AvatarUploader
|
|
162
|
+
serialize :avatars, JSON # If you use SQLite, add this line.
|
|
155
163
|
end
|
|
156
164
|
```
|
|
157
165
|
|
|
166
|
+
Make sure that you mount the uploader with write (mount_uploaders) with `s` not (mount_uploader)
|
|
167
|
+
in order to avoid errors when uploading multiple files
|
|
168
|
+
|
|
158
169
|
Make sure your file input fields are set up as multiple file fields. For
|
|
159
170
|
example in Rails you'll want to do something like this:
|
|
160
171
|
|
|
@@ -179,6 +190,17 @@ u.avatars[0].current_path # => 'path/to/file.png'
|
|
|
179
190
|
u.avatars[0].identifier # => 'file.png'
|
|
180
191
|
```
|
|
181
192
|
|
|
193
|
+
If you want to preserve existing files on uploading new one, you can go like:
|
|
194
|
+
|
|
195
|
+
```erb
|
|
196
|
+
<% user.avatars.each do |avatar| %>
|
|
197
|
+
<%= hidden_field :user, :avatars, multiple: true, value: avatar.identifier %>
|
|
198
|
+
<% end %>
|
|
199
|
+
<%= form.file_field :avatars, multiple: true %>
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
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).
|
|
203
|
+
|
|
182
204
|
## Changing the storage directory
|
|
183
205
|
|
|
184
206
|
In order to change where uploaded files are put, just override the `store_dir`
|
|
@@ -244,6 +266,22 @@ class NoJsonUploader < CarrierWave::Uploader::Base
|
|
|
244
266
|
end
|
|
245
267
|
```
|
|
246
268
|
|
|
269
|
+
### CVE-2016-3714 (ImageTragick)
|
|
270
|
+
This version of CarrierWave has the ability to mitigate CVE-2016-3714. However, you **MUST** set a content_type_whitelist 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.
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
A valid whitelist that will restrict your uploader to images only, and mitigate the CVE is:
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
class MyUploader < CarrierWave::Uploader::Base
|
|
277
|
+
def content_type_whitelist
|
|
278
|
+
[/image\//]
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
**WARNING**: A `content_type_whitelist` is the only form of whitelist or blacklist supported by CarrierWave that can effectively mitigate against CVE-2016-3714. Use of `extension_whitelist` will not inspect the file headers, and thus still leaves your application open to the vulnerability.
|
|
284
|
+
|
|
247
285
|
### Filenames and unicode chars
|
|
248
286
|
|
|
249
287
|
Another security issue you should care for is the file names (see
|
|
@@ -269,7 +307,7 @@ You no longer need to do this manually.
|
|
|
269
307
|
|
|
270
308
|
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*:
|
|
271
309
|
|
|
272
|
-
*Note:* You must have Imagemagick
|
|
310
|
+
*Note:* You must have Imagemagick installed to do image resizing.
|
|
273
311
|
|
|
274
312
|
Some documentation refers to RMagick instead of MiniMagick but MiniMagick is recommended.
|
|
275
313
|
|
|
@@ -293,14 +331,22 @@ end
|
|
|
293
331
|
```
|
|
294
332
|
|
|
295
333
|
When this uploader is used, an uploaded image would be scaled to be no larger
|
|
296
|
-
than 800 by 800 pixels.
|
|
297
|
-
|
|
334
|
+
than 800 by 800 pixels. The original aspect ratio will be kept.
|
|
335
|
+
A version called thumb is then created, which is scaled
|
|
336
|
+
to exactly 200 by 200 pixels.
|
|
337
|
+
|
|
338
|
+
If you would like to crop images to a specific height and width you
|
|
339
|
+
can use the alternative option of '''resize_to_fill'''. It will make sure
|
|
340
|
+
that the width and height specified are filled, only cropping
|
|
341
|
+
if the aspect ratio requires it.
|
|
342
|
+
|
|
343
|
+
The uploader could be used like this:
|
|
298
344
|
|
|
299
345
|
```ruby
|
|
300
346
|
uploader = AvatarUploader.new
|
|
301
347
|
uploader.store!(my_file) # size: 1024x768
|
|
302
348
|
|
|
303
|
-
uploader.url # => '/url/to/my_file.png' # size:
|
|
349
|
+
uploader.url # => '/url/to/my_file.png' # size: 800x800
|
|
304
350
|
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
|
|
305
351
|
```
|
|
306
352
|
|
|
@@ -343,7 +389,7 @@ private
|
|
|
343
389
|
end
|
|
344
390
|
|
|
345
391
|
def is_landscape? picture
|
|
346
|
-
image = MiniMagick::Image.
|
|
392
|
+
image = MiniMagick::Image.new(picture.path)
|
|
347
393
|
image[:width] > image[:height]
|
|
348
394
|
end
|
|
349
395
|
|
|
@@ -617,6 +663,8 @@ describe MyUploader do
|
|
|
617
663
|
end
|
|
618
664
|
```
|
|
619
665
|
|
|
666
|
+
If you're looking for minitest asserts, checkout [carrierwave_asserts](https://github.com/hcfairbanks/carrierwave_asserts).
|
|
667
|
+
|
|
620
668
|
Setting the enable_processing flag on an uploader will prevent any of the versions from processing as well.
|
|
621
669
|
Processing can be enabled for a single version by setting the processing flag on the version like so:
|
|
622
670
|
|
|
@@ -630,7 +678,6 @@ If you want to use fog you must add in your CarrierWave initializer the
|
|
|
630
678
|
following lines
|
|
631
679
|
|
|
632
680
|
```ruby
|
|
633
|
-
config.fog_provider = 'fog' # 'fog/aws' etc. Defaults to 'fog'
|
|
634
681
|
config.fog_credentials = { ... } # Provider specific credentials
|
|
635
682
|
```
|
|
636
683
|
|
|
@@ -648,18 +695,18 @@ You can also pass in additional options, as documented fully in lib/carrierwave/
|
|
|
648
695
|
|
|
649
696
|
```ruby
|
|
650
697
|
CarrierWave.configure do |config|
|
|
651
|
-
config.fog_provider = 'fog/aws' # required
|
|
652
698
|
config.fog_credentials = {
|
|
653
699
|
provider: 'AWS', # required
|
|
654
|
-
aws_access_key_id: 'xxx', # required
|
|
655
|
-
aws_secret_access_key: 'yyy', # required
|
|
700
|
+
aws_access_key_id: 'xxx', # required unless using use_iam_profile
|
|
701
|
+
aws_secret_access_key: 'yyy', # required unless using use_iam_profile
|
|
702
|
+
use_iam_profile: true, # optional, defaults to false
|
|
656
703
|
region: 'eu-west-1', # optional, defaults to 'us-east-1'
|
|
657
704
|
host: 's3.example.com', # optional, defaults to nil
|
|
658
705
|
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
|
|
659
706
|
}
|
|
660
|
-
config.fog_directory = '
|
|
661
|
-
config.fog_public = false
|
|
662
|
-
config.fog_attributes = {
|
|
707
|
+
config.fog_directory = 'name_of_bucket' # required
|
|
708
|
+
config.fog_public = false # optional, defaults to true
|
|
709
|
+
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
|
|
663
710
|
end
|
|
664
711
|
```
|
|
665
712
|
|
|
@@ -673,6 +720,14 @@ end
|
|
|
673
720
|
|
|
674
721
|
That's it! You can still use the `CarrierWave::Uploader#url` method to return the url to the file on Amazon S3.
|
|
675
722
|
|
|
723
|
+
**Note**: for Carrierwave to work properly it needs credentials with the following permissions:
|
|
724
|
+
|
|
725
|
+
* `s3:ListBucket`
|
|
726
|
+
* `s3:PutObject`
|
|
727
|
+
* `s3:GetObject`
|
|
728
|
+
* `s3:DeleteObject`
|
|
729
|
+
* `s3:PutObjectAcl`
|
|
730
|
+
|
|
676
731
|
## Using Rackspace Cloud Files
|
|
677
732
|
|
|
678
733
|
[Fog](http://github.com/fog/fog) is used to support Rackspace Cloud Files. Ensure you have it in your Gemfile:
|
|
@@ -688,7 +743,6 @@ Using a US-based account:
|
|
|
688
743
|
|
|
689
744
|
```ruby
|
|
690
745
|
CarrierWave.configure do |config|
|
|
691
|
-
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
|
692
746
|
config.fog_credentials = {
|
|
693
747
|
provider: 'Rackspace',
|
|
694
748
|
rackspace_username: 'xxxxxx',
|
|
@@ -703,7 +757,6 @@ Using a UK-based account:
|
|
|
703
757
|
|
|
704
758
|
```ruby
|
|
705
759
|
CarrierWave.configure do |config|
|
|
706
|
-
config.fog_provider = "fog/rackspace/storage" # optional, defaults to "fog"
|
|
707
760
|
config.fog_credentials = {
|
|
708
761
|
provider: 'Rackspace',
|
|
709
762
|
rackspace_username: 'xxxxxx',
|
|
@@ -740,7 +793,7 @@ the url to the file on Rackspace Cloud Files.
|
|
|
740
793
|
|
|
741
794
|
```ruby
|
|
742
795
|
gem "fog-google"
|
|
743
|
-
gem "google-api-client", "
|
|
796
|
+
gem "google-api-client", "> 0.8.5", "< 0.9"
|
|
744
797
|
gem "mime-types"
|
|
745
798
|
```
|
|
746
799
|
|
|
@@ -752,7 +805,6 @@ Please read the [fog-google README](https://github.com/fog/fog-google/blob/maste
|
|
|
752
805
|
|
|
753
806
|
```ruby
|
|
754
807
|
CarrierWave.configure do |config|
|
|
755
|
-
config.fog_provider = 'fog/google' # required
|
|
756
808
|
config.fog_credentials = {
|
|
757
809
|
provider: 'Google',
|
|
758
810
|
google_storage_access_key_id: 'xxxxxx',
|
|
@@ -849,8 +901,8 @@ manipulation methods.
|
|
|
849
901
|
|
|
850
902
|
## Using MiniMagick
|
|
851
903
|
|
|
852
|
-
MiniMagick is similar to RMagick but performs all the operations using the '
|
|
853
|
-
|
|
904
|
+
MiniMagick is similar to RMagick but performs all the operations using the 'convert'
|
|
905
|
+
CLI which is part of the standard ImageMagick kit. This allows you to have the power
|
|
854
906
|
of ImageMagick without having to worry about installing all the RMagick libraries.
|
|
855
907
|
|
|
856
908
|
See the MiniMagick site for more details:
|
|
@@ -893,19 +945,28 @@ mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
|
|
|
893
945
|
|
|
894
946
|
## I18n
|
|
895
947
|
|
|
896
|
-
The Active Record validations use the Rails i18n framework. Add these keys to
|
|
948
|
+
The Active Record validations use the Rails `i18n` framework. Add these keys to
|
|
897
949
|
your translations file:
|
|
898
950
|
|
|
899
951
|
```yaml
|
|
900
952
|
errors:
|
|
901
953
|
messages:
|
|
902
|
-
carrierwave_processing_error:
|
|
903
|
-
carrierwave_integrity_error:
|
|
904
|
-
carrierwave_download_error:
|
|
954
|
+
carrierwave_processing_error: failed to be processed
|
|
955
|
+
carrierwave_integrity_error: is not of an allowed file type
|
|
956
|
+
carrierwave_download_error: could not be downloaded
|
|
905
957
|
extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
|
|
906
958
|
extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
|
|
959
|
+
content_type_whitelist_error: "You are not allowed to upload %{content_type} files, allowed types: %{allowed_types}"
|
|
960
|
+
content_type_blacklist_error: "You are not allowed to upload %{content_type} files"
|
|
961
|
+
rmagick_processing_error: "Failed to manipulate with rmagick, maybe it is not an image?"
|
|
962
|
+
mini_magick_processing_error: "Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
|
|
963
|
+
min_size_error: "File size should be greater than %{min_size}"
|
|
964
|
+
max_size_error: "File size should be less than %{max_size}"
|
|
907
965
|
```
|
|
908
966
|
|
|
967
|
+
The [`carrierwave-i18n`](https://github.com/carrierwaveuploader/carrierwave-i18n)
|
|
968
|
+
library adds support for additional locales.
|
|
969
|
+
|
|
909
970
|
## Large files
|
|
910
971
|
|
|
911
972
|
By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'open-uri'
|
|
2
|
+
require 'addressable'
|
|
3
|
+
require 'carrierwave/downloader/remote_file'
|
|
4
|
+
|
|
5
|
+
module CarrierWave
|
|
6
|
+
module Downloader
|
|
7
|
+
class Base
|
|
8
|
+
attr_reader :uploader
|
|
9
|
+
|
|
10
|
+
def initialize(uploader)
|
|
11
|
+
@uploader = uploader
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
##
|
|
15
|
+
# Downloads a file from given URL and returns a RemoteFile.
|
|
16
|
+
#
|
|
17
|
+
# === Parameters
|
|
18
|
+
#
|
|
19
|
+
# [url (String)] The URL where the remote file is stored
|
|
20
|
+
# [remote_headers (Hash)] Request headers
|
|
21
|
+
#
|
|
22
|
+
def download(url, remote_headers = {})
|
|
23
|
+
headers = remote_headers.
|
|
24
|
+
reverse_merge('User-Agent' => "CarrierWave/#{CarrierWave::VERSION}")
|
|
25
|
+
begin
|
|
26
|
+
file = OpenURI.open_uri(process_uri(url.to_s), headers)
|
|
27
|
+
rescue StandardError => e
|
|
28
|
+
raise CarrierWave::DownloadError, "could not download file: #{e.message}"
|
|
29
|
+
end
|
|
30
|
+
CarrierWave::Downloader::RemoteFile.new(file)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
##
|
|
34
|
+
# Processes the given URL by parsing and escaping it. Public to allow overriding.
|
|
35
|
+
#
|
|
36
|
+
# === Parameters
|
|
37
|
+
#
|
|
38
|
+
# [url (String)] The URL where the remote file is stored
|
|
39
|
+
#
|
|
40
|
+
def process_uri(uri)
|
|
41
|
+
uri_parts = uri.split('?')
|
|
42
|
+
encoded_uri = Addressable::URI.parse(uri_parts.shift).normalize.to_s
|
|
43
|
+
encoded_uri << '?' << URI.encode(uri_parts.join('?')) if uri_parts.any?
|
|
44
|
+
URI.parse(encoded_uri)
|
|
45
|
+
rescue URI::InvalidURIError, Addressable::URI::InvalidURIError
|
|
46
|
+
raise CarrierWave::DownloadError, "couldn't parse URL: #{uri}"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module CarrierWave
|
|
2
|
+
module Downloader
|
|
3
|
+
class RemoteFile
|
|
4
|
+
attr_reader :file
|
|
5
|
+
|
|
6
|
+
def initialize(file)
|
|
7
|
+
@file = file.is_a?(String) ? StringIO.new(file) : file
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def original_filename
|
|
11
|
+
filename = filename_from_header || filename_from_uri
|
|
12
|
+
mime_type = MiniMime.lookup_by_content_type(file.content_type)
|
|
13
|
+
unless File.extname(filename).present? || mime_type.blank?
|
|
14
|
+
filename = "#{filename}.#{mime_type.extension}"
|
|
15
|
+
end
|
|
16
|
+
filename
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def respond_to?(*args)
|
|
20
|
+
super || file.respond_to?(*args)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def filename_from_header
|
|
26
|
+
if file.meta.include? 'content-disposition'
|
|
27
|
+
match = file.meta['content-disposition'].match(/filename=(?:"([^"]+)"|([^";]+))/)
|
|
28
|
+
match[1].presence || match[2].presence
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def filename_from_uri
|
|
33
|
+
CGI.unescape(File.basename(file.base_uri.path))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def method_missing(*args, &block)
|
|
37
|
+
file.send(*args, &block)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
@@ -6,7 +6,7 @@ en:
|
|
|
6
6
|
carrierwave_download_error: could not be downloaded
|
|
7
7
|
extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
|
|
8
8
|
extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
|
|
9
|
-
content_type_whitelist_error: "You are not allowed to upload %{content_type} files"
|
|
9
|
+
content_type_whitelist_error: "You are not allowed to upload %{content_type} files, allowed types: %{allowed_types}"
|
|
10
10
|
content_type_blacklist_error: "You are not allowed to upload %{content_type} files"
|
|
11
11
|
rmagick_processing_error: "Failed to manipulate with rmagick, maybe it is not an image?"
|
|
12
12
|
mini_magick_processing_error: "Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
|
data/lib/carrierwave/mount.rb
CHANGED
|
@@ -174,19 +174,29 @@ module CarrierWave
|
|
|
174
174
|
return if frozen?
|
|
175
175
|
mounter = _mounter(:#{column})
|
|
176
176
|
|
|
177
|
-
if mounter.remove?
|
|
178
|
-
|
|
179
|
-
elsif mounter.identifiers.first
|
|
180
|
-
write_uploader(mounter.serialization_column, mounter.identifiers.first)
|
|
181
|
-
end
|
|
177
|
+
mounter.clear! if mounter.remove?
|
|
178
|
+
write_uploader(mounter.serialization_column, mounter.identifiers.first)
|
|
182
179
|
end
|
|
183
180
|
|
|
184
181
|
def #{column}_identifier
|
|
185
182
|
_mounter(:#{column}).read_identifiers[0]
|
|
186
183
|
end
|
|
187
184
|
|
|
185
|
+
def #{column}_integrity_error
|
|
186
|
+
#{column}_integrity_errors.last
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def #{column}_processing_error
|
|
190
|
+
#{column}_processing_errors.last
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def #{column}_download_error
|
|
194
|
+
#{column}_download_errors.last
|
|
195
|
+
end
|
|
196
|
+
|
|
188
197
|
def store_previous_changes_for_#{column}
|
|
189
|
-
|
|
198
|
+
attribute_changes = ::ActiveRecord.version.to_s.to_f >= 5.1 ? saved_changes : changes
|
|
199
|
+
@_previous_changes_for_#{column} = attribute_changes[_mounter(:#{column}).serialization_column]
|
|
190
200
|
end
|
|
191
201
|
|
|
192
202
|
def remove_previously_stored_#{column}
|
|
@@ -239,9 +249,9 @@ module CarrierWave
|
|
|
239
249
|
# [store_images!] Stores all files that have been assigned with +images=+
|
|
240
250
|
# [remove_images!] Removes the uploaded file from the filesystem.
|
|
241
251
|
#
|
|
242
|
-
# [
|
|
243
|
-
# [
|
|
244
|
-
# [
|
|
252
|
+
# [image_integrity_errors] Returns error objects of files which failed to pass integrity check
|
|
253
|
+
# [image_processing_errors] Returns error objects of files which failed to be processed
|
|
254
|
+
# [image_download_errors] Returns error objects of files which failed to be downloaded
|
|
245
255
|
#
|
|
246
256
|
# [image_identifiers] Reads out the identifiers of the files
|
|
247
257
|
#
|
|
@@ -328,11 +338,8 @@ module CarrierWave
|
|
|
328
338
|
return if frozen?
|
|
329
339
|
mounter = _mounter(:#{column})
|
|
330
340
|
|
|
331
|
-
if mounter.remove?
|
|
332
|
-
|
|
333
|
-
elsif mounter.identifiers.any?
|
|
334
|
-
write_uploader(mounter.serialization_column, mounter.identifiers)
|
|
335
|
-
end
|
|
341
|
+
mounter.clear! if mounter.remove?
|
|
342
|
+
write_uploader(mounter.serialization_column, mounter.identifiers.presence)
|
|
336
343
|
end
|
|
337
344
|
|
|
338
345
|
def #{column}_identifiers
|
|
@@ -340,7 +347,8 @@ module CarrierWave
|
|
|
340
347
|
end
|
|
341
348
|
|
|
342
349
|
def store_previous_changes_for_#{column}
|
|
343
|
-
|
|
350
|
+
attribute_changes = ::ActiveRecord.version.to_s.to_f >= 5.1 ? saved_changes : changes
|
|
351
|
+
@_previous_changes_for_#{column} = attribute_changes[_mounter(:#{column}).serialization_column]
|
|
344
352
|
end
|
|
345
353
|
|
|
346
354
|
def remove_previously_stored_#{column}
|
|
@@ -393,16 +401,16 @@ module CarrierWave
|
|
|
393
401
|
_mounter(:#{column}).store!
|
|
394
402
|
end
|
|
395
403
|
|
|
396
|
-
def #{column}
|
|
397
|
-
_mounter(:#{column}).
|
|
404
|
+
def #{column}_integrity_errors
|
|
405
|
+
_mounter(:#{column}).integrity_errors
|
|
398
406
|
end
|
|
399
407
|
|
|
400
|
-
def #{column}
|
|
401
|
-
_mounter(:#{column}).
|
|
408
|
+
def #{column}_processing_errors
|
|
409
|
+
_mounter(:#{column}).processing_errors
|
|
402
410
|
end
|
|
403
411
|
|
|
404
|
-
def #{column}
|
|
405
|
-
_mounter(:#{column}).
|
|
412
|
+
def #{column}_download_errors
|
|
413
|
+
_mounter(:#{column}).download_errors
|
|
406
414
|
end
|
|
407
415
|
|
|
408
416
|
def mark_remove_#{column}_false
|