carrierwave 0.11.2 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +452 -178
  3. data/lib/carrierwave/compatibility/paperclip.rb +4 -4
  4. data/lib/carrierwave/downloader/base.rb +101 -0
  5. data/lib/carrierwave/downloader/remote_file.rb +68 -0
  6. data/lib/carrierwave/error.rb +1 -0
  7. data/lib/carrierwave/locale/en.yml +11 -5
  8. data/lib/carrierwave/mount.rb +217 -182
  9. data/lib/carrierwave/mounter.rb +255 -0
  10. data/lib/carrierwave/orm/activerecord.rb +29 -35
  11. data/lib/carrierwave/processing/mini_magick.rb +140 -84
  12. data/lib/carrierwave/processing/rmagick.rb +72 -21
  13. data/lib/carrierwave/processing/vips.rb +284 -0
  14. data/lib/carrierwave/processing.rb +1 -1
  15. data/lib/carrierwave/sanitized_file.rb +83 -84
  16. data/lib/carrierwave/storage/abstract.rb +16 -3
  17. data/lib/carrierwave/storage/file.rb +71 -3
  18. data/lib/carrierwave/storage/fog.rb +215 -57
  19. data/lib/carrierwave/storage.rb +1 -9
  20. data/lib/carrierwave/test/matchers.rb +88 -19
  21. data/lib/carrierwave/uploader/cache.rb +75 -45
  22. data/lib/carrierwave/uploader/callbacks.rb +1 -3
  23. data/lib/carrierwave/uploader/configuration.rb +80 -16
  24. data/lib/carrierwave/uploader/content_type_allowlist.rb +62 -0
  25. data/lib/carrierwave/uploader/content_type_denylist.rb +62 -0
  26. data/lib/carrierwave/uploader/default_url.rb +3 -5
  27. data/lib/carrierwave/uploader/dimension.rb +66 -0
  28. data/lib/carrierwave/uploader/download.rb +4 -74
  29. data/lib/carrierwave/uploader/extension_allowlist.rb +63 -0
  30. data/lib/carrierwave/uploader/extension_denylist.rb +64 -0
  31. data/lib/carrierwave/uploader/file_size.rb +43 -0
  32. data/lib/carrierwave/uploader/mountable.rb +13 -8
  33. data/lib/carrierwave/uploader/processing.rb +48 -13
  34. data/lib/carrierwave/uploader/proxy.rb +20 -9
  35. data/lib/carrierwave/uploader/remove.rb +0 -2
  36. data/lib/carrierwave/uploader/serialization.rb +2 -4
  37. data/lib/carrierwave/uploader/store.rb +59 -28
  38. data/lib/carrierwave/uploader/url.rb +8 -7
  39. data/lib/carrierwave/uploader/versions.rb +171 -123
  40. data/lib/carrierwave/uploader.rb +12 -10
  41. data/lib/carrierwave/utilities/file_name.rb +47 -0
  42. data/lib/carrierwave/utilities/uri.rb +14 -12
  43. data/lib/carrierwave/utilities.rb +1 -3
  44. data/lib/carrierwave/validations/active_model.rb +7 -11
  45. data/lib/carrierwave/version.rb +1 -1
  46. data/lib/carrierwave.rb +39 -21
  47. data/lib/generators/templates/{uploader.rb → uploader.rb.erb} +6 -10
  48. data/lib/generators/uploader_generator.rb +3 -3
  49. metadata +135 -83
  50. data/lib/carrierwave/locale/cs.yml +0 -11
  51. data/lib/carrierwave/locale/de.yml +0 -11
  52. data/lib/carrierwave/locale/el.yml +0 -11
  53. data/lib/carrierwave/locale/es.yml +0 -11
  54. data/lib/carrierwave/locale/fr.yml +0 -11
  55. data/lib/carrierwave/locale/ja.yml +0 -11
  56. data/lib/carrierwave/locale/nb.yml +0 -11
  57. data/lib/carrierwave/locale/nl.yml +0 -11
  58. data/lib/carrierwave/locale/pl.yml +0 -11
  59. data/lib/carrierwave/locale/pt-BR.yml +0 -11
  60. data/lib/carrierwave/locale/pt-PT.yml +0 -11
  61. data/lib/carrierwave/locale/ru.yml +0 -11
  62. data/lib/carrierwave/locale/sk.yml +0 -11
  63. data/lib/carrierwave/locale/tr.yml +0 -11
  64. data/lib/carrierwave/processing/mime_types.rb +0 -74
  65. data/lib/carrierwave/uploader/content_type_blacklist.rb +0 -48
  66. data/lib/carrierwave/uploader/content_type_whitelist.rb +0 -48
  67. data/lib/carrierwave/uploader/extension_blacklist.rb +0 -47
  68. data/lib/carrierwave/uploader/extension_whitelist.rb +0 -49
  69. data/lib/carrierwave/utilities/deprecation.rb +0 -18
