carrierwave 1.2.1 → 2.1.1

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.

@@ -1,94 +0,0 @@
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