carrierwave 0.11.2 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of carrierwave might be problematic. Click here for more details.

Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +220 -116
  3. data/lib/carrierwave.rb +8 -10
  4. data/lib/carrierwave/compatibility/paperclip.rb +0 -2
  5. data/lib/carrierwave/error.rb +1 -0
  6. data/lib/carrierwave/locale/en.yml +7 -4
  7. data/lib/carrierwave/mount.rb +209 -176
  8. data/lib/carrierwave/mounter.rb +163 -0
  9. data/lib/carrierwave/orm/activerecord.rb +49 -20
  10. data/lib/carrierwave/processing.rb +0 -1
  11. data/lib/carrierwave/processing/mini_magick.rb +64 -13
  12. data/lib/carrierwave/processing/rmagick.rb +31 -3
  13. data/lib/carrierwave/sanitized_file.rb +43 -38
  14. data/lib/carrierwave/storage.rb +0 -9
  15. data/lib/carrierwave/storage/abstract.rb +15 -2
  16. data/lib/carrierwave/storage/file.rb +64 -2
  17. data/lib/carrierwave/storage/fog.rb +100 -27
  18. data/lib/carrierwave/test/matchers.rb +77 -12
  19. data/lib/carrierwave/uploader.rb +2 -2
  20. data/lib/carrierwave/uploader/cache.rb +40 -26
  21. data/lib/carrierwave/uploader/callbacks.rb +0 -2
  22. data/lib/carrierwave/uploader/configuration.rb +48 -6
  23. data/lib/carrierwave/uploader/default_url.rb +3 -5
  24. data/lib/carrierwave/uploader/download.rb +2 -4
  25. data/lib/carrierwave/uploader/extension_blacklist.rb +14 -10
  26. data/lib/carrierwave/uploader/extension_whitelist.rb +12 -10
  27. data/lib/carrierwave/uploader/file_size.rb +41 -0
  28. data/lib/carrierwave/uploader/magic_mime_blacklist.rb +94 -0
  29. data/lib/carrierwave/uploader/magic_mime_whitelist.rb +94 -0
  30. data/lib/carrierwave/uploader/mountable.rb +7 -8
  31. data/lib/carrierwave/uploader/processing.rb +1 -3
  32. data/lib/carrierwave/uploader/proxy.rb +5 -7
  33. data/lib/carrierwave/uploader/remove.rb +0 -2
  34. data/lib/carrierwave/uploader/serialization.rb +1 -3
  35. data/lib/carrierwave/uploader/store.rb +5 -23
  36. data/lib/carrierwave/uploader/url.rb +1 -3
  37. data/lib/carrierwave/uploader/versions.rb +75 -82
  38. data/lib/carrierwave/utilities.rb +0 -3
  39. data/lib/carrierwave/utilities/uri.rb +4 -7
  40. data/lib/carrierwave/validations/active_model.rb +0 -2
  41. data/lib/carrierwave/version.rb +1 -1
  42. data/lib/generators/templates/uploader.rb +3 -5
  43. metadata +43 -97
  44. data/lib/carrierwave/locale/cs.yml +0 -11
  45. data/lib/carrierwave/locale/de.yml +0 -11
  46. data/lib/carrierwave/locale/el.yml +0 -11
  47. data/lib/carrierwave/locale/es.yml +0 -11
  48. data/lib/carrierwave/locale/fr.yml +0 -11
  49. data/lib/carrierwave/locale/ja.yml +0 -11
  50. data/lib/carrierwave/locale/nb.yml +0 -11
  51. data/lib/carrierwave/locale/nl.yml +0 -11
  52. data/lib/carrierwave/locale/pl.yml +0 -11
  53. data/lib/carrierwave/locale/pt-BR.yml +0 -11
  54. data/lib/carrierwave/locale/pt-PT.yml +0 -11
  55. data/lib/carrierwave/locale/ru.yml +0 -11
  56. data/lib/carrierwave/locale/sk.yml +0 -11
  57. data/lib/carrierwave/locale/tr.yml +0 -11
  58. data/lib/carrierwave/processing/mime_types.rb +0 -74
  59. data/lib/carrierwave/utilities/deprecation.rb +0 -18
