carrierwave 1.1.0 → 1.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 637cc1f96fe4f41cc4468837e5f3ea358e80ed6e
4
- data.tar.gz: 7e160616b11d78c39bee758a90412dbadb588c04
2
+ SHA256:
3
+ metadata.gz: d5e6d1dd656203f5e14c43b75b807e793d623126006ddf8c5a4f9f4706faa750
4
+ data.tar.gz: 82281b939837f54716f580a1deb6397d87cc9bb867dc6a6938a4322d795500b1
5
5
  SHA512:
6
- metadata.gz: 9757ab08f11fee70c9337f706dce60d20f12200890ed1816f6eedc0fb2939e282c9290d724ccaa592f3c78bf91f5f643b3af0bd27240bd7ced5132068af1e45e
7
- data.tar.gz: 768e9c140fba9bd1f3dd299cdf83b94cc4348a1888801793a638d6a20220c2eff152916a6066bcb0b586f96080039e7594c13c89b352d4711699502110772293
6
+ metadata.gz: 8eca3a53204f520d0cabf6e2384e6670cb634159e4b6c1e8d3915b6bab8473f21fb116047129b142a986588ea27401131d095186657770d7c31b03d6c929a1c9
7
+ data.tar.gz: 316797cffad6d2568e50929862cd80ba3e56c05d557f70f8631b66fc60ffd65fe5862afe4121a1e5e775e55a5f6c9a6c14414bce6a57deab2d12a8a127ed5b0d
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
  [![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)
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
9
 
10
10
 
11
11
  ## Information
@@ -24,7 +24,7 @@ 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 -v "1.0.0"
27
+ $ gem install carrierwave
28
28
  ```
29
29
 
30
30
  In Rails, add it to your Gemfile:
@@ -89,7 +89,7 @@ a migration:
89
89
 
90
90
 
91
91
  rails g migration add_avatar_to_users avatar:string
92
- rake db:migrate
92
+ rails db:migrate
93
93
 
94
94
  Open your model file and mount the uploader:
95
95
 
@@ -144,12 +144,12 @@ example, create a migration like this:
144
144
  #### For databases with ActiveRecord json data type support (e.g. PostgreSQL, MySQL)
145
145
 
146
146
  rails g migration add_avatars_to_users avatars:json
147
- rake db:migrate
147
+ rails db:migrate
148
148
 
149
149
  #### For database without ActiveRecord json data type support (e.g. SQLite)
150
150
 
151
151
  rails g migration add_avatars_to_users avatars:string
152
- rake db:migrate
152
+ rails db:migrate
153
153
 
154
154
  __Note__: JSON datatype doesn't exists in SQLite adapter, that's why you can use a string datatype which will be serialized in model.
155
155
 
@@ -163,6 +163,9 @@ class User < ActiveRecord::Base
163
163
  end
164
164
  ```
165
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
+
166
169
  Make sure your file input fields are set up as multiple file fields. For
167
170
  example in Rails you'll want to do something like this:
168
171
 
@@ -301,14 +304,22 @@ end
301
304
  ```
302
305
 
303
306
  When this uploader is used, an uploaded image would be scaled to be no larger
304
- than 800 by 800 pixels. A version called thumb is then created, which is scaled
305
- and cropped to exactly 200 by 200 pixels. The uploader could be used like this:
307
+ than 800 by 800 pixels. The original aspect ratio will be kept.
308
+ A version called thumb is then created, which is scaled
309
+ to exactly 200 by 200 pixels.
310
+
311
+ If you would like to crop images to a specific height and width you
312
+ can use the alternative option of '''resize_to_fill'''. It will make sure
313
+ that the width and height specified are filled, only cropping
314
+ if the aspect ratio requires it.
315
+
316
+ The uploader could be used like this:
306
317
 
307
318
  ```ruby
308
319
  uploader = AvatarUploader.new
309
320
  uploader.store!(my_file) # size: 1024x768
310
321
 
311
- uploader.url # => '/url/to/my_file.png' # size: 800x600
322
+ uploader.url # => '/url/to/my_file.png' # size: 800x800
312
323
  uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
313
324
  ```
314
325
 
@@ -625,6 +636,8 @@ describe MyUploader do
625
636
  end
626
637
  ```
627
638
 
639
+ If you're looking for minitest asserts, checkout [carrierwave_asserts](https://github.com/hcfairbanks/carrierwave_asserts).
640
+
628
641
  Setting the enable_processing flag on an uploader will prevent any of the versions from processing as well.
629
642
  Processing can be enabled for a single version by setting the processing flag on the version like so:
630
643
 
@@ -659,15 +672,16 @@ CarrierWave.configure do |config|
659
672
  config.fog_provider = 'fog/aws' # required
660
673
  config.fog_credentials = {
661
674
  provider: 'AWS', # required
662
- aws_access_key_id: 'xxx', # required
663
- aws_secret_access_key: 'yyy', # required
675
+ aws_access_key_id: 'xxx', # required unless using use_iam_profile
676
+ aws_secret_access_key: 'yyy', # required unless using use_iam_profile
677
+ use_iam_profile: true, # optional, defaults to false
664
678
  region: 'eu-west-1', # optional, defaults to 'us-east-1'
665
679
  host: 's3.example.com', # optional, defaults to nil
666
680
  endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
667
681
  }
668
- config.fog_directory = 'name_of_directory' # required
669
- config.fog_public = false # optional, defaults to true
670
- config.fog_attributes = { cache_control: "public, max-age=#{365.day.to_i}" } # optional, defaults to {}
682
+ config.fog_directory = 'name_of_bucket' # required
683
+ config.fog_public = false # optional, defaults to true
684
+ config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
671
685
  end
672
686
  ```
673
687
 
@@ -901,19 +915,28 @@ mount_uploader :avatar, AvatarUploader, mount_on: :avatar_file_name
901
915
 
902
916
  ## I18n
903
917
 
904
- The Active Record validations use the Rails i18n framework. Add these keys to
918
+ The Active Record validations use the Rails `i18n` framework. Add these keys to
905
919
  your translations file:
906
920
 
907
921
  ```yaml
908
922
  errors:
909
923
  messages:
910
- carrierwave_processing_error: "Cannot resize image."
911
- carrierwave_integrity_error: "Not an image."
912
- carrierwave_download_error: "Couldn't download image."
924
+ carrierwave_processing_error: failed to be processed
925
+ carrierwave_integrity_error: is not of an allowed file type
926
+ carrierwave_download_error: could not be downloaded
913
927
  extension_whitelist_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
914
928
  extension_blacklist_error: "You are not allowed to upload %{extension} files, prohibited types: %{prohibited_types}"
929
+ content_type_whitelist_error: "You are not allowed to upload %{content_type} files, allowed types: %{allowed_types}"
930
+ content_type_blacklist_error: "You are not allowed to upload %{content_type} files"
931
+ rmagick_processing_error: "Failed to manipulate with rmagick, maybe it is not an image?"
932
+ mini_magick_processing_error: "Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
933
+ min_size_error: "File size should be greater than %{min_size}"
934
+ max_size_error: "File size should be less than %{max_size}"
915
935
  ```
916
936
 
937
+ The [`carrierwave-i18n`](https://github.com/carrierwaveuploader/carrierwave-i18n)
938
+ library adds support for additional locales.
939
+
917
940
  ## Large files
918
941
 
919
942
  By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then
data/lib/carrierwave.rb CHANGED
@@ -34,6 +34,26 @@ if defined?(Merb)
34
34
  Dir.glob(File.join(Merb.load_paths[:uploaders])).each {|f| require f }
35
35
  end
36
36
 
37
+ elsif defined?(Jets)
38
+
39
+ module CarrierWave
40
+ class Turbine < Jets::Turbine
41
+ initializer "carrierwave.setup_paths" do |app|
42
+ CarrierWave.root = Jets.root.to_s
43
+ CarrierWave.tmp_path = "/tmp/carrierwave"
44
+ CarrierWave.configure do |config|
45
+ config.cache_dir = "/tmp/carrierwave/uploads/tmp"
46
+ end
47
+ end
48
+
49
+ initializer "carrierwave.active_record" do
50
+ ActiveSupport.on_load :active_record do
51
+ require 'carrierwave/orm/activerecord'
52
+ end
53
+ end
54
+ end
55
+ end
56
+
37
57
  elsif defined?(Rails)
38
58
 
39
59
  module CarrierWave
@@ -41,6 +61,10 @@ elsif defined?(Rails)
41
61
  initializer "carrierwave.setup_paths" do |app|
42
62
  CarrierWave.root = Rails.root.join(Rails.public_path).to_s
43
63
  CarrierWave.base_path = ENV['RAILS_RELATIVE_URL_ROOT']
64
+ available_locales = Array(app.config.i18n.available_locales || [])
65
+ if available_locales.blank? || available_locales.include?(:en)
66
+ I18n.load_path.prepend(File.join(File.dirname(__FILE__), 'carrierwave', 'locale', "en.yml"))
67
+ end
44
68
  end
45
69
 
46
70
  initializer "carrierwave.active_record" do
@@ -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}"
@@ -15,7 +15,7 @@ module CarrierWave
15
15
  class_eval <<-RUBY, __FILE__, __LINE__+1
16
16
  def remote_#{column}_url=(url)
17
17
  column = _mounter(:#{column}).serialization_column
18
- send(:"\#{column}_will_change!")
18
+ __send__(:"\#{column}_will_change!")
19
19
  super
20
20
  end
21
21
  RUBY
@@ -30,7 +30,7 @@ module CarrierWave
30
30
  class_eval <<-RUBY, __FILE__, __LINE__+1
31
31
  def remote_#{column}_urls=(url)
32
32
  column = _mounter(:#{column}).serialization_column
33
- send(:"\#{column}_will_change!")
33
+ __send__(:"\#{column}_will_change!")
34
34
  super
35
35
  end
36
36
  RUBY
@@ -63,8 +63,8 @@ module CarrierWave
63
63
  class_eval <<-RUBY, __FILE__, __LINE__+1
64
64
  def #{column}=(new_file)
65
65
  column = _mounter(:#{column}).serialization_column
66
- if !(new_file.blank? && send(:#{column}).blank?)
67
- send(:"\#{column}_will_change!")
66
+ if !(new_file.blank? && __send__(:#{column}).blank?)
67
+ __send__(:"\#{column}_will_change!")
68
68
  end
69
69
 
70
70
  super
@@ -72,7 +72,7 @@ module CarrierWave
72
72
 
73
73
  def remove_#{column}=(value)
74
74
  column = _mounter(:#{column}).serialization_column
75
- send(:"\#{column}_will_change!")
75
+ __send__(:"\#{column}_will_change!")
76
76
  super
77
77
  end
78
78
 
@@ -61,6 +61,13 @@ module CarrierWave
61
61
  e.message << " (You may need to install the mini_magick gem)"
62
62
  raise e
63
63
  end
64
+
65
+ prepend Module.new {
66
+ def initialize(*)
67
+ super
68
+ @format = nil
69
+ end
70
+ }
64
71
  end
65
72
 
66
73
  module ClassMethods
@@ -127,6 +134,8 @@ module CarrierWave
127
134
  # [MiniMagick::Image] additional manipulations to perform
128
135
  #
129
136
  def resize_to_limit(width, height, combine_options: {})
137
+ width = dimension_from width
138
+ height = dimension_from height
130
139
  manipulate! do |img|
131
140
  img.combine_options do |cmd|
132
141
  cmd.resize "#{width}x#{height}>"
@@ -152,6 +161,8 @@ module CarrierWave
152
161
  # [MiniMagick::Image] additional manipulations to perform
153
162
  #
154
163
  def resize_to_fit(width, height, combine_options: {})
164
+ width = dimension_from width
165
+ height = dimension_from height
155
166
  manipulate! do |img|
156
167
  img.combine_options do |cmd|
157
168
  cmd.resize "#{width}x#{height}"
@@ -178,6 +189,8 @@ module CarrierWave
178
189
  # [MiniMagick::Image] additional manipulations to perform
179
190
  #
180
191
  def resize_to_fill(width, height, gravity = 'Center', combine_options: {})
192
+ width = dimension_from width
193
+ height = dimension_from height
181
194
  manipulate! do |img|
182
195
  cols, rows = img[:dimensions]
183
196
  img.combine_options do |cmd|
@@ -225,6 +238,8 @@ module CarrierWave
225
238
  # [MiniMagick::Image] additional manipulations to perform
226
239
  #
227
240
  def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {})
241
+ width = dimension_from width
242
+ height = dimension_from height
228
243
  manipulate! do |img|
229
244
  img.combine_options do |cmd|
230
245
  cmd.thumbnail "#{width}x#{height}>"
@@ -295,6 +310,7 @@ module CarrierWave
295
310
 
296
311
  if @format
297
312
  move_to = current_path.chomp(File.extname(current_path)) + ".#{@format}"
313
+ file.content_type = ::MIME::Types.type_for(move_to).first.to_s
298
314
  file.move_to(move_to, permissions, directory_permissions)
299
315
  end
300
316
 
@@ -319,12 +335,13 @@ module CarrierWave
319
335
  end
320
336
  end
321
337
 
338
+ def dimension_from(value)
339
+ return value unless value.instance_of?(Proc)
340
+ value.arity >= 1 ? value.call(self) : value.call
341
+ end
342
+
322
343
  def mini_magick_image
323
- if url
324
- ::MiniMagick::Image.open(url)
325
- else
326
- ::MiniMagick::Image.open(current_path)
327
- end
344
+ ::MiniMagick::Image.read(read)
328
345
  end
329
346
 
330
347
  end # MiniMagick
@@ -67,6 +67,13 @@ module CarrierWave
67
67
  e.message << " (You may need to install the rmagick gem)"
68
68
  raise e
69
69
  end
70
+
71
+ prepend Module.new {
72
+ def initialize(*)
73
+ super
74
+ @format = nil
75
+ end
76
+ }
70
77
  end
71
78
 
72
79
  module ClassMethods
@@ -133,6 +140,8 @@ module CarrierWave
133
140
  # [Magick::Image] additional manipulations to perform
134
141
  #
135
142
  def resize_to_limit(width, height)
143
+ width = dimension_from width
144
+ height = dimension_from height
136
145
  manipulate! do |img|
137
146
  geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
138
147
  new_img = img.change_geometry(geometry) do |new_width, new_height|
@@ -162,6 +171,8 @@ module CarrierWave
162
171
  # [Magick::Image] additional manipulations to perform
163
172
  #
164
173
  def resize_to_fit(width, height)
174
+ width = dimension_from width
175
+ height = dimension_from height
165
176
  manipulate! do |img|
166
177
  img.resize_to_fit!(width, height)
167
178
  img = yield(img) if block_given?
@@ -186,6 +197,8 @@ module CarrierWave
186
197
  # [Magick::Image] additional manipulations to perform
187
198
  #
188
199
  def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
200
+ width = dimension_from width
201
+ height = dimension_from height
189
202
  manipulate! do |img|
190
203
  img.crop_resized!(width, height, gravity)
191
204
  img = yield(img) if block_given?
@@ -211,6 +224,8 @@ module CarrierWave
211
224
  # [Magick::Image] additional manipulations to perform
212
225
  #
213
226
  def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
227
+ width = dimension_from width
228
+ height = dimension_from height
214
229
  manipulate! do |img|
215
230
  img.resize_to_fit!(width, height)
216
231
  new_img = ::Magick::Image.new(width, height) { self.background_color = background == :transparent ? 'rgba(255,255,255,0)' : background.to_s }
@@ -338,7 +353,7 @@ module CarrierWave
338
353
  frames = ::Magick::ImageList.new
339
354
 
340
355
  image.each_with_index do |frame, index|
341
- frame = yield *[frame, index, options].take(block.arity) if block_given?
356
+ frame = yield(*[frame, index, options].take(block.arity)) if block_given?
342
357
  frames << frame if frame
343
358
  end
344
359
  frames.append(true) if block_given?
@@ -348,6 +363,7 @@ module CarrierWave
348
363
  if options[:format] || @format
349
364
  frames.write("#{options[:format] || @format}:#{current_path}", &write_block)
350
365
  move_to = current_path.chomp(File.extname(current_path)) + ".#{options[:format] || @format}"
366
+ file.content_type = ::MIME::Types.type_for(move_to).first.to_s
351
367
  file.move_to(move_to, permissions, directory_permissions)
352
368
  else
353
369
  frames.write(current_path, &write_block)
@@ -362,17 +378,28 @@ module CarrierWave
362
378
 
363
379
  def create_info_block(options)
364
380
  return nil unless options
365
- assignments = options.map { |k, v| "self.#{k} = #{v}" }
366
- code = "lambda { |img| " + assignments.join(";") + "}"
367
- eval code
381
+ proc do |img|
382
+ options.each do |k, v|
383
+ if v.is_a?(String) && (matches = v.match(/^["'](.+)["']/))
384
+ ActiveSupport::Deprecation.warn "Passing quoted strings like #{v} to #manipulate! is deprecated, pass them without quoting."
385
+ v = matches[1]
386
+ end
387
+ img.public_send(:"#{k}=", v)
388
+ end
389
+ end
368
390
  end
369
391
 
370
392
  def destroy_image(image)
371
393
  image.try(:destroy!)
372
394
  end
373
395
 
396
+ def dimension_from(value)
397
+ return value unless value.instance_of?(Proc)
398
+ value.arity >= 1 ? value.call(self) : value.call
399
+ end
400
+
374
401
  def rmagick_image
375
- ::Magick::Image.read(current_path).first
402
+ ::Magick::Image.from_blob(self.read).first
376
403
  end
377
404
 
378
405
  end # RMagick
@@ -20,7 +20,7 @@ module CarrierWave
20
20
  #
21
21
  class SanitizedFile
22
22
 
23
- attr_accessor :file
23
+ attr_reader :file
24
24
 
25
25
  class << self
26
26
  attr_writer :sanitize_regexp
@@ -32,6 +32,7 @@ module CarrierWave
32
32
 
33
33
  def initialize(file)
34
34
  self.file = file
35
+ @content = nil
35
36
  end
36
37
 
37
38
  ##
@@ -113,12 +114,11 @@ module CarrierWave
113
114
  # [String, nil] the path where the file is located.
114
115
  #
115
116
  def path
116
- unless @file.blank?
117
- if is_path?
118
- File.expand_path(@file)
119
- elsif @file.respond_to?(:path) and not @file.path.blank?
120
- File.expand_path(@file.path)
121
- end
117
+ return if @file.blank?
118
+ if is_path?
119
+ File.expand_path(@file)
120
+ elsif @file.respond_to?(:path) && !@file.path.blank?
121
+ File.expand_path(@file.path)
122
122
  end
123
123
  end
124
124
 
@@ -197,7 +197,7 @@ module CarrierWave
197
197
  #
198
198
  def move!(new_path)
199
199
  if exists?
200
- FileUtils.mv(path, new_path) unless new_path == path
200
+ FileUtils.mv(path, new_path) unless File.identical?(new_path, path)
201
201
  else
202
202
  File.open(new_path, "wb") { |f| f.write(read) }
203
203
  end
@@ -312,7 +312,7 @@ module CarrierWave
312
312
  def mkdir!(path, directory_permissions)
313
313
  options = {}
314
314
  options[:mode] = directory_permissions if directory_permissions
315
- FileUtils.mkdir_p(File.dirname(path), options) unless File.exist?(File.dirname(path))
315
+ FileUtils.mkdir_p(File.dirname(path), **options) unless File.exist?(File.dirname(path))
316
316
  end
317
317
 
318
318
  def chmod!(path, permissions)