paperclip 3.5.4 → 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +17 -20
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +11 -3
- data/LICENSE +1 -3
- data/NEWS +241 -49
- data/README.md +471 -166
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +36 -28
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +151 -46
- data/lib/paperclip/attachment_registry.rb +3 -2
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -22
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/geometry_detector_factory.rb +5 -3
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +26 -13
- data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +90 -0
- data/lib/paperclip/processor.rb +5 -41
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +9 -7
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +37 -19
- data/lib/paperclip/storage/s3.rb +129 -69
- data/lib/paperclip/style.rb +8 -2
- data/lib/paperclip/tempfile_factory.rb +5 -1
- data/lib/paperclip/thumbnail.rb +30 -18
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +11 -4
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +30 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +21 -16
- data/shoulda_macros/paperclip.rb +0 -1
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +502 -407
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +14 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +137 -128
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +132 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +94 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +255 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +153 -134
- data/spec/paperclip/url_generator_spec.rb +222 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +47 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/conditional_filter_helper.rb +5 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +348 -225
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -88
- data/test/file_command_content_type_detector_test.rb +0 -27
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -84
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -232
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -74
- data/test/io_adapters/empty_string_adapter_test.rb +0 -18
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -90
- data/test/paperclip_test.rb +0 -217
- data/test/plural_cache_test.rb +0 -36
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -473
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1356
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -17
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -324
- data/test/validators_test.rb +0 -61
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
data/lib/paperclip/thumbnail.rb
CHANGED
@@ -3,10 +3,11 @@ module Paperclip
|
|
3
3
|
class Thumbnail < Processor
|
4
4
|
|
5
5
|
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options,
|
6
|
-
:source_file_options, :animated, :auto_orient
|
6
|
+
:source_file_options, :animated, :auto_orient, :frame_index
|
7
7
|
|
8
8
|
# List of formats that we need to preserve animation
|
9
9
|
ANIMATED_FORMATS = %w(gif)
|
10
|
+
MULTI_FRAME_FORMATS = %w(.mkv .avi .mp4 .mov .mpg .mpeg .gif)
|
10
11
|
|
11
12
|
# Creates a Thumbnail object set to work on the +file+ given. It
|
12
13
|
# will attempt to transform the image into one defined by +target_geometry+
|
@@ -25,29 +26,29 @@ module Paperclip
|
|
25
26
|
# +whiny+ - whether to raise an error when processing fails. Defaults to true
|
26
27
|
# +format+ - the desired filename extension
|
27
28
|
# +animated+ - whether to merge all the layers in the image. Defaults to true
|
29
|
+
# +frame_index+ - the frame index of the source file to render as the thumbnail
|
28
30
|
def initialize(file, options = {}, attachment = nil)
|
29
31
|
super
|
30
32
|
|
31
|
-
geometry = options[:geometry]
|
32
|
-
@file = file
|
33
|
+
geometry = options[:geometry].to_s
|
33
34
|
@crop = geometry[-1,1] == '#'
|
34
|
-
@target_geometry = (
|
35
|
-
@current_geometry = (
|
35
|
+
@target_geometry = options.fetch(:string_geometry_parser, Geometry).parse(geometry)
|
36
|
+
@current_geometry = options.fetch(:file_geometry_parser, Geometry).from_file(@file)
|
36
37
|
@source_file_options = options[:source_file_options]
|
37
38
|
@convert_options = options[:convert_options]
|
38
|
-
@whiny = options
|
39
|
+
@whiny = options.fetch(:whiny, true)
|
39
40
|
@format = options[:format]
|
40
|
-
@animated = options
|
41
|
-
@auto_orient = options
|
41
|
+
@animated = options.fetch(:animated, true)
|
42
|
+
@auto_orient = options.fetch(:auto_orient, true)
|
42
43
|
if @auto_orient && @current_geometry.respond_to?(:auto_orient)
|
43
44
|
@current_geometry.auto_orient
|
44
45
|
end
|
45
|
-
|
46
46
|
@source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
|
47
47
|
@convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
|
48
48
|
|
49
49
|
@current_format = File.extname(@file.path)
|
50
50
|
@basename = File.basename(@file.path, @current_format)
|
51
|
+
@frame_index = multi_frame_format? ? options.fetch(:frame_index, 0) : 0
|
51
52
|
end
|
52
53
|
|
53
54
|
# Returns true if the +target_geometry+ is meant to crop.
|
@@ -64,8 +65,8 @@ module Paperclip
|
|
64
65
|
# that contains the new image.
|
65
66
|
def make
|
66
67
|
src = @file
|
67
|
-
|
68
|
-
dst.
|
68
|
+
filename = [@basename, @format ? ".#{@format}" : ""].join
|
69
|
+
dst = TempfileFactory.new.generate(filename)
|
69
70
|
|
70
71
|
begin
|
71
72
|
parameters = []
|
@@ -77,10 +78,15 @@ module Paperclip
|
|
77
78
|
|
78
79
|
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
frame = animated? ? "" : "[#{@frame_index}]"
|
82
|
+
convert(
|
83
|
+
parameters,
|
84
|
+
source: "#{File.expand_path(src.path)}#{frame}",
|
85
|
+
dest: File.expand_path(dst.path),
|
86
|
+
)
|
87
|
+
rescue Terrapin::ExitStatusError => e
|
82
88
|
raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
|
83
|
-
rescue
|
89
|
+
rescue Terrapin::CommandNotFoundError => e
|
84
90
|
raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
|
85
91
|
end
|
86
92
|
|
@@ -102,17 +108,23 @@ module Paperclip
|
|
102
108
|
|
103
109
|
protected
|
104
110
|
|
105
|
-
|
111
|
+
def multi_frame_format?
|
112
|
+
MULTI_FRAME_FORMATS.include? @current_format
|
113
|
+
end
|
114
|
+
|
106
115
|
def animated?
|
107
116
|
@animated && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?) && identified_as_animated?
|
108
117
|
end
|
109
118
|
|
110
119
|
# Return true if ImageMagick's +identify+ returns an animated format
|
111
120
|
def identified_as_animated?
|
112
|
-
|
113
|
-
|
121
|
+
if @identified_as_animated.nil?
|
122
|
+
@identified_as_animated = ANIMATED_FORMATS.include? identify("-format %m :file", :file => "#{@file.path}[0]").to_s.downcase.strip
|
123
|
+
end
|
124
|
+
@identified_as_animated
|
125
|
+
rescue Terrapin::ExitStatusError => e
|
114
126
|
raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
|
115
|
-
rescue
|
127
|
+
rescue Terrapin::CommandNotFoundError => e
|
116
128
|
raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
|
117
129
|
end
|
118
130
|
end
|
@@ -1,30 +1,34 @@
|
|
1
1
|
require 'uri'
|
2
|
+
require 'active_support/core_ext/module/delegation'
|
2
3
|
|
3
4
|
module Paperclip
|
4
5
|
class UrlGenerator
|
5
|
-
def initialize(attachment
|
6
|
+
def initialize(attachment)
|
6
7
|
@attachment = attachment
|
7
|
-
@attachment_options = attachment_options
|
8
8
|
end
|
9
9
|
|
10
10
|
def for(style_name, options)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
interpolated = attachment_options[:interpolator].interpolate(
|
12
|
+
most_appropriate_url, @attachment, style_name
|
13
|
+
)
|
14
|
+
|
15
|
+
escaped = escape_url_as_needed(interpolated, options)
|
16
|
+
timestamp_as_needed(escaped, options)
|
16
17
|
end
|
17
18
|
|
18
19
|
private
|
19
20
|
|
21
|
+
attr_reader :attachment
|
22
|
+
delegate :options, to: :attachment, prefix: true
|
23
|
+
|
20
24
|
# This method is all over the place.
|
21
25
|
def default_url
|
22
|
-
if
|
23
|
-
|
24
|
-
elsif
|
25
|
-
@attachment.instance.send(
|
26
|
+
if attachment_options[:default_url].respond_to?(:call)
|
27
|
+
attachment_options[:default_url].call(@attachment)
|
28
|
+
elsif attachment_options[:default_url].is_a?(Symbol)
|
29
|
+
@attachment.instance.send(attachment_options[:default_url])
|
26
30
|
else
|
27
|
-
|
31
|
+
attachment_options[:default_url]
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
@@ -32,7 +36,7 @@ module Paperclip
|
|
32
36
|
if @attachment.original_filename.nil?
|
33
37
|
default_url
|
34
38
|
else
|
35
|
-
|
39
|
+
attachment_options[:url]
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
@@ -58,7 +62,15 @@ module Paperclip
|
|
58
62
|
end
|
59
63
|
|
60
64
|
def escape_url(url)
|
61
|
-
|
65
|
+
if url.respond_to?(:escape)
|
66
|
+
url.escape
|
67
|
+
else
|
68
|
+
URI.escape(url).gsub(escape_regex){|m| "%#{m.ord.to_s(16).upcase}" }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def escape_regex
|
73
|
+
/[\?\(\)\[\]\+]/
|
62
74
|
end
|
63
75
|
end
|
64
76
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Validators
|
3
|
+
class AttachmentFileNameValidator < ActiveModel::EachValidator
|
4
|
+
def initialize(options)
|
5
|
+
options[:allow_nil] = true unless options.has_key?(:allow_nil)
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.helper_method_name
|
10
|
+
:validates_attachment_file_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_each(record, attribute, value)
|
14
|
+
base_attribute = attribute.to_sym
|
15
|
+
attribute = "#{attribute}_file_name".to_sym
|
16
|
+
value = record.send :read_attribute_for_validation, attribute
|
17
|
+
|
18
|
+
return if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
|
19
|
+
|
20
|
+
validate_whitelist(record, attribute, value)
|
21
|
+
validate_blacklist(record, attribute, value)
|
22
|
+
|
23
|
+
if record.errors.include? attribute
|
24
|
+
record.errors[attribute].each do |error|
|
25
|
+
record.errors.add base_attribute, error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_whitelist(record, attribute, value)
|
31
|
+
if allowed.present? && allowed.none? { |type| type === value }
|
32
|
+
mark_invalid record, attribute, allowed
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def validate_blacklist(record, attribute, value)
|
37
|
+
if forbidden.present? && forbidden.any? { |type| type === value }
|
38
|
+
mark_invalid record, attribute, forbidden
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def mark_invalid(record, attribute, patterns)
|
43
|
+
record.errors.add attribute, :invalid, options.merge(:names => patterns.join(', '))
|
44
|
+
end
|
45
|
+
|
46
|
+
def allowed
|
47
|
+
[options[:matches]].flatten.compact
|
48
|
+
end
|
49
|
+
|
50
|
+
def forbidden
|
51
|
+
[options[:not]].flatten.compact
|
52
|
+
end
|
53
|
+
|
54
|
+
def check_validity!
|
55
|
+
unless options.has_key?(:matches) || options.has_key?(:not)
|
56
|
+
raise ArgumentError, "You must pass in either :matches or :not to the validator"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module HelperMethods
|
62
|
+
# Places ActiveModel validations on the name of the file
|
63
|
+
# assigned. The possible options are:
|
64
|
+
# * +matches+: Allowed filename patterns as Regexps. Can be a single one
|
65
|
+
# or an array.
|
66
|
+
# * +not+: Forbidden file name patterns, specified the same was as +matches+.
|
67
|
+
# * +message+: The message to display when the uploaded file has an invalid
|
68
|
+
# name.
|
69
|
+
# * +if+: A lambda or name of an instance method. Validation will only
|
70
|
+
# be run is this lambda or method returns true.
|
71
|
+
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
72
|
+
def validates_attachment_file_name(*attr_names)
|
73
|
+
options = _merge_attributes(attr_names)
|
74
|
+
validates_with AttachmentFileNameValidator, options.dup
|
75
|
+
validate_before_processing AttachmentFileNameValidator, options.dup
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_model/validations/presence'
|
2
|
+
|
3
|
+
module Paperclip
|
4
|
+
module Validators
|
5
|
+
class AttachmentFileTypeIgnoranceValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
# This doesn't do anything. It's just to mark that you don't care about
|
8
|
+
# the file_names or content_types of your incoming attachments.
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.helper_method_name
|
12
|
+
:do_not_validate_attachment_file_type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module HelperMethods
|
17
|
+
# Places ActiveModel validations on the presence of a file.
|
18
|
+
# Options:
|
19
|
+
# * +if+: A lambda or name of an instance method. Validation will only
|
20
|
+
# be run if this lambda or method returns true.
|
21
|
+
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
22
|
+
def do_not_validate_attachment_file_type(*attr_names)
|
23
|
+
options = _merge_attributes(attr_names)
|
24
|
+
validates_with AttachmentFileTypeIgnoranceValidator, options.dup
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -10,6 +10,10 @@ module Paperclip
|
|
10
10
|
super
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.helper_method_name
|
14
|
+
:validates_attachment_size
|
15
|
+
end
|
16
|
+
|
13
17
|
def validate_each(record, attr_name, value)
|
14
18
|
base_attr_name = attr_name
|
15
19
|
attr_name = "#{attr_name}_file_size".to_sym
|
@@ -67,9 +71,7 @@ module Paperclip
|
|
67
71
|
end
|
68
72
|
|
69
73
|
def human_size(size)
|
70
|
-
|
71
|
-
unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => size.to_i, :raise => true)
|
72
|
-
storage_units_format.gsub(/%n/, size.to_i.to_s).gsub(/%u/, unit).html_safe
|
74
|
+
ActiveSupport::NumberHelper.number_to_human_size(size)
|
73
75
|
end
|
74
76
|
|
75
77
|
def min_value_in_human_size(record)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_model/validations/presence'
|
2
|
+
|
3
|
+
module Paperclip
|
4
|
+
module Validators
|
5
|
+
class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
adapter = Paperclip.io_adapters.for(value)
|
8
|
+
if Paperclip::MediaTypeSpoofDetector.using(adapter, value.original_filename, value.content_type).spoofed?
|
9
|
+
record.errors.add(attribute, :spoofed_media_type)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module HelperMethods
|
15
|
+
# Places ActiveModel validations on the presence of a file.
|
16
|
+
# Options:
|
17
|
+
# * +if+: A lambda or name of an instance method. Validation will only
|
18
|
+
# be run if this lambda or method returns true.
|
19
|
+
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
20
|
+
def validates_media_type_spoof_detection(*attr_names)
|
21
|
+
options = _merge_attributes(attr_names)
|
22
|
+
validates_with MediaTypeSpoofDetectionValidator, options.dup
|
23
|
+
validate_before_processing MediaTypeSpoofDetectionValidator, options.dup
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/paperclip/validators.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
3
4
|
require 'paperclip/validators/attachment_content_type_validator'
|
5
|
+
require 'paperclip/validators/attachment_file_name_validator'
|
4
6
|
require 'paperclip/validators/attachment_presence_validator'
|
5
7
|
require 'paperclip/validators/attachment_size_validator'
|
8
|
+
require 'paperclip/validators/media_type_spoof_detection_validator'
|
9
|
+
require 'paperclip/validators/attachment_file_type_ignorance_validator'
|
6
10
|
|
7
11
|
module Paperclip
|
8
12
|
module Validators
|
@@ -13,6 +17,8 @@ module Paperclip
|
|
13
17
|
include HelperMethods
|
14
18
|
end
|
15
19
|
|
20
|
+
::Paperclip::REQUIRED_VALIDATORS = [AttachmentFileNameValidator, AttachmentContentTypeValidator, AttachmentFileTypeIgnoranceValidator]
|
21
|
+
|
16
22
|
module ClassMethods
|
17
23
|
# This method is a shortcut to validator classes that is in
|
18
24
|
# "Attachment...Validator" format. It is almost the same thing as the
|
@@ -30,16 +36,17 @@ module Paperclip
|
|
30
36
|
options = attributes.extract_options!.dup
|
31
37
|
|
32
38
|
Paperclip::Validators.constants.each do |constant|
|
33
|
-
if constant.to_s =~ /\AAttachment(.+)Validator\
|
39
|
+
if constant.to_s =~ /\AAttachment(.+)Validator\z/
|
34
40
|
validator_kind = $1.underscore.to_sym
|
35
41
|
|
36
42
|
if options.has_key?(validator_kind)
|
37
43
|
validator_options = options.delete(validator_kind)
|
38
44
|
validator_options = {} if validator_options == true
|
39
|
-
local_options = attributes + [validator_options]
|
40
45
|
conditional_options = options.slice(:if, :unless)
|
41
|
-
|
42
|
-
|
46
|
+
Array.wrap(validator_options).each do |local_options|
|
47
|
+
method_name = Paperclip::Validators.const_get(constant.to_s).helper_method_name
|
48
|
+
send(method_name, attributes, local_options.merge(conditional_options))
|
49
|
+
end
|
43
50
|
end
|
44
51
|
end
|
45
52
|
end
|
data/lib/paperclip/version.rb
CHANGED
data/lib/paperclip.rb
CHANGED
@@ -33,6 +33,7 @@ require 'paperclip/geometry_parser_factory'
|
|
33
33
|
require 'paperclip/geometry_detector_factory'
|
34
34
|
require 'paperclip/geometry'
|
35
35
|
require 'paperclip/processor'
|
36
|
+
require 'paperclip/processor_helpers'
|
36
37
|
require 'paperclip/tempfile'
|
37
38
|
require 'paperclip/thumbnail'
|
38
39
|
require 'paperclip/interpolations/plural_cache'
|
@@ -43,6 +44,7 @@ require 'paperclip/attachment'
|
|
43
44
|
require 'paperclip/storage'
|
44
45
|
require 'paperclip/callbacks'
|
45
46
|
require 'paperclip/file_command_content_type_detector'
|
47
|
+
require 'paperclip/media_type_spoof_detector'
|
46
48
|
require 'paperclip/content_type_detector'
|
47
49
|
require 'paperclip/glue'
|
48
50
|
require 'paperclip/errors'
|
@@ -53,11 +55,21 @@ require 'paperclip/helpers'
|
|
53
55
|
require 'paperclip/has_attached_file'
|
54
56
|
require 'paperclip/attachment_registry'
|
55
57
|
require 'paperclip/filename_cleaner'
|
56
|
-
require '
|
58
|
+
require 'paperclip/rails_environment'
|
59
|
+
|
60
|
+
begin
|
61
|
+
# Use mime/types/columnar if available, for reduced memory usage
|
62
|
+
require "mime/types/columnar"
|
63
|
+
rescue LoadError
|
64
|
+
require "mime/types"
|
65
|
+
end
|
66
|
+
|
67
|
+
require 'mimemagic'
|
68
|
+
require 'mimemagic/overlay'
|
57
69
|
require 'logger'
|
58
|
-
require '
|
70
|
+
require 'terrapin'
|
59
71
|
|
60
|
-
require 'paperclip/railtie' if defined?(Rails)
|
72
|
+
require 'paperclip/railtie' if defined?(Rails::Railtie)
|
61
73
|
|
62
74
|
# The base module that gets included in ActiveRecord::Base. See the
|
63
75
|
# documentation for Paperclip::ClassMethods for more useful information.
|
@@ -74,14 +86,18 @@ module Paperclip
|
|
74
86
|
# * command_path: Defines the path at which to find the command line
|
75
87
|
# programs if they are not visible to Rails the system's search path. Defaults to
|
76
88
|
# nil, which uses the first executable found in the user's search path.
|
89
|
+
# * use_exif_orientation: Whether to inspect EXIF data to determine an
|
90
|
+
# image's orientation. Defaults to true.
|
77
91
|
def self.options
|
78
92
|
@options ||= {
|
79
|
-
:
|
80
|
-
:
|
81
|
-
:
|
82
|
-
:
|
83
|
-
:
|
84
|
-
:
|
93
|
+
command_path: nil,
|
94
|
+
content_type_mappings: {},
|
95
|
+
log: true,
|
96
|
+
log_command: true,
|
97
|
+
read_timeout: nil,
|
98
|
+
swallow_stderr: true,
|
99
|
+
use_exif_orientation: true,
|
100
|
+
whiny: true,
|
85
101
|
}
|
86
102
|
end
|
87
103
|
|
@@ -103,7 +119,7 @@ module Paperclip
|
|
103
119
|
# called on it, the attachment will *not* be deleted until +save+ is called. See the
|
104
120
|
# Paperclip::Attachment documentation for more specifics. There are a number of options
|
105
121
|
# you can set to change the behavior of a Paperclip attachment:
|
106
|
-
# * +url+: The full URL of where the attachment is
|
122
|
+
# * +url+: The full URL of where the attachment is publicly accessible. This can just
|
107
123
|
# as easily point to a directory served directly through Apache as it can to an action
|
108
124
|
# that can control permissions. You can specify the full domain and path, but usually
|
109
125
|
# just an absolute path is sufficient. The leading slash *must* be included manually for
|
@@ -112,6 +128,9 @@ module Paperclip
|
|
112
128
|
# Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
113
129
|
# :url => "/:class/:attachment/:id/:style_:filename"
|
114
130
|
# :url => "http://some.other.host/stuff/:class/:id_:extension"
|
131
|
+
# Note: When using the +s3+ storage option, the +url+ option expects
|
132
|
+
# particular values. See the Paperclip::Storage::S3#url documentation for
|
133
|
+
# specifics.
|
115
134
|
# * +default_url+: The URL that will be returned if there is no attachment assigned.
|
116
135
|
# This field is interpolated just as the url is. The default value is
|
117
136
|
# "/:attachment/:style/missing.png"
|
@@ -130,7 +149,7 @@ module Paperclip
|
|
130
149
|
# user.avatar.url # => "/avatars/23/normal_me.png"
|
131
150
|
# * +keep_old_files+: Keep the existing attachment files (original + resized) from
|
132
151
|
# being automatically deleted when an attachment is cleared or updated. Defaults to +false+.
|
133
|
-
# * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
|
152
|
+
# * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
|
134
153
|
# record is destroyed. Defaults to +false+.
|
135
154
|
# * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
|
136
155
|
# to a command line error. This will override the global setting for this attachment.
|
data/lib/tasks/paperclip.rake
CHANGED
@@ -18,7 +18,7 @@ module Paperclip
|
|
18
18
|
raise "Class #{klass.name} has no attachments specified"
|
19
19
|
end
|
20
20
|
|
21
|
-
if
|
21
|
+
if name.present? && attachment_names.map(&:to_s).include?(name.to_s)
|
22
22
|
[ name ]
|
23
23
|
else
|
24
24
|
attachment_names
|
@@ -46,7 +46,7 @@ namespace :paperclip do
|
|
46
46
|
attachment = instance.send(name)
|
47
47
|
begin
|
48
48
|
attachment.reprocess!(*styles)
|
49
|
-
rescue
|
49
|
+
rescue StandardError => e
|
50
50
|
Paperclip::Task.log_error("exception while processing #{klass} ID #{instance.id}:")
|
51
51
|
Paperclip::Task.log_error(" " + e.message + "\n")
|
52
52
|
end
|
@@ -64,7 +64,8 @@ namespace :paperclip do
|
|
64
64
|
names = Paperclip::Task.obtain_attachments(klass)
|
65
65
|
names.each do |name|
|
66
66
|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
67
|
-
|
67
|
+
attachment = instance.send(name)
|
68
|
+
if file = Paperclip.io_adapters.for(attachment, attachment.options[:adapter_options])
|
68
69
|
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
|
69
70
|
instance.send("#{name}_content_type=", file.content_type.to_s.strip)
|
70
71
|
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
|
@@ -78,8 +79,7 @@ namespace :paperclip do
|
|
78
79
|
|
79
80
|
desc "Regenerates missing thumbnail styles for all classes using Paperclip."
|
80
81
|
task :missing_styles => :environment do
|
81
|
-
|
82
|
-
Dir[Rails.root + 'app/models/**/*.rb'].each { |path| load path }
|
82
|
+
Rails.application.eager_load!
|
83
83
|
Paperclip.missing_attachments_styles.each do |klass, attachment_definitions|
|
84
84
|
attachment_definitions.each do |attachment_name, missing_styles|
|
85
85
|
puts "Regenerating #{klass} -> #{attachment_name} -> #{missing_styles.inspect}"
|
@@ -91,6 +91,19 @@ namespace :paperclip do
|
|
91
91
|
end
|
92
92
|
Paperclip.save_current_attachments_styles!
|
93
93
|
end
|
94
|
+
|
95
|
+
desc "Regenerates fingerprints for a given CLASS (and optional ATTACHMENT). Useful when changing digest."
|
96
|
+
task :fingerprints => :environment do
|
97
|
+
klass = Paperclip::Task.obtain_class
|
98
|
+
names = Paperclip::Task.obtain_attachments(klass)
|
99
|
+
names.each do |name|
|
100
|
+
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
101
|
+
attachment = instance.send(name)
|
102
|
+
attachment.assign(attachment)
|
103
|
+
instance.save(:validate => false)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
94
107
|
end
|
95
108
|
|
96
109
|
desc "Cleans out invalid attachments. Useful after you've added new validations."
|
@@ -109,4 +122,20 @@ namespace :paperclip do
|
|
109
122
|
end
|
110
123
|
end
|
111
124
|
end
|
125
|
+
|
126
|
+
desc "find missing attachments. Useful to know which attachments are broken"
|
127
|
+
task :find_broken_attachments => :environment do
|
128
|
+
klass = Paperclip::Task.obtain_class
|
129
|
+
names = Paperclip::Task.obtain_attachments(klass)
|
130
|
+
names.each do |name|
|
131
|
+
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
132
|
+
attachment = instance.send(name)
|
133
|
+
if attachment.exists?
|
134
|
+
print "."
|
135
|
+
else
|
136
|
+
Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
112
141
|
end
|
data/paperclip.gemspec
CHANGED
@@ -12,38 +12,43 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.description = "Easy upload management for ActiveRecord"
|
13
13
|
s.license = "MIT"
|
14
14
|
|
15
|
-
s.rubyforge_project = "paperclip"
|
16
|
-
|
17
15
|
s.files = `git ls-files`.split("\n")
|
18
|
-
s.test_files = `git ls-files -- {
|
16
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
19
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
18
|
s.require_paths = ["lib"]
|
21
19
|
|
20
|
+
if File.exist?('UPGRADING')
|
21
|
+
s.post_install_message = File.read("UPGRADING")
|
22
|
+
end
|
23
|
+
|
22
24
|
s.requirements << "ImageMagick"
|
23
|
-
s.required_ruby_version = ">= 1.
|
25
|
+
s.required_ruby_version = ">= 2.1.0"
|
24
26
|
|
25
|
-
s.add_dependency('activemodel', '>=
|
26
|
-
s.add_dependency('activesupport', '>=
|
27
|
-
s.add_dependency('
|
27
|
+
s.add_dependency('activemodel', '>= 4.2.0')
|
28
|
+
s.add_dependency('activesupport', '>= 4.2.0')
|
29
|
+
s.add_dependency('terrapin', '~> 0.6.0')
|
28
30
|
s.add_dependency('mime-types')
|
31
|
+
s.add_dependency('mimemagic', '~> 0.3.0')
|
29
32
|
|
30
|
-
s.add_development_dependency('activerecord', '>=
|
33
|
+
s.add_development_dependency('activerecord', '>= 4.2.0')
|
31
34
|
s.add_development_dependency('shoulda')
|
35
|
+
s.add_development_dependency('rspec', '~> 3.0')
|
32
36
|
s.add_development_dependency('appraisal')
|
33
37
|
s.add_development_dependency('mocha')
|
34
|
-
s.add_development_dependency('aws-sdk', '>=
|
38
|
+
s.add_development_dependency('aws-sdk', '>= 2.3.0', '< 3.0')
|
35
39
|
s.add_development_dependency('bourne')
|
36
|
-
s.add_development_dependency('
|
37
|
-
s.add_development_dependency('cucumber', '
|
38
|
-
s.add_development_dependency('aruba')
|
40
|
+
s.add_development_dependency('cucumber-rails')
|
41
|
+
s.add_development_dependency('cucumber-expressions', '4.0.3') # TODO: investigate failures on 4.0.4
|
42
|
+
s.add_development_dependency('aruba', '~> 0.9.0')
|
39
43
|
s.add_development_dependency('nokogiri')
|
40
|
-
|
41
|
-
s.add_development_dependency('capybara', '= 2.0.3')
|
44
|
+
s.add_development_dependency('capybara')
|
42
45
|
s.add_development_dependency('bundler')
|
43
|
-
s.add_development_dependency('fog'
|
46
|
+
s.add_development_dependency('fog-aws')
|
47
|
+
s.add_development_dependency('fog-local')
|
44
48
|
s.add_development_dependency('launchy')
|
45
49
|
s.add_development_dependency('rake')
|
46
50
|
s.add_development_dependency('fakeweb')
|
47
51
|
s.add_development_dependency('railties')
|
48
|
-
s.add_development_dependency('
|
52
|
+
s.add_development_dependency('generator_spec')
|
53
|
+
s.add_development_dependency('timecop')
|
49
54
|
end
|
data/shoulda_macros/paperclip.rb
CHANGED