@@ -1,19 +1,17 @@
1
- # encoding: utf-8
2
-
3
1
  module CarrierWave
4
2
  module Uploader
5
3
  module DefaultUrl
6
4
 
7
5
  def url(*args)
8
- super || default_url
6
+ super || default_url(*args)
9
7
  end
10
8
 
11
9
  ##
12
10
  # Override this method in your uploader to provide a default url
13
11
  # in case no file has been cached/stored yet.
14
12
  #
15
- def default_url; end
13
+ def default_url(*args); end
16
14
 
17
15
  end # DefaultPath
18
16
  end # Uploader
19
- end # CarrierWave
17
+ end # CarrierWave
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'open-uri'
4
2
 
5
3
  module CarrierWave
@@ -37,12 +35,12 @@ module CarrierWave
37
35
 
38
36
  def file
39
37
  if @file.blank?
40
- @file = Kernel.open(@uri.to_s)
38
+ @file = Kernel.open(@uri.to_s, "User-Agent" => "CarrierWave/#{CarrierWave::VERSION}")
41
39
  @file = @file.is_a?(String) ? StringIO.new(@file) : @file
42
40
  end
43
41
  @file
44
42
 
45
- rescue Exception => e
43
+ rescue StandardError => e
46
44
  raise CarrierWave::DownloadError, "could not download file: #{e.message}"
47
45
  end
48
46
 
@@ -4,7 +4,7 @@ module CarrierWave
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- before :cache, :check_blacklist!
7
+ before :cache, :check_extension_blacklist!
8
8
  end
9
9
 
10
10
  ##
@@ -16,32 +16,36 @@ module CarrierWave
16
16
  # the Regexp expression, also case insensitive.
17
17
  #
18
18
  # === Returns
19
-
20
- # [NilClass, Array[String,Regexp]] a black list of extensions which are prohibited to be uploaded
19
+
20
+ # [NilClass, String, Regexp, Array[String, Regexp]] a black list of extensions which are prohibited to be uploaded
21
21
  #
22
22
  # === Examples
23
23
  #
24
- # def extension_black_list
24
+ # def extension_blacklist
25
25
  # %w(swf tiff)
26
26
  # end
27
27
  #
28
28
  # Basically the same, but using a Regexp:
29
29
  #
30
- # def extension_black_list
30
+ # def extension_blacklist
31
31
  # [/swf/, 'tiff']
32
32
  # end
33
33
  #
34
-
35
- def extension_black_list; end
34
+
35
+ def extension_blacklist; end
36
36
 
37
37
  private
38
38
 
39
- def check_blacklist!(new_file)
39
+ def check_extension_blacklist!(new_file)
40
40
  extension = new_file.extension.to_s
