paperclip 3.4.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -12
- data/Appraisals +4 -11
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +13 -4
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +390 -71
- data/README.md +607 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +34 -21
- data/features/migration.feature +0 -24
- data/features/rake_tasks.feature +2 -3
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +125 -26
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/fakeweb.rb +4 -1
- data/features/support/file_helpers.rb +12 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +4 -11
- 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 +215 -82
- data/lib/paperclip/attachment_registry.rb +60 -0
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +48 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +15 -0
- data/lib/paperclip/geometry_detector_factory.rb +12 -5
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -2
- data/lib/paperclip/has_attached_file.rb +115 -0
- data/lib/paperclip/helpers.rb +15 -20
- data/lib/paperclip/interpolations/plural_cache.rb +18 -0
- data/lib/paperclip/interpolations.rb +36 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
- 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 +11 -7
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/matchers.rb +1 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/missing_attachment_styles.rb +11 -16
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +20 -5
- data/lib/paperclip/storage/fog.rb +49 -23
- data/lib/paperclip/storage/s3.rb +153 -82
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +35 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
- 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 +12 -8
- data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +31 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +41 -55
- data/lib/tasks/paperclip.rake +56 -9
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/spec/paperclip/attachment_processing_spec.rb +79 -0
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
- data/spec/paperclip/content_type_detector_spec.rb +48 -0
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -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} +179 -199
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
- 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/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -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/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -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 +120 -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/spec/paperclip/rake_spec.rb +103 -0
- 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 +254 -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} +186 -141
- data/spec/paperclip/url_generator_spec.rb +221 -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} +87 -59
- 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 +46 -0
- data/spec/support/assertions.rb +82 -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 +395 -346
- data/Gemfile.lock +0 -200
- 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/lib/paperclip/attachment_options.rb +0 -9
- data/lib/paperclip/instance_methods.rb +0 -35
- data/test/attachment_options_test.rb +0 -27
- data/test/attachment_processing_test.rb +0 -29
- data/test/content_type_detector_test.rb +0 -40
- data/test/file_command_content_type_detector_test.rb +0 -25
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/helper.rb +0 -199
- data/test/io_adapters/abstract_adapter_test.rb +0 -50
- data/test/io_adapters/file_adapter_test.rb +0 -100
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/stringio_adapter_test.rb +0 -51
- data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
- data/test/io_adapters/uri_adapter_test.rb +0 -86
- 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 -47
- 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 -94
- data/test/paperclip_test.rb +0 -259
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -453
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1236
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -13
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -292
- data/test/validators_test.rb +0 -25
- /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
@@ -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
|
@@ -6,7 +6,12 @@ module Paperclip
|
|
6
6
|
super
|
7
7
|
end
|
8
8
|
|
9
|
+
def self.helper_method_name
|
10
|
+
:validates_attachment_content_type
|
11
|
+
end
|
12
|
+
|
9
13
|
def validate_each(record, attribute, value)
|
14
|
+
base_attribute = attribute.to_sym
|
10
15
|
attribute = "#{attribute}_content_type".to_sym
|
11
16
|
value = record.send :read_attribute_for_validation, attribute
|
12
17
|
|
@@ -14,6 +19,12 @@ module Paperclip
|
|
14
19
|
|
15
20
|
validate_whitelist(record, attribute, value)
|
16
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
|
17
28
|
end
|
18
29
|
|
19
30
|
def validate_whitelist(record, attribute, value)
|
@@ -48,7 +59,7 @@ module Paperclip
|
|
48
59
|
end
|
49
60
|
|
50
61
|
module HelperMethods
|
51
|
-
# Places
|
62
|
+
# Places ActiveModel validations on the content type of the file
|
52
63
|
# assigned. The possible options are:
|
53
64
|
# * +content_type+: Allowed content types. Can be a single content type
|
54
65
|
# or an array. Each type can be a String or a Regexp. It should be
|
@@ -68,7 +79,9 @@ module Paperclip
|
|
68
79
|
# You'll still need to have a virtual attribute (created by +attr_accessor+)
|
69
80
|
# name +[attachment]_content_type+ to be able to use this validator.
|
70
81
|
def validates_attachment_content_type(*attr_names)
|
71
|
-
|
82
|
+
options = _merge_attributes(attr_names)
|
83
|
+
validates_with AttachmentContentTypeValidator, options.dup
|
84
|
+
validate_before_processing AttachmentContentTypeValidator, options.dup
|
72
85
|
end
|
73
86
|
end
|
74
87
|
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
|
+
|
@@ -2,24 +2,28 @@ require 'active_model/validations/presence'
|
|
2
2
|
|
3
3
|
module Paperclip
|
4
4
|
module Validators
|
5
|
-
class AttachmentPresenceValidator < ActiveModel::
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
record.errors.add(attribute, :blank, options)
|
10
|
-
end
|
5
|
+
class AttachmentPresenceValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
if record.send("#{attribute}_file_name").blank?
|
8
|
+
record.errors.add(attribute, :blank, options)
|
11
9
|
end
|
12
10
|
end
|
11
|
+
|
12
|
+
def self.helper_method_name
|
13
|
+
:validates_attachment_presence
|
14
|
+
end
|
13
15
|
end
|
14
16
|
|
15
17
|
module HelperMethods
|
16
|
-
# Places
|
18
|
+
# Places ActiveModel validations on the presence of a file.
|
17
19
|
# Options:
|
18
20
|
# * +if+: A lambda or name of an instance method. Validation will only
|
19
21
|
# be run if this lambda or method returns true.
|
20
22
|
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
21
23
|
def validates_attachment_presence(*attr_names)
|
22
|
-
|
24
|
+
options = _merge_attributes(attr_names)
|
25
|
+
validates_with AttachmentPresenceValidator, options.dup
|
26
|
+
validate_before_processing AttachmentPresenceValidator, options.dup
|
23
27
|
end
|
24
28
|
end
|
25
29
|
end
|
@@ -10,7 +10,12 @@ 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)
|
18
|
+
base_attr_name = attr_name
|
14
19
|
attr_name = "#{attr_name}_file_size".to_sym
|
15
20
|
value = record.send(:read_attribute_for_validation, attr_name)
|
16
21
|
|
@@ -21,11 +26,13 @@ module Paperclip
|
|
21
26
|
|
22
27
|
unless value.send(CHECKS[option], option_value)
|
23
28
|
error_message_key = options[:in] ? :in_between : option
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
[ attr_name, base_attr_name ].each do |error_attr_name|
|
30
|
+
record.errors.add(error_attr_name, error_message_key, filtered_options(value).merge(
|
31
|
+
:min => min_value_in_human_size(record),
|
32
|
+
:max => max_value_in_human_size(record),
|
33
|
+
:count => human_size(option_value)
|
34
|
+
))
|
35
|
+
end
|
29
36
|
end
|
30
37
|
end
|
31
38
|
end
|
@@ -64,9 +71,7 @@ module Paperclip
|
|
64
71
|
end
|
65
72
|
|
66
73
|
def human_size(size)
|
67
|
-
|
68
|
-
unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => size.to_i, :raise => true)
|
69
|
-
storage_units_format.gsub(/%n/, size.to_i.to_s).gsub(/%u/, unit).html_safe
|
74
|
+
ActiveSupport::NumberHelper.number_to_human_size(size)
|
70
75
|
end
|
71
76
|
|
72
77
|
def min_value_in_human_size(record)
|
@@ -85,7 +90,7 @@ module Paperclip
|
|
85
90
|
end
|
86
91
|
|
87
92
|
module HelperMethods
|
88
|
-
# Places
|
93
|
+
# Places ActiveModel validations on the size of the file assigned. The
|
89
94
|
# possible options are:
|
90
95
|
# * +in+: a Range of bytes (i.e. +1..1.megabyte+),
|
91
96
|
# * +less_than+: equivalent to :in => 0..options[:less_than]
|
@@ -95,7 +100,9 @@ module Paperclip
|
|
95
100
|
# be run if this lambda or method returns true.
|
96
101
|
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
97
102
|
def validates_attachment_size(*attr_names)
|
98
|
-
|
103
|
+
options = _merge_attributes(attr_names)
|
104
|
+
validates_with AttachmentSizeValidator, options.dup
|
105
|
+
validate_before_processing AttachmentSizeValidator, options.dup
|
99
106
|
end
|
100
107
|
end
|
101
108
|
end
|
@@ -0,0 +1,31 @@
|
|
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
|
+
|
12
|
+
if adapter.tempfile
|
13
|
+
adapter.tempfile.close(true)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module HelperMethods
|
19
|
+
# Places ActiveModel validations on the presence of a file.
|
20
|
+
# Options:
|
21
|
+
# * +if+: A lambda or name of an instance method. Validation will only
|
22
|
+
# be run if this lambda or method returns true.
|
23
|
+
# * +unless+: Same as +if+ but validates if lambda or method returns false.
|
24
|
+
def validates_media_type_spoof_detection(*attr_names)
|
25
|
+
options = _merge_attributes(attr_names)
|
26
|
+
validates_with MediaTypeSpoofDetectionValidator, options.dup
|
27
|
+
validate_before_processing MediaTypeSpoofDetectionValidator, options.dup
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
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,17 +36,39 @@ module Paperclip
|
|
30
36
|
options = attributes.extract_options!.dup
|
31
37
|
|
32
38
|
Paperclip::Validators.constants.each do |constant|
|
33
|
-
if constant.to_s =~
|
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)
|
44
|
+
validator_options = {} if validator_options == true
|
45
|
+
conditional_options = options.slice(:if, :unless)
|
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
|
38
50
|
end
|
39
51
|
end
|
40
52
|
end
|
53
|
+
end
|
41
54
|
|
42
|
-
|
55
|
+
def validate_before_processing(validator_class, options)
|
56
|
+
options = options.dup
|
57
|
+
attributes = options.delete(:attributes)
|
58
|
+
attributes.each do |attribute|
|
59
|
+
options[:attributes] = [attribute]
|
60
|
+
create_validating_before_filter(attribute, validator_class, options)
|
61
|
+
end
|
43
62
|
end
|
63
|
+
|
64
|
+
def create_validating_before_filter(attribute, validator_class, options)
|
65
|
+
if_clause = options.delete(:if)
|
66
|
+
unless_clause = options.delete(:unless)
|
67
|
+
send(:"before_#{attribute}_post_process", :if => if_clause, :unless => unless_clause) do |*args|
|
68
|
+
validator_class.new(options.dup).validate(self)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
44
72
|
end
|
45
73
|
end
|
46
74
|
end
|
data/lib/paperclip/version.rb
CHANGED
data/lib/paperclip.rb
CHANGED
@@ -33,29 +33,43 @@ 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'
|
39
|
+
require 'paperclip/interpolations/plural_cache'
|
38
40
|
require 'paperclip/interpolations'
|
39
41
|
require 'paperclip/tempfile_factory'
|
40
42
|
require 'paperclip/style'
|
41
43
|
require 'paperclip/attachment'
|
42
|
-
require 'paperclip/attachment_options'
|
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'
|
49
51
|
require 'paperclip/missing_attachment_styles'
|
50
52
|
require 'paperclip/validators'
|
51
|
-
require 'paperclip/instance_methods'
|
52
53
|
require 'paperclip/logger'
|
53
54
|
require 'paperclip/helpers'
|
54
|
-
require '
|
55
|
+
require 'paperclip/has_attached_file'
|
56
|
+
require 'paperclip/attachment_registry'
|
57
|
+
require 'paperclip/filename_cleaner'
|
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'
|
55
69
|
require 'logger'
|
56
|
-
require '
|
70
|
+
require 'terrapin'
|
57
71
|
|
58
|
-
require 'paperclip/railtie' if defined?(Rails)
|
72
|
+
require 'paperclip/railtie' if defined?(Rails::Railtie)
|
59
73
|
|
60
74
|
# The base module that gets included in ActiveRecord::Base. See the
|
61
75
|
# documentation for Paperclip::ClassMethods for more useful information.
|
@@ -72,14 +86,19 @@ module Paperclip
|
|
72
86
|
# * command_path: Defines the path at which to find the command line
|
73
87
|
# programs if they are not visible to Rails the system's search path. Defaults to
|
74
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.
|
75
91
|
def self.options
|
76
92
|
@options ||= {
|
77
|
-
:
|
78
|
-
:
|
79
|
-
:
|
80
|
-
:
|
81
|
-
:
|
82
|
-
:
|
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,
|
101
|
+
is_windows: Gem.win_platform?
|
83
102
|
}
|
84
103
|
end
|
85
104
|
|
@@ -101,7 +120,7 @@ module Paperclip
|
|
101
120
|
# called on it, the attachment will *not* be deleted until +save+ is called. See the
|
102
121
|
# Paperclip::Attachment documentation for more specifics. There are a number of options
|
103
122
|
# you can set to change the behavior of a Paperclip attachment:
|
104
|
-
# * +url+: The full URL of where the attachment is
|
123
|
+
# * +url+: The full URL of where the attachment is publicly accessible. This can just
|
105
124
|
# as easily point to a directory served directly through Apache as it can to an action
|
106
125
|
# that can control permissions. You can specify the full domain and path, but usually
|
107
126
|
# just an absolute path is sufficient. The leading slash *must* be included manually for
|
@@ -110,6 +129,9 @@ module Paperclip
|
|
110
129
|
# Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
111
130
|
# :url => "/:class/:attachment/:id/:style_:filename"
|
112
131
|
# :url => "http://some.other.host/stuff/:class/:id_:extension"
|
132
|
+
# Note: When using the +s3+ storage option, the +url+ option expects
|
133
|
+
# particular values. See the Paperclip::Storage::S3#url documentation for
|
134
|
+
# specifics.
|
113
135
|
# * +default_url+: The URL that will be returned if there is no attachment assigned.
|
114
136
|
# This field is interpolated just as the url is. The default value is
|
115
137
|
# "/:attachment/:style/missing.png"
|
@@ -127,8 +149,9 @@ module Paperclip
|
|
127
149
|
# :default_style => :normal
|
128
150
|
# user.avatar.url # => "/avatars/23/normal_me.png"
|
129
151
|
# * +keep_old_files+: Keep the existing attachment files (original + resized) from
|
130
|
-
# being automatically deleted when an attachment is cleared or updated.
|
131
|
-
#
|
152
|
+
# being automatically deleted when an attachment is cleared or updated. Defaults to +false+.
|
153
|
+
# * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
|
154
|
+
# record is destroyed. Defaults to +false+.
|
132
155
|
# * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
|
133
156
|
# to a command line error. This will override the global setting for this attachment.
|
134
157
|
# Defaults to true.
|
@@ -172,47 +195,7 @@ module Paperclip
|
|
172
195
|
# end
|
173
196
|
# end
|
174
197
|
def has_attached_file(name, options = {})
|
175
|
-
|
176
|
-
|
177
|
-
if attachment_definitions.nil?
|
178
|
-
self.attachment_definitions = {}
|
179
|
-
else
|
180
|
-
self.attachment_definitions = self.attachment_definitions.dup
|
181
|
-
end
|
182
|
-
|
183
|
-
attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
|
184
|
-
Paperclip.classes_with_attachments << self.name
|
185
|
-
Paperclip.check_for_path_clash(name,attachment_definitions[name][:path],self.name)
|
186
|
-
|
187
|
-
after_save :save_attached_files
|
188
|
-
before_destroy :prepare_for_destroy
|
189
|
-
after_destroy :destroy_attached_files
|
190
|
-
|
191
|
-
define_paperclip_callbacks :post_process, :"#{name}_post_process"
|
192
|
-
|
193
|
-
define_method name do |*args|
|
194
|
-
a = attachment_for(name)
|
195
|
-
(args.length > 0) ? a.to_s(args.first) : a
|
196
|
-
end
|
197
|
-
|
198
|
-
define_method "#{name}=" do |file|
|
199
|
-
attachment_for(name).assign(file)
|
200
|
-
end
|
201
|
-
|
202
|
-
define_method "#{name}?" do
|
203
|
-
attachment_for(name).file?
|
204
|
-
end
|
205
|
-
|
206
|
-
validates_each(name) do |record, attr, value|
|
207
|
-
attachment = record.attachment_for(name)
|
208
|
-
attachment.send(:flush_errors)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
# Returns the attachment definitions defined by each call to
|
213
|
-
# has_attached_file.
|
214
|
-
def attachment_definitions
|
215
|
-
self.attachment_definitions
|
198
|
+
HasAttachedFile.define_on(self, name, options)
|
216
199
|
end
|
217
200
|
end
|
218
201
|
end
|
@@ -220,10 +203,13 @@ end
|
|
220
203
|
# This stuff needs to be run after Paperclip is defined.
|
221
204
|
require 'paperclip/io_adapters/registry'
|
222
205
|
require 'paperclip/io_adapters/abstract_adapter'
|
206
|
+
require 'paperclip/io_adapters/empty_string_adapter'
|
223
207
|
require 'paperclip/io_adapters/identity_adapter'
|
224
208
|
require 'paperclip/io_adapters/file_adapter'
|
225
209
|
require 'paperclip/io_adapters/stringio_adapter'
|
210
|
+
require 'paperclip/io_adapters/data_uri_adapter'
|
226
211
|
require 'paperclip/io_adapters/nil_adapter'
|
227
212
|
require 'paperclip/io_adapters/attachment_adapter'
|
228
213
|
require 'paperclip/io_adapters/uploaded_file_adapter'
|
229
214
|
require 'paperclip/io_adapters/uri_adapter'
|
215
|
+
require 'paperclip/io_adapters/http_url_proxy_adapter'
|