@@ -0,0 +1,47 @@
1
+ module CarrierWave
2
+ module Utilities
3
+ module FileName
4
+
5
+ ##
6
+ # Returns the part of the filename before the extension. So if a file is called 'test.jpeg'
7
+ # this would return 'test'
8
+ #
9
+ # === Returns
10
+ #
11
+ # [String] the first part of the filename
12
+ #
13
+ def basename
14
+ split_extension(filename)[0] if filename
15
+ end
16
+
17
+ ##
18
+ # Returns the file extension
19
+ #
20
+ # === Returns
21
+ #
22
+ # [String] extension of file or "" if the file has no extension
23
+ #
24
+ def extension
25
+ split_extension(filename)[1] if filename
26
+ end
27
+
28
+ private
29
+
30
+ def split_extension(filename)
31
+ # regular expressions to try for identifying extensions
32
+ extension_matchers = [
33
+ /\A(.+)\.(tar\.([glx]?z|bz2))\z/, # matches "something.tar.gz"
34
+ /\A(.+)\.([^\.]+)\z/ # matches "something.jpg"
35
+ ]
36
+
37
+ extension_matchers.each do |regexp|
38
+ if filename =~ regexp
39
+ return $1, $2
40
+ end
41
+ end
42
+
43
+ [filename, ""] # In case we weren't able to split the extension
44
+ end
45
+ end # FileName
46
+ end # Utilities
47
+ end # CarrierWave
@@ -1,23 +1,25 @@
1
- # encoding: utf-8
1
+ require 'uri'
2
2
 
3
3
  module CarrierWave
4
4
  module Utilities
5
5
  module Uri
6
+ # based on Ruby < 2.0's URI.encode
7
+ PATH_SAFE = URI::REGEXP::PATTERN::UNRESERVED + '\/'
8
+ PATH_UNSAFE = Regexp.new("[^#{PATH_SAFE}]", false)
9
+ NON_ASCII = /[^[:ascii:]]/.freeze
6
10
 
7
11
  private
12
+
8
13
  def encode_path(path)
9
- # based on Ruby < 2.0's URI.encode
10
- safe_string = URI::REGEXP::PATTERN::UNRESERVED + '\/'
11
- unsafe = Regexp.new("[^#{safe_string}]", false)
14
+ URI::DEFAULT_PARSER.escape(path, PATH_UNSAFE)
15
+ end
16
+
17
+ def encode_non_ascii(str)
18
+ URI::DEFAULT_PARSER.escape(str, NON_ASCII)
19
+ end
12
20
 
13
- path.to_s.gsub(unsafe) do
14
- us = $&
15
- tmp = ''
16
- us.each_byte do |uc|
17
- tmp << sprintf('%%%02X', uc)
18
- end
19
- tmp
20
- end
21
+ def decode_uri(str)
22
+ URI::DEFAULT_PARSER.unescape(str)
21
23
  end
22
24
  end # Uri
23
25
  end # Utilities
@@ -1,7 +1,5 @@
1
- # encoding: utf-8
2
-
3
1
  require 'carrierwave/utilities/uri'
4
- require 'carrierwave/utilities/deprecation'
2
+ require 'carrierwave/utilities/file_name'
5
3
 
6
4
  module CarrierWave
7
5
  module Utilities
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'active_model/validator'
4
2
  require 'active_support/concern'
5
3
 
@@ -13,9 +11,8 @@ module CarrierWave
13
11
  class ProcessingValidator < ::ActiveModel::EachValidator
14
12
 
15
13
  def validate_each(record, attribute, value)
16
- if e = record.send("#{attribute}_processing_error")
17
- message = (e.message == e.class.to_s) ? :carrierwave_processing_error : e.message
18
- record.errors.add(attribute, message)
14
+ record.__send__("#{attribute}_processing_errors").each do |e|
15
+ record.errors.add(attribute, :carrierwave_processing_error, message: (e.message != e.class.to_s) && e.message)
19
16
  end