41
- if extension_black_list and extension_black_list.detect { |item| extension =~ /\A#{item}\z/i }
42
- raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_black_list_error", :extension => new_file.extension.inspect, :prohibited_types => extension_black_list.join(", "))
41
+ if extension_blacklist && blacklisted_extension?(extension)
42
+ raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_blacklist_error", extension: new_file.extension.inspect, prohibited_types: Array(extension_blacklist).join(", "))
43
43
  end
44
44
  end
45
+
46
+ def blacklisted_extension?(extension)
47
+ Array(extension_blacklist).any? { |item| extension =~ /\A#{item}\z/i }
48
+ end
45
49
  end
46
50
  end
47
51
  end
@@ -1,12 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  module CarrierWave
4
2
  module Uploader
5
3
  module ExtensionWhitelist
6
4
  extend ActiveSupport::Concern
7
5
 
8
6
  included do
9
- before :cache, :check_whitelist!
7
+ before :cache, :check_extension_whitelist!
10
8
  end
11
9
 
12
10
  ##
@@ -19,31 +17,35 @@ module CarrierWave
19
17
  #
20
18
  # === Returns
21
19
  #
22
- # [NilClass, Array[String,Regexp]] a white list of extensions which are allowed to be uploaded
20
+ # [NilClass, String, Regexp, Array[String, Regexp]] a white list of extensions which are allowed to be uploaded
23
21
  #
24
22
  # === Examples
25
23
  #
26
- # def extension_white_list
24
+ # def extension_whitelist
27
25
  # %w(jpg jpeg gif png)
28
26
  # end
29
27
  #
30
28
  # Basically the same, but using a Regexp:
31
29
  #
32
- # def extension_white_list
30
+ # def extension_whitelist
33
31
  # [/jpe?g/, 'gif', 'png']
34
32
  # end
35
33
  #
36
- def extension_white_list; end
34
+ def extension_whitelist; end
37
35
 
38
36
  private
39
37
 
40
- def check_whitelist!(new_file)
38
+ def check_extension_whitelist!(new_file)
41
39
  extension = new_file.extension.to_s
42
- if extension_white_list and not extension_white_list.detect { |item| extension =~ /\A#{item}\z/i }
43
- raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_white_list_error", :extension => new_file.extension.inspect, :allowed_types => extension_white_list.join(", "))
40
+ if extension_whitelist && !whitelisted_extension?(extension)
41
+ raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_whitelist_error", extension: new_file.extension.inspect, allowed_types: Array(extension_whitelist).join(", "))
44
42
  end
45
43
  end
46
44
 
45
+ def whitelisted_extension?(extension)
46
+ Array(extension_whitelist).any? { |item| extension =~ /\A#{item}\z/i }
47
+ end
48
+
47
49
  end # ExtensionWhitelist
48
50
  end # Uploader
49
51
  end # CarrierWave
@@ -0,0 +1,41 @@
1
+ module CarrierWave
2
+ module Uploader
3
+ module FileSize
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before :cache, :check_size!
8
+ end
9
+
10
+ ##
11
+ # Override this method in your uploader to provide a Range of Size which
12
+ # are allowed to be uploaded.
13
+ # === Returns
14
+ #
15
+ # [NilClass, Range] a size range which are permitted to be uploaded
16
+ #
17
+ # === Examples
18
+ #
19
+ # def size_range
20
+ # 3256...5748
21
+ # end
22
+ #
23
+ def size_range; end
24
+
25
+ private
26
+
27
+ def check_size!(new_file)
28
+ size = new_file.size
29
+ expected_size_range = size_range
30
+ if expected_size_range.is_a?(::Range)
31
+ if size < expected_size_range.min
32
+ raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.min_size_error", :min_size => expected_size_range.min)
33
+ elsif size > expected_size_range.max
34
+ raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.max_size_error", :max_size => expected_size_range.max)
35
+ end
36
+ end
37
+ end
38
+
39
+ end # FileSize
40
+ end # Uploader
41
+ end # CarrierWave
@@ -0,0 +1,94 @@
1
+ module CarrierWave
2
+ module Uploader
3
+
4
+ ##
5
+ # This modules validates the content type of a file with the use of
6
+ # ruby-filemagic gem and a blacklist regular expression. If you want
7
+ # to use this, you'll need to require this file:
8
+ #
9
+ # require 'carrierwave/uploader/magic_mime_blacklist'
10
+ #
11
+ # And then include it in your uploader:
12
+ #
13
+ # class MyUploader < CarrierWave::Uploader::Base
14
+ # include CarrierWave::Uploader::MagicMimeBlacklist
15
+ #
16
+ # def blacklist_mime_type_pattern
17
+ # /image\//
18
+ # end
19
+ # end
20
+ #
21
+ module MagicMimeBlacklist
22
+ extend ActiveSupport::Concern
23
+
24
+ included do
25
+ begin
26
+ require "filemagic"
27
+ rescue LoadError => e
28
+ e.message << " (You may need to install the ruby-filemagic gem)"
29
+ raise e
30
+ end
31
+
32
+ before :cache, :check_blacklist_pattern!
33
+ end
34
+
35
+ ##
36
+ # Override this method in your uploader to provide a black list pattern (regexp)
37
+ # of content-types which are prohibited to be uploaded.
38
+ # Compares the file's content-type.
39
+ #
40
+ # === Returns
41
+ #
42
+ # [Regexp] a black list regexp to match the content_type
43
+ #
44
+ # === Examples
45
+ #
46
+ # def blacklist_mime_type_pattern
47
+ # /(text|application)\/json/
48
+ # end
49
+ #
50
+ def blacklist_mime_type_pattern; end
51
+
52
+ private
53
+
54
+ def check_blacklist_pattern!(new_file)
55
+ return if blacklist_mime_type_pattern.nil?
56
+
57
+ content_type = extract_content_type(new_file)
58
+
59
+ if content_type.match(blacklist_mime_type_pattern)
60
+ raise CarrierWave::IntegrityError,
61
+ I18n.translate(:"errors.messages.mime_type_pattern_black_list_error",
62
+ :content_type => content_type)
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Extracts the content type of the given file
68
+ #
69
+ # === Returns
70
+ #
71
+ # [String] the extracted content type
72
+ #
73
+ def extract_content_type(new_file)
74
+ content_type = nil
75
+
76
+ File.open(new_file.path) do |fd|
77
+ data = fd.read(1024) || ""
78
+ content_type = filemagic.buffer(data)
79
+ end
80
+
81
+ content_type
82
+ end
83
+
84
+ ##
85
+ # FileMagic object with the MAGIC_MIME_TYPE flag set
86
+ #
87
+ # @return [FileMagic] a filemagic object
88
+ def filemagic
89
+ @filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME_TYPE)
90
+ end
91
+
92
+ end # MagicMimeblackList
93
+ end # Uploader
94
+ end # CarrierWave
@@ -0,0 +1,94 @@
1
+ module CarrierWave
2
+ module Uploader
3
+
4
+ ##
5
+ # This modules validates the content type of a file with the use of
6
+ # ruby-filemagic gem and a whitelist regular expression. If you want
7
+ # to use this, you'll need to require this file:
8
+ #
9
+ # require 'carrierwave/uploader/magic_mime_whitelist'
10
+ #
11
+ # And then include it in your uploader:
12
+ #
13
+ # class MyUploader < CarrierWave::Uploader::Base
14
+ # include CarrierWave::Uploader::MagicMimeWhitelist
15
+ #
16
+ # def whitelist_mime_type_pattern
17
+ # /image\//
18
+ # end
19
+ # end
20
+ #
21
+ module MagicMimeWhitelist
22
+ extend ActiveSupport::Concern
23
+
24
+ included do
25
+ begin
26
+ require "filemagic"
27
+ rescue LoadError => e
28
+ e.message << " (You may need to install the ruby-filemagic gem)"
29
+ raise e
30
+ end
31
+
32
+ before :cache, :check_whitelist_pattern!
33
+ end
34
+
35
+ ##
36
+ # Override this method in your uploader to provide a white list pattern (regexp)
37
+ # of content-types which are allowed to be uploaded.
38
+ # Compares the file's content-type.
39
+ #
40
+ # === Returns
41
+ #
42
+ # [Regexp] a white list regexp to match the content_type
43
+ #
44
+ # === Examples
45
+ #
46
+ # def whitelist_mime_type_pattern
47
+ # /(text|application)\/json/
48
+ # end
49
+ #
50
+ def whitelist_mime_type_pattern; end
51
+
52
+ private
53
+
54
+ def check_whitelist_pattern!(new_file)
55
+ return if whitelist_mime_type_pattern.nil?
56
+
57
+ content_type = extract_content_type(new_file)
58
+
59
+ if !content_type.match(whitelist_mime_type_pattern)
60
+ raise CarrierWave::IntegrityError,
61
+ I18n.translate(:"errors.messages.mime_type_pattern_white_list_error",
62
+ :content_type => content_type)
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Extracts the content type of the given file
68
+ #
69
+ # === Returns
70
+ #
71
+ # [String] the extracted content type
72
+ #
73
+ def extract_content_type(new_file)
74
+ content_type = nil
75
+
76
+ File.open(new_file.path) do |fd|
77
+ data = fd.read(1024) || ""
78
+ content_type = filemagic.buffer(data)
79
+ end
80
+
81
+ content_type
82
+ end
83
+
84
+ ##
85
+ # FileMagic object with the MAGIC_MIME_TYPE flag set
86
+ #
87
+ # @return [FileMagic] a filemagic object
88
+ def filemagic
89
+ @filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME_TYPE)
90
+ end
91
+
92
+ end # MagicMimeWhiteList
93
+ end # Uploader
94
+ end # CarrierWave
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module CarrierWave
4
2
  module Uploader
5
3
  module Mountable
@@ -7,13 +5,14 @@ module CarrierWave
7
5
  attr_reader :model, :mounted_as
8
6
 
9
7
  ##
10
- # If a model is given as the first parameter, it will be stored in the uploader, and
11
- # available throught +#model+. Likewise, mounted_as stores the name of the column
12
- # where this instance of the uploader is mounted. These values can then be used inside
13
- # your uploader.
8
+ # If a model is given as the first parameter, it will be stored in the
9
+ # uploader, and available through +#model+. Likewise, mounted_as stores
10
+ # the name of the column where this instance of the uploader is mounted.
11
+ # These values can then be used inside your uploader.
14
12
  #
15
- # If you do not wish to mount your uploaders with the ORM extensions in -more then you
16
- # can override this method inside your uploader. Just be sure to call +super+
13
+ # If you do not wish to mount your uploaders with the ORM extensions in
14
+ # -more then you can override this method inside your uploader. Just be
15
+ # sure to call +super+
17
16
  #
18
17
  # === Parameters
19
18
  #
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module CarrierWave
4
2
  module Uploader
5
3
  module Processing
@@ -11,7 +9,7 @@ module CarrierWave
11
9
  class_attribute :processors, :instance_writer => false
12
10
  self.processors = []
13
11
 
14
- after :cache, :process!
12
+ before :cache, :process!
15
13
  end
16
14
 
17
15
  module ClassMethods
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  module CarrierWave
4
2
  module Uploader
5
3
  module Proxy
@@ -19,7 +17,7 @@ module CarrierWave
19
17
  # [String] the path where the file is currently located.
20
18
  #
21
19
  def current_path
22
- file.path if file.respond_to?(:path)
20
+ file.try(:path)
23
21
  end
24
22
 
25
23
  alias_method :path, :current_path
@@ -32,7 +30,7 @@ module CarrierWave
32
30
  # [String] uniquely identifies a file
33
31
  #
34
32
  def identifier
35
- storage.identifier if storage.respond_to?(:identifier)
33
+ storage.try(:identifier)
36
34
  end
37
35
 
38
36
  ##
@@ -43,7 +41,7 @@ module CarrierWave
43
41
  # [String] contents of the file
44
42
  #
45
43
  def read
46
- file.read if file.respond_to?(:read)
44
+ file.try(:read)
47
45
  end
48
46
 
49
47
  ##
@@ -54,7 +52,7 @@ module CarrierWave
54
52
  # [Integer] size of the file
55
53
  #
56
54
  def size
57
- file.respond_to?(:size) ? file.size : 0
55
+ file.try(:size) || 0
58
56
  end
59
57
 
60
58
  ##
@@ -80,7 +78,7 @@ module CarrierWave
80
78
  # [String] content type of the file
81
79
  #
82
80
  def content_type
83
- file.respond_to?(:content_type) ? file.content_type : nil
81
+ file.try(:content_type)
84
82
  end
85
83
 
86
84
  end # Proxy