carrierwave 0.11.2 → 1.1.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.
Potentially problematic release.
This version of carrierwave might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +223 -116
- data/lib/carrierwave/compatibility/paperclip.rb +0 -2
- data/lib/carrierwave/error.rb +1 -0
- data/lib/carrierwave/locale/en.yml +7 -4
- data/lib/carrierwave/mount.rb +219 -176
- data/lib/carrierwave/mounter.rb +165 -0
- data/lib/carrierwave/orm/activerecord.rb +49 -20
- data/lib/carrierwave/processing/mini_magick.rb +69 -15
- data/lib/carrierwave/processing/rmagick.rb +32 -4
- data/lib/carrierwave/processing.rb +0 -1
- data/lib/carrierwave/sanitized_file.rb +43 -38
- data/lib/carrierwave/storage/abstract.rb +15 -2
- data/lib/carrierwave/storage/file.rb +65 -2
- data/lib/carrierwave/storage/fog.rb +105 -28
- data/lib/carrierwave/storage.rb +0 -9
- data/lib/carrierwave/test/matchers.rb +77 -12
- data/lib/carrierwave/uploader/cache.rb +41 -27
- data/lib/carrierwave/uploader/callbacks.rb +0 -2
- data/lib/carrierwave/uploader/configuration.rb +50 -7
- data/lib/carrierwave/uploader/default_url.rb +3 -5
- data/lib/carrierwave/uploader/download.rb +10 -7
- data/lib/carrierwave/uploader/extension_blacklist.rb +14 -10
- data/lib/carrierwave/uploader/extension_whitelist.rb +12 -10
- data/lib/carrierwave/uploader/file_size.rb +41 -0
- data/lib/carrierwave/uploader/magic_mime_blacklist.rb +94 -0
- data/lib/carrierwave/uploader/magic_mime_whitelist.rb +94 -0
- data/lib/carrierwave/uploader/mountable.rb +7 -8
- data/lib/carrierwave/uploader/processing.rb +10 -10
- data/lib/carrierwave/uploader/proxy.rb +5 -7
- data/lib/carrierwave/uploader/remove.rb +0 -2
- data/lib/carrierwave/uploader/serialization.rb +1 -3
- data/lib/carrierwave/uploader/store.rb +5 -23
- data/lib/carrierwave/uploader/url.rb +3 -5
- data/lib/carrierwave/uploader/versions.rb +75 -82
- data/lib/carrierwave/uploader.rb +2 -2
- data/lib/carrierwave/utilities/uri.rb +4 -7
- data/lib/carrierwave/utilities.rb +0 -3
- data/lib/carrierwave/validations/active_model.rb +0 -2
- data/lib/carrierwave/version.rb +1 -1
- data/lib/carrierwave.rb +8 -10
- data/lib/generators/templates/uploader.rb +4 -6
- metadata +41 -95
- data/lib/carrierwave/locale/cs.yml +0 -11
- data/lib/carrierwave/locale/de.yml +0 -11
- data/lib/carrierwave/locale/el.yml +0 -11
- data/lib/carrierwave/locale/es.yml +0 -11
- data/lib/carrierwave/locale/fr.yml +0 -11
- data/lib/carrierwave/locale/ja.yml +0 -11
- data/lib/carrierwave/locale/nb.yml +0 -11
- data/lib/carrierwave/locale/nl.yml +0 -11
- data/lib/carrierwave/locale/pl.yml +0 -11
- data/lib/carrierwave/locale/pt-BR.yml +0 -11
- data/lib/carrierwave/locale/pt-PT.yml +0 -11
- data/lib/carrierwave/locale/ru.yml +0 -11
- data/lib/carrierwave/locale/sk.yml +0 -11
- data/lib/carrierwave/locale/tr.yml +0 -11
- data/lib/carrierwave/processing/mime_types.rb +0 -74
- data/lib/carrierwave/utilities/deprecation.rb +0 -18
@@ -5,7 +5,7 @@ module CarrierWave
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
included do
|
8
|
-
class_attribute :_storage, :instance_writer => false
|
8
|
+
class_attribute :_storage, :_cache_storage, :instance_writer => false
|
9
9
|
|
10
10
|
add_config :root
|
11
11
|
add_config :base_path
|
@@ -23,12 +23,14 @@ module CarrierWave
|
|
23
23
|
add_config :remove_previously_stored_files_after_update
|
24
24
|
|
25
25
|
# fog
|
26
|
+
add_config :fog_provider
|
26
27
|
add_config :fog_attributes
|
27
28
|
add_config :fog_credentials
|
28
29
|
add_config :fog_directory
|
29
30
|
add_config :fog_public
|
30
31
|
add_config :fog_authenticated_url_expiration
|
31
32
|
add_config :fog_use_ssl_for_aws
|
33
|
+
add_config :fog_aws_accelerate
|
32
34
|
|
33
35
|
# Mounting
|
34
36
|
add_config :ignore_integrity_errors
|
@@ -38,6 +40,7 @@ module CarrierWave
|
|
38
40
|
add_config :validate_processing
|
39
41
|
add_config :validate_download
|
40
42
|
add_config :mount_on
|
43
|
+
add_config :cache_only
|
41
44
|
|
42
45
|
# set default values
|
43
46
|
reset_config
|
@@ -69,17 +72,55 @@ module CarrierWave
|
|
69
72
|
# storage MyCustomStorageEngine
|
70
73
|
#
|
71
74
|
def storage(storage = nil)
|
72
|
-
|
73
|
-
|
75
|
+
case storage
|
76
|
+
when Symbol
|
77
|
+
if storage_engine = storage_engines[storage]
|
78
|
+
self._storage = eval storage_engine
|
79
|
+
else
|
80
|
+
raise CarrierWave::UnknownStorageError, "Unknown storage: #{storage}"
|
81
|
+
end
|
82
|
+
when nil
|
83
|
+
storage
|
84
|
+
else
|
85
|
+
self._storage = storage
|
74
86
|
end
|
75
87
|
_storage
|
76
88
|
end
|
77
89
|
alias_method :storage=, :storage
|
78
90
|
|
91
|
+
##
|
92
|
+
# Sets the cache storage engine to be used when storing cache files with this uploader.
|
93
|
+
# Same as .storage except for required methods being #cache!(CarrierWave::SanitizedFile),
|
94
|
+
# #retrieve_from_cache! and #delete_dir!.
|
95
|
+
#
|
96
|
+
# === Parameters
|
97
|
+
#
|
98
|
+
# [storage (Symbol, Class)] The cache storage engine to use for this uploader
|
99
|
+
#
|
100
|
+
# === Returns
|
101
|
+
#
|
102
|
+
# [Class] the cache storage engine to be used with this uploader
|
103
|
+
#
|
104
|
+
# === Examples
|
105
|
+
#
|
106
|
+
# cache_storage :file
|
107
|
+
# cache_storage CarrierWave::Storage::File
|
108
|
+
# cache_storage MyCustomStorageEngine
|
109
|
+
#
|
110
|
+
def cache_storage(storage = nil)
|
111
|
+
if storage
|
112
|
+
self._cache_storage = storage.is_a?(Symbol) ? eval(storage_engines[storage]) : storage
|
113
|
+
end
|
114
|
+
_cache_storage
|
115
|
+
end
|
116
|
+
alias_method :cache_storage=, :cache_storage
|
117
|
+
|
79
118
|
def add_config(name)
|
80
119
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
81
120
|
def self.eager_load_fog(fog_credentials)
|
82
121
|
# see #1198. This will hopefully no longer be necessary after fog 2.0
|
122
|
+
require self.fog_provider
|
123
|
+
require 'carrierwave/storage/fog'
|
83
124
|
Fog::Storage.new(fog_credentials) if fog_credentials.present?
|
84
125
|
end
|
85
126
|
|
@@ -93,12 +134,12 @@ module CarrierWave
|
|
93
134
|
end
|
94
135
|
|
95
136
|
def self.#{name}=(value)
|
96
|
-
eager_load_fog(value) if '#{name}' == 'fog_credentials'
|
137
|
+
eager_load_fog(value) if '#{name}' == 'fog_credentials' && value.present?
|
97
138
|
@#{name} = value
|
98
139
|
end
|
99
140
|
|
100
141
|
def #{name}=(value)
|
101
|
-
self.class.eager_load_fog(value) if '#{name}' == 'fog_credentials'
|
142
|
+
self.class.eager_load_fog(value) if '#{name}' == 'fog_credentials' && value.present?
|
102
143
|
@#{name} = value
|
103
144
|
end
|
104
145
|
|
@@ -107,7 +148,7 @@ module CarrierWave
|
|
107
148
|
value = self.class.#{name} unless instance_variable_defined?(:@#{name})
|
108
149
|
if value.instance_of?(Proc)
|
109
150
|
value.arity >= 1 ? value.call(self) : value.call
|
110
|
-
else
|
151
|
+
else
|
111
152
|
value
|
112
153
|
end
|
113
154
|
end
|
@@ -130,11 +171,14 @@ module CarrierWave
|
|
130
171
|
:fog => "CarrierWave::Storage::Fog"
|
131
172
|
}
|
132
173
|
config.storage = :file
|
174
|
+
config.cache_storage = :file
|
175
|
+
config.fog_provider = 'fog'
|
133
176
|
config.fog_attributes = {}
|
134
177
|
config.fog_credentials = {}
|
135
178
|
config.fog_public = true
|
136
179
|
config.fog_authenticated_url_expiration = 600
|
137
180
|
config.fog_use_ssl_for_aws = true
|
181
|
+
config.fog_aws_accelerate = false
|
138
182
|
config.store_dir = 'uploads'
|
139
183
|
config.cache_dir = 'uploads/tmp'
|
140
184
|
config.delete_tmp_file_after_storage = true
|
@@ -158,4 +202,3 @@ module CarrierWave
|
|
158
202
|
end
|
159
203
|
end
|
160
204
|
end
|
161
|
-
|
@@ -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
|
@@ -12,8 +10,9 @@ module CarrierWave
|
|
12
10
|
include CarrierWave::Uploader::Cache
|
13
11
|
|
14
12
|
class RemoteFile
|
15
|
-
def initialize(uri)
|
13
|
+
def initialize(uri, remote_headers = {})
|
16
14
|
@uri = uri
|
15
|
+
@remote_headers = remote_headers
|
17
16
|
end
|
18
17
|
|
19
18
|
def original_filename
|
@@ -37,12 +36,15 @@ module CarrierWave
|
|
37
36
|
|
38
37
|
def file
|
39
38
|
if @file.blank?
|
40
|
-
|
39
|
+
headers = @remote_headers.
|
40
|
+
reverse_merge('User-Agent' => "CarrierWave/#{CarrierWave::VERSION}")
|
41
|
+
|
42
|
+
@file = Kernel.open(@uri.to_s, headers)
|
41
43
|
@file = @file.is_a?(String) ? StringIO.new(@file) : @file
|
42
44
|
end
|
43
45
|
@file
|
44
46
|
|
45
|
-
rescue
|
47
|
+
rescue StandardError => e
|
46
48
|
raise CarrierWave::DownloadError, "could not download file: #{e.message}"
|
47
49
|
end
|
48
50
|
|
@@ -64,10 +66,11 @@ module CarrierWave
|
|
64
66
|
# === Parameters
|
65
67
|
#
|
66
68
|
# [url (String)] The URL where the remote file is stored
|
69
|
+
# [remote_headers (Hash)] Request headers
|
67
70
|
#
|
68
|
-
def download!(uri)
|
71
|
+
def download!(uri, remote_headers = {})
|
69
72
|
processed_uri = process_uri(uri)
|
70
|
-
file = RemoteFile.new(processed_uri)
|
73
|
+
file = RemoteFile.new(processed_uri, remote_headers)
|
71
74
|
raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
|
72
75
|
cache!(file)
|
73
76
|
end
|
@@ -4,7 +4,7 @@ module CarrierWave
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
before :cache, :
|
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
|
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
|
30
|
+
# def extension_blacklist
|
31
31
|
# [/swf/, 'tiff']
|
32
32
|
# end
|
33
33
|
#
|
34
|
-
|
35
|
-
def
|
34
|
+
|
35
|
+
def extension_blacklist; end
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
def
|
39
|
+
def check_extension_blacklist!(new_file)
|
40
40
|
extension = new_file.extension.to_s
|
41
|
-
if
|
42
|
-
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.
|
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, :
|
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
|
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
|
30
|
+
# def extension_whitelist
|
33
31
|
# [/jpe?g/, 'gif', 'png']
|
34
32
|
# end
|
35
33
|
#
|
36
|
-
def
|
34
|
+
def extension_whitelist; end
|
37
35
|
|
38
36
|
private
|
39
37
|
|
40
|
-
def
|
38
|
+
def check_extension_whitelist!(new_file)
|
41
39
|
extension = new_file.extension.to_s
|
42
|
-
if
|
43
|
-
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.
|
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
|
11
|
-
# available
|
12
|
-
# where this instance of the uploader is mounted.
|
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
|
16
|
-
# can override this method inside your uploader. Just be
|
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
|
-
|
12
|
+
before :cache, :process!
|
15
13
|
end
|
16
14
|
|
17
15
|
module ClassMethods
|
@@ -73,15 +71,17 @@ module CarrierWave
|
|
73
71
|
def process!(new_file=nil)
|
74
72
|
return unless enable_processing
|
75
73
|
|
76
|
-
|
77
|
-
|
78
|
-
if
|
79
|
-
|
80
|
-
|
81
|
-
|
74
|
+
with_callbacks(:process, new_file) do
|
75
|
+
self.class.processors.each do |method, args, condition|
|
76
|
+
if(condition)
|
77
|
+
if condition.respond_to?(:call)
|
78
|
+
next unless condition.call(self, :args => args, :method => method, :file => new_file)
|
79
|
+
else
|
80
|
+
next unless self.send(condition, new_file)
|
81
|
+
end
|
82
82
|
end
|
83
|
+
self.send(method, *args)
|
83
84
|
end
|
84
|
-
self.send(method, *args)
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -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.
|
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.
|
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.
|
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.
|
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.
|
81
|
+
file.try(:content_type)
|
84
82
|
end
|
85
83
|
|
86
84
|
end # Proxy
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require "json"
|
4
2
|
require "active_support/core_ext/hash"
|
5
3
|
|
@@ -13,7 +11,7 @@ module CarrierWave
|
|
13
11
|
end
|
14
12
|
|
15
13
|
def as_json(options=nil)
|
16
|
-
|
14
|
+
serializable_hash
|
17
15
|
end
|
18
16
|
|
19
17
|
def to_json(options=nil)
|