20
17
  end
21
18
  end
@@ -23,9 +20,8 @@ module CarrierWave
23
20
  class IntegrityValidator < ::ActiveModel::EachValidator
24
21
 
25
22
  def validate_each(record, attribute, value)
26
- if e = record.send("#{attribute}_integrity_error")
27
- message = (e.message == e.class.to_s) ? :carrierwave_integrity_error : e.message
28
- record.errors.add(attribute, message)
23
+ record.__send__("#{attribute}_integrity_errors").each do |e|
24
+ record.errors.add(attribute, :carrierwave_integrity_error, message: (e.message != e.class.to_s) && e.message)
29
25
  end
30
26
  end
31
27
  end
@@ -33,9 +29,8 @@ module CarrierWave
33
29
  class DownloadValidator < ::ActiveModel::EachValidator
34
30
 
35
31
  def validate_each(record, attribute, value)
36
- if e = record.send("#{attribute}_download_error")
37
- message = (e.message == e.class.to_s) ? :carrierwave_download_error : e.message
38
- record.errors.add(attribute, message)
32
+ record.__send__("#{attribute}_download_errors").each do |e|
33
+ record.errors.add(attribute, :carrierwave_download_error, message: (e.message != e.class.to_s) && e.message)
39
34
  end
40
35
  end
41
36
  end
@@ -60,6 +55,7 @@ module CarrierWave
60
55
  def validates_processing_of(*attr_names)
61
56
  validates_with ProcessingValidator, _merge_attributes(attr_names)
62
57
  end
58
+
63
59
  #
64
60
  ##
65
61
  # Makes the record invalid if the remote file couldn't be downloaded
@@ -1,3 +1,3 @@
1
1
  module CarrierWave
2
- VERSION = "0.11.2"
2
+ VERSION = "3.0.7".freeze
3
3
  end
data/lib/carrierwave.rb CHANGED
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
2
-
3
1
  require 'fileutils'
4
2
  require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/object/try'
5
4
  require 'active_support/core_ext/class/attribute'
6
5
  require 'active_support/concern'
7
6
 
@@ -9,6 +8,7 @@ module CarrierWave
9
8
 
10
9
  class << self
11
10
  attr_accessor :root, :base_path
11
+ attr_writer :tmp_path
12
12
 
13
13
  def configure(&block)
14
14
  CarrierWave::Uploader::Base.configure(&block)
@@ -17,26 +17,45 @@ module CarrierWave
17
17
  def clean_cached_files!(seconds=60*60*24)
18
18
  CarrierWave::Uploader::Base.clean_cached_files!(seconds)
19
19
  end
20
+
21
+ def tmp_path
22
+ @tmp_path ||= File.expand_path(File.join('..', 'tmp'), root)
23
+ end
20
24
  end
21
25
 
22
26
  end
23
27
 
24
- if defined?(Merb)
28
+ if defined?(Jets)
29
+
30
+ module CarrierWave
31
+ class Turbine < Jets::Turbine
32
+ initializer "carrierwave.setup_paths" do |app|
33
+ CarrierWave.root = Jets.root.to_s
34
+ CarrierWave.tmp_path = "/tmp/carrierwave"
35
+ CarrierWave.configure do |config|
36
+ config.cache_dir = "/tmp/carrierwave/uploads/tmp"
37
+ end
38
+ end
25
39
 
26
- CarrierWave.root = Merb.dir_for(:public)
27
- Merb::BootLoader.before_app_loads do
28
- # Setup path for uploaders and load all of them before classes are loaded
29
- Merb.push_path(:uploaders, Merb.root / 'app' / 'uploaders', '*.rb')
30
- Dir.glob(File.join(Merb.load_paths[:uploaders])).each {|f| require f }
40
+ initializer "carrierwave.active_record" do
41
+ ActiveSupport.on_load :active_record do
42
+ require 'carrierwave/orm/activerecord'
43
+ end
44
+ end
45
+ end
31
46
  end
32
47
 
33
48
  elsif defined?(Rails)
34
49
 
35
50
  module CarrierWave
36
51
  class Railtie < Rails::Railtie
37
- initializer "carrierwave.setup_paths" do
52
+ initializer "carrierwave.setup_paths" do |app|
38
53
  CarrierWave.root = Rails.root.join(Rails.public_path).to_s
39
54
  CarrierWave.base_path = ENV['RAILS_RELATIVE_URL_ROOT']
