paperclip 3.4.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.gitignore +0 -6
  5. data/.hound.yml +1055 -0
  6. data/.rubocop.yml +1 -0
  7. data/.travis.yml +19 -12
  8. data/Appraisals +4 -11
  9. data/CONTRIBUTING.md +29 -13
  10. data/Gemfile +13 -4
  11. data/LICENSE +1 -3
  12. data/MIGRATING-ES.md +317 -0
  13. data/MIGRATING.md +375 -0
  14. data/NEWS +390 -71
  15. data/README.md +607 -152
  16. data/RELEASING.md +17 -0
  17. data/Rakefile +6 -8
  18. data/UPGRADING +12 -9
  19. data/features/basic_integration.feature +34 -21
  20. data/features/migration.feature +0 -24
  21. data/features/rake_tasks.feature +2 -3
  22. data/features/step_definitions/attachment_steps.rb +44 -36
  23. data/features/step_definitions/html_steps.rb +2 -2
  24. data/features/step_definitions/rails_steps.rb +125 -26
  25. data/features/step_definitions/s3_steps.rb +3 -3
  26. data/features/step_definitions/web_steps.rb +1 -103
  27. data/features/support/env.rb +3 -2
  28. data/features/support/fakeweb.rb +4 -1
  29. data/features/support/file_helpers.rb +12 -2
  30. data/features/support/fixtures/gemfile.txt +1 -1
  31. data/features/support/paths.rb +1 -1
  32. data/features/support/rails.rb +4 -11
  33. data/gemfiles/4.2.gemfile +17 -0
  34. data/gemfiles/5.0.gemfile +17 -0
  35. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  36. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  37. data/lib/paperclip/attachment.rb +215 -82
  38. data/lib/paperclip/attachment_registry.rb +60 -0
  39. data/lib/paperclip/callbacks.rb +13 -1
  40. data/lib/paperclip/content_type_detector.rb +48 -24
  41. data/lib/paperclip/errors.rb +8 -1
  42. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  43. data/lib/paperclip/filename_cleaner.rb +15 -0
  44. data/lib/paperclip/geometry_detector_factory.rb +12 -5
  45. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  46. data/lib/paperclip/glue.rb +1 -2
  47. data/lib/paperclip/has_attached_file.rb +115 -0
  48. data/lib/paperclip/helpers.rb +15 -20
  49. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  50. data/lib/paperclip/interpolations.rb +36 -14
  51. data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
  52. data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
  53. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  54. data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
  55. data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
  56. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  57. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  58. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  59. data/lib/paperclip/io_adapters/registry.rb +6 -2
  60. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  61. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +11 -7
  62. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  63. data/lib/paperclip/locales/en.yml +1 -0
  64. data/lib/paperclip/logger.rb +1 -1
  65. data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
  66. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  67. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
  68. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  69. data/lib/paperclip/matchers.rb +1 -1
  70. data/lib/paperclip/media_type_spoof_detector.rb +93 -0
  71. data/lib/paperclip/missing_attachment_styles.rb +11 -16
  72. data/lib/paperclip/processor.rb +15 -43
  73. data/lib/paperclip/processor_helpers.rb +50 -0
  74. data/lib/paperclip/rails_environment.rb +25 -0
  75. data/lib/paperclip/schema.rb +10 -8
  76. data/lib/paperclip/storage/filesystem.rb +20 -5
  77. data/lib/paperclip/storage/fog.rb +49 -23
  78. data/lib/paperclip/storage/s3.rb +153 -82
  79. data/lib/paperclip/style.rb +8 -3
  80. data/lib/paperclip/tempfile_factory.rb +6 -4
  81. data/lib/paperclip/thumbnail.rb +35 -19
  82. data/lib/paperclip/url_generator.rb +26 -14
  83. data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
  84. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  85. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  86. data/lib/paperclip/validators/attachment_presence_validator.rb +12 -8
  87. data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
  88. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
  89. data/lib/paperclip/validators.rb +31 -3
  90. data/lib/paperclip/version.rb +3 -1
  91. data/lib/paperclip.rb +41 -55
  92. data/lib/tasks/paperclip.rake +56 -9
  93. data/paperclip.gemspec +18 -17
  94. data/shoulda_macros/paperclip.rb +13 -3
  95. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  96. data/spec/paperclip/attachment_processing_spec.rb +79 -0
  97. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  98. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
  99. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  100. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  101. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  102. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  103. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  104. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  105. data/spec/paperclip/glue_spec.rb +44 -0
  106. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  107. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +179 -199
  108. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
  109. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  110. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
  111. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  112. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  113. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  114. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
  115. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  116. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  117. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  118. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  119. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  120. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
  121. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  122. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  123. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  124. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  125. data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
  126. data/spec/paperclip/meta_class_spec.rb +30 -0
  127. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  128. data/spec/paperclip/paperclip_spec.rb +192 -0
  129. data/spec/paperclip/plural_cache_spec.rb +37 -0
  130. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  131. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  132. data/spec/paperclip/rails_environment_spec.rb +33 -0
  133. data/spec/paperclip/rake_spec.rb +103 -0
  134. data/spec/paperclip/schema_spec.rb +248 -0
  135. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  136. data/spec/paperclip/storage/fog_spec.rb +566 -0
  137. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  138. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  139. data/spec/paperclip/style_spec.rb +254 -0
  140. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  141. data/spec/paperclip/tempfile_spec.rb +35 -0
  142. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +186 -141
  143. data/spec/paperclip/url_generator_spec.rb +221 -0
  144. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  145. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  146. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  147. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +87 -59
  148. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  149. data/spec/paperclip/validators_spec.rb +164 -0
  150. data/spec/spec_helper.rb +46 -0
  151. data/spec/support/assertions.rb +82 -0
  152. data/spec/support/fake_model.rb +25 -0
  153. data/spec/support/fake_rails.rb +12 -0
  154. data/spec/support/fixtures/empty.html +1 -0
  155. data/spec/support/fixtures/empty.xlsx +0 -0
  156. data/spec/support/fixtures/spaced file.jpg +0 -0
  157. data/spec/support/matchers/accept.rb +5 -0
  158. data/spec/support/matchers/exist.rb +5 -0
  159. data/spec/support/matchers/have_column.rb +23 -0
  160. data/{test → spec}/support/mock_attachment.rb +2 -0
  161. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  162. data/spec/support/model_reconstruction.rb +68 -0
  163. data/spec/support/reporting.rb +11 -0
  164. data/spec/support/test_data.rb +13 -0
  165. data/spec/support/version_helper.rb +9 -0
  166. metadata +395 -346
  167. data/Gemfile.lock +0 -200
  168. data/RUNNING_TESTS.md +0 -4
  169. data/cucumber/paperclip_steps.rb +0 -6
  170. data/gemfiles/3.0.gemfile +0 -11
  171. data/gemfiles/3.1.gemfile +0 -11
  172. data/gemfiles/3.2.gemfile +0 -11
  173. data/lib/paperclip/attachment_options.rb +0 -9
  174. data/lib/paperclip/instance_methods.rb +0 -35
  175. data/test/attachment_options_test.rb +0 -27
  176. data/test/attachment_processing_test.rb +0 -29
  177. data/test/content_type_detector_test.rb +0 -40
  178. data/test/file_command_content_type_detector_test.rb +0 -25
  179. data/test/generator_test.rb +0 -80
  180. data/test/geometry_detector_test.rb +0 -24
  181. data/test/helper.rb +0 -199
  182. data/test/io_adapters/abstract_adapter_test.rb +0 -50
  183. data/test/io_adapters/file_adapter_test.rb +0 -100
  184. data/test/io_adapters/identity_adapter_test.rb +0 -8
  185. data/test/io_adapters/stringio_adapter_test.rb +0 -51
  186. data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
  187. data/test/io_adapters/uri_adapter_test.rb +0 -86
  188. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  189. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  190. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -47
  191. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  192. data/test/meta_class_test.rb +0 -32
  193. data/test/paperclip_missing_attachment_styles_test.rb +0 -94
  194. data/test/paperclip_test.rb +0 -259
  195. data/test/schema_test.rb +0 -200
  196. data/test/storage/fog_test.rb +0 -453
  197. data/test/storage/s3_live_test.rb +0 -179
  198. data/test/storage/s3_test.rb +0 -1236
  199. data/test/style_test.rb +0 -213
  200. data/test/support/mock_model.rb +0 -2
  201. data/test/tempfile_factory_test.rb +0 -13
  202. data/test/url_generator_test.rb +0 -187
  203. data/test/validators/attachment_content_type_validator_test.rb +0 -292
  204. data/test/validators_test.rb +0 -25
  205. /data/{test → spec}/database.yml +0 -0
  206. /data/{test → spec/support}/fixtures/12k.png +0 -0
  207. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  208. /data/{test → spec/support}/fixtures/5k.png +0 -0
  209. /data/{test → spec/support}/fixtures/animated +0 -0
  210. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  211. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  212. /data/{test → spec/support}/fixtures/bad.png +0 -0
  213. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  214. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  215. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  216. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  217. /data/{test → spec/support}/fixtures/text.txt +0 -0
  218. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  219. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  220. /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, attachment_options)
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
- escape_url_as_needed(
12
- timestamp_as_needed(
13
- @attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
14
- options
15
- ), options)
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 @attachment_options[:default_url].respond_to?(:call)
23
- @attachment_options[:default_url].call(@attachment)
24
- elsif @attachment_options[:default_url].is_a?(Symbol)
25
- @attachment.instance.send(@attachment_options[:default_url])
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
- @attachment_options[:default_url]
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
- @attachment_options[:url]
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
- (url.respond_to?(:escape) ? url.escape : URI.escape(url)).gsub(/(\/.+)\?(.+\.)/, '\1%3F\2')
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 ActiveRecord-style validations on the content type of the file
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
- validates_with AttachmentContentTypeValidator, _merge_attributes(attr_names)
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::Validations::PresenceValidator
6
- def validate(record)
7
- [attributes].flatten.map do |attribute|
8
- if record.send(:read_attribute_for_validation, "#{attribute}_file_name").blank?
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 ActiveRecord-style validations on the presence of a file.
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
- validates_with AttachmentPresenceValidator, _merge_attributes(attr_names)
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
- record.errors.add(attr_name, error_message_key, filtered_options(value).merge(
25
- :min => min_value_in_human_size(record),
26
- :max => max_value_in_human_size(record),
27
- :count => human_size(option_value)
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
- storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
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 ActiveRecord-style validations on the size of the file assigned. The
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
- validates_with AttachmentSizeValidator, _merge_attributes(attr_names)
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
@@ -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 =~ /^Attachment(.+)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
- options[:"attachment_#{validator_kind}"] = options.delete(validator_kind)
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
- validates(*attributes + [options])
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
@@ -1,3 +1,5 @@
1
1
  module Paperclip
2
- VERSION = "3.4.0" unless defined? Paperclip::VERSION
2
+ unless defined?(Paperclip::VERSION)
3
+ VERSION = "6.1.0".freeze
4
+ end
3
5
  end
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 'mime/types'
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 'cocaine'
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
- :whiny => true,
78
- :image_magick_path => nil,
79
- :command_path => nil,
80
- :log => true,
81
- :log_command => true,
82
- :swallow_stderr => true
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 publically accessible. This can just
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
- # Defaults to +false+.#
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
- include InstanceMethods
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'