55
+ available_locales = Array(app.config.i18n.available_locales || [])
56
+ if available_locales.blank? || available_locales.include?(:en)
57
+ I18n.load_path.prepend(File.join(File.dirname(__FILE__), 'carrierwave', 'locale', "en.yml"))
58
+ end
40
59
  end
41
60
 
42
61
  initializer "carrierwave.active_record" do
@@ -45,11 +64,8 @@ elsif defined?(Rails)
45
64
  end
46
65
  end
47
66
 
48
- ##
49
- # Loads the Carrierwave locale files before the Rails application locales
50
- # letting the Rails application overrite the carrierwave locale defaults
51
- config.before_configuration do
52
- I18n.load_path << File.join(File.dirname(__FILE__), "carrierwave", "locale", 'en.yml')
67
+ config.before_eager_load do
68
+ CarrierWave::Storage::Fog.eager_load
53
69
  end
54
70
  end
55
71
  end
@@ -59,19 +75,21 @@ elsif defined?(Sinatra)
59
75
  CarrierWave.root = File.join(PADRINO_ROOT, "public")
60
76
  else
61
77
 
62
- CarrierWave.root = if Sinatra::Application.respond_to?(:public_folder)
63
- # Sinatra >= 1.3
64
- Sinatra::Application.public_folder
65
- else
66
- # Sinatra < 1.3
67
- Sinatra::Application.public
68
- end
78
+ CarrierWave.root =
79
+ if Sinatra::Application.respond_to?(:public_folder)
80
+ # Sinatra >= 1.3
81
+ Sinatra::Application.public_folder
82
+ else
83
+ # Sinatra < 1.3
84
+ Sinatra::Application.public
85
+ end
69
86
  end
70
87
  end
71
88
 
72
89
  require "carrierwave/utilities"
73
90
  require "carrierwave/error"
74
91
  require "carrierwave/sanitized_file"
92
+ require "carrierwave/mounter"
75
93
  require "carrierwave/mount"
76
94
  require "carrierwave/processing"
77
95
  require "carrierwave/version"
@@ -1,7 +1,4 @@
1
- # encoding: utf-8
2
-
3
1
  class <%= class_name %>Uploader < CarrierWave::Uploader::Base
4
-
5
2
  # Include RMagick or MiniMagick support:
6
3
  # include CarrierWave::RMagick
7
4
  # include CarrierWave::MiniMagick
@@ -17,7 +14,7 @@ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
17
14
  end
18
15
 
19
16
  # Provide a default URL as a default if there hasn't been a file uploaded:
20
- # def default_url
17
+ # def default_url(*args)
21
18
  # # For Rails 3.1+ asset pipeline compatibility:
22
19
  # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
23
20
  #
@@ -25,7 +22,7 @@ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
25
22
  # end
26
23
 
27
24
  # Process files as they are uploaded:
28
- # process :scale => [200, 300]
25
+ # process scale: [200, 300]
29
26
  #
30
27
  # def scale(width, height)
31
28
  # # do something
@@ -33,19 +30,18 @@ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
33
30
 
34
31
  # Create different versions of your uploaded files:
35
32
  # version :thumb do
36
- # process :resize_to_fit => [50, 50]
33
+ # process resize_to_fit: [50, 50]
37
34
  # end
38
35
 
39
- # Add a white list of extensions which are allowed to be uploaded.
36
+ # Add an allowlist of extensions which are allowed to be uploaded.
40
37
  # For images you might use something like this:
41
- # def extension_white_list
38
+ # def extension_allowlist
42
39
  # %w(jpg jpeg gif png)
43
40
  # end
44
41
 
45
42
  # Override the filename of the uploaded files:
46
43
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
47
44
  # def filename
48
- # "something.jpg" if original_filename
45
+ # "something.jpg"
49
46
  # end
50
-
51
47
  end
@@ -1,7 +1,7 @@
1
1
  class UploaderGenerator < Rails::Generators::NamedBase
2
- source_root File.expand_path("../templates", __FILE__)
2
+ source_root File.expand_path('templates', __dir__)
3
3
 
4
4
  def create_uploader_file
5
- template "uploader.rb", File.join('app/uploaders', class_path, "#{file_name}_uploader.rb")
5
+ template "uploader.rb.erb", File.join('app/uploaders', class_path, "#{file_name}_uploader.rb")
6
6
  end
7
- end
7
+ end