paperclip 3.5.2 → 5.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +17 -0
  3. data/.gitignore +0 -6
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +19 -13
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +29 -13
  9. data/Gemfile +10 -7
  10. data/LICENSE +1 -3
  11. data/NEWS +226 -23
  12. data/README.md +494 -152
  13. data/RELEASING.md +17 -0
  14. data/Rakefile +6 -8
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +27 -8
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +36 -28
  19. data/features/step_definitions/html_steps.rb +2 -2
  20. data/features/step_definitions/rails_steps.rb +68 -37
  21. data/features/step_definitions/s3_steps.rb +2 -2
  22. data/features/step_definitions/web_steps.rb +1 -103
  23. data/features/support/env.rb +3 -2
  24. data/features/support/file_helpers.rb +2 -2
  25. data/features/support/fixtures/gemfile.txt +1 -1
  26. data/features/support/paths.rb +1 -1
  27. data/features/support/rails.rb +2 -25
  28. data/gemfiles/4.2.gemfile +17 -0
  29. data/gemfiles/5.0.gemfile +17 -0
  30. data/lib/generators/paperclip/paperclip_generator.rb +0 -2
  31. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  32. data/lib/paperclip/attachment.rb +160 -47
  33. data/lib/paperclip/attachment_registry.rb +4 -1
  34. data/lib/paperclip/callbacks.rb +13 -1
  35. data/lib/paperclip/content_type_detector.rb +26 -24
  36. data/lib/paperclip/errors.rb +8 -1
  37. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  38. data/lib/paperclip/geometry_detector_factory.rb +10 -3
  39. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  40. data/lib/paperclip/glue.rb +1 -1
  41. data/lib/paperclip/has_attached_file.rb +17 -1
  42. data/lib/paperclip/helpers.rb +14 -10
  43. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  44. data/lib/paperclip/interpolations.rb +27 -14
  45. data/lib/paperclip/io_adapters/abstract_adapter.rb +28 -4
  46. data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
  47. data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
  48. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  49. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  50. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -8
  51. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  53. data/lib/paperclip/io_adapters/registry.rb +6 -2
  54. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  55. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  56. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  57. data/lib/paperclip/locales/en.yml +1 -0
  58. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  59. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  60. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  61. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  62. data/lib/paperclip/media_type_spoof_detector.rb +89 -0
  63. data/lib/paperclip/processor.rb +5 -41
  64. data/lib/paperclip/processor_helpers.rb +50 -0
  65. data/lib/paperclip/rails_environment.rb +25 -0
  66. data/lib/paperclip/schema.rb +9 -7
  67. data/lib/paperclip/storage/filesystem.rb +14 -3
  68. data/lib/paperclip/storage/fog.rb +47 -22
  69. data/lib/paperclip/storage/s3.rb +144 -73
  70. data/lib/paperclip/style.rb +8 -2
  71. data/lib/paperclip/tempfile_factory.rb +6 -4
  72. data/lib/paperclip/thumbnail.rb +26 -14
  73. data/lib/paperclip/url_generator.rb +25 -14
  74. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  75. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  76. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  77. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  78. data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
  79. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  80. data/lib/paperclip/validators.rb +12 -3
  81. data/lib/paperclip/version.rb +3 -1
  82. data/lib/paperclip.rb +31 -11
  83. data/lib/tasks/paperclip.rake +34 -5
  84. data/paperclip.gemspec +18 -17
  85. data/shoulda_macros/paperclip.rb +13 -3
  86. data/{test → spec}/database.yml +0 -0
  87. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  88. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
  89. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  90. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +524 -400
  91. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -19
  92. data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +7 -6
  93. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  94. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  95. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  96. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  97. data/spec/paperclip/glue_spec.rb +44 -0
  98. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  99. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +141 -133
  100. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  101. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +101 -0
  102. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +38 -34
  103. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  104. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  105. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  106. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
  107. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  108. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  109. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  110. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  111. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  112. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
  113. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  114. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  115. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  116. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  117. data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
  118. data/spec/paperclip/meta_class_spec.rb +30 -0
  119. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  120. data/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +46 -71
  121. data/spec/paperclip/plural_cache_spec.rb +37 -0
  122. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  123. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +5 -5
  124. data/spec/paperclip/rails_environment_spec.rb +33 -0
  125. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  126. data/spec/paperclip/schema_spec.rb +248 -0
  127. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  128. data/spec/paperclip/storage/fog_spec.rb +561 -0
  129. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  130. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  131. data/spec/paperclip/style_spec.rb +255 -0
  132. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  133. data/spec/paperclip/tempfile_spec.rb +35 -0
  134. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +150 -131
  135. data/spec/paperclip/url_generator_spec.rb +222 -0
  136. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  137. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  138. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  139. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  140. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  141. data/spec/paperclip/validators_spec.rb +164 -0
  142. data/spec/spec_helper.rb +47 -0
  143. data/spec/support/assertions.rb +82 -0
  144. data/spec/support/conditional_filter_helper.rb +5 -0
  145. data/spec/support/fake_model.rb +25 -0
  146. data/spec/support/fake_rails.rb +12 -0
  147. data/{test → spec/support}/fixtures/12k.png +0 -0
  148. data/{test → spec/support}/fixtures/50x50.png +0 -0
  149. data/{test → spec/support}/fixtures/5k.png +0 -0
  150. data/{test → spec/support}/fixtures/animated +0 -0
  151. data/{test → spec/support}/fixtures/animated.gif +0 -0
  152. data/{test → spec/support}/fixtures/animated.unknown +0 -0
  153. data/{test → spec/support}/fixtures/bad.png +0 -0
  154. data/spec/support/fixtures/empty.html +1 -0
  155. data/spec/support/fixtures/empty.xlsx +0 -0
  156. data/{test → spec/support}/fixtures/fog.yml +0 -0
  157. data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  158. data/{test → spec/support}/fixtures/s3.yml +0 -0
  159. data/spec/support/fixtures/spaced file.jpg +0 -0
  160. data/{test → spec/support}/fixtures/spaced file.png +0 -0
  161. data/{test → spec/support}/fixtures/text.txt +0 -0
  162. data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  163. data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  164. data/spec/support/matchers/accept.rb +5 -0
  165. data/spec/support/matchers/exist.rb +5 -0
  166. data/spec/support/matchers/have_column.rb +23 -0
  167. data/{test → spec}/support/mock_attachment.rb +2 -0
  168. data/{test → spec}/support/mock_interpolator.rb +0 -0
  169. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  170. data/spec/support/model_reconstruction.rb +68 -0
  171. data/spec/support/reporting.rb +11 -0
  172. data/spec/support/test_data.rb +13 -0
  173. data/spec/support/version_helper.rb +9 -0
  174. metadata +262 -297
  175. data/RUNNING_TESTS.md +0 -4
  176. data/cucumber/paperclip_steps.rb +0 -6
  177. data/gemfiles/3.0.gemfile +0 -11
  178. data/gemfiles/3.1.gemfile +0 -11
  179. data/gemfiles/3.2.gemfile +0 -11
  180. data/gemfiles/4.0.gemfile +0 -11
  181. data/test/attachment_definitions_test.rb +0 -12
  182. data/test/attachment_registry_test.rb +0 -77
  183. data/test/filename_cleaner_test.rb +0 -14
  184. data/test/generator_test.rb +0 -80
  185. data/test/geometry_detector_test.rb +0 -24
  186. data/test/has_attached_file_test.rb +0 -125
  187. data/test/helper.rb +0 -215
  188. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  189. data/test/io_adapters/data_uri_adapter_test.rb +0 -67
  190. data/test/io_adapters/empty_string_adapter_test.rb +0 -17
  191. data/test/io_adapters/file_adapter_test.rb +0 -119
  192. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  193. data/test/io_adapters/identity_adapter_test.rb +0 -8
  194. data/test/io_adapters/uri_adapter_test.rb +0 -102
  195. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  196. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  197. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  198. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  199. data/test/meta_class_test.rb +0 -32
  200. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  201. data/test/plural_cache_test.rb +0 -36
  202. data/test/schema_test.rb +0 -200
  203. data/test/storage/fog_test.rb +0 -453
  204. data/test/storage/s3_live_test.rb +0 -179
  205. data/test/storage/s3_test.rb +0 -1348
  206. data/test/style_test.rb +0 -213
  207. data/test/support/mock_model.rb +0 -2
  208. data/test/tempfile_factory_test.rb +0 -13
  209. data/test/url_generator_test.rb +0 -187
  210. data/test/validators/attachment_content_type_validator_test.rb +0 -323
  211. data/test/validators_test.rb +0 -32
@@ -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] # this is not an option
32
- @file = file
33
+ geometry = options[:geometry].to_s
33
34
  @crop = geometry[-1,1] == '#'
34
- @target_geometry = (options[:string_geometry_parser] || Geometry).parse(geometry)
35
- @current_geometry = (options[:file_geometry_parser] || Geometry).from_file(@file)
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[:whiny].nil? ? true : options[:whiny]
39
+ @whiny = options.fetch(:whiny, true)
39
40
  @format = options[:format]
40
- @animated = options[:animated].nil? ? true : options[:animated]
41
- @auto_orient = options[:auto_orient].nil? ? true : options[:auto_orient]
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
- dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
68
- dst.binmode
68
+ filename = [@basename, @format ? ".#{@format}" : ""].join
69
+ dst = TempfileFactory.new.generate(filename)
69
70
 
70
71
  begin
71
72
  parameters = []
@@ -77,7 +78,12 @@ module Paperclip
77
78
 
78
79
  parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
79
80
 
80
- success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
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
+ )
81
87
  rescue Cocaine::ExitStatusError => e
82
88
  raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
83
89
  rescue Cocaine::CommandNotFoundError => e
@@ -102,14 +108,20 @@ module Paperclip
102
108
 
103
109
  protected
104
110
 
105
- # Return true if the format is animated
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
- ANIMATED_FORMATS.include? identify("-format %m :file", :file => "#{@file.path}[0]").to_s.downcase.strip
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
113
125
  rescue Cocaine::ExitStatusError => e
114
126
  raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
115
127
  rescue Cocaine::CommandNotFoundError => e
@@ -2,29 +2,32 @@ require 'uri'
2
2
 
3
3
  module Paperclip
4
4
  class UrlGenerator
5
- def initialize(attachment, attachment_options)
5
+ def initialize(attachment)
6
6
  @attachment = attachment
7
- @attachment_options = attachment_options
8
7
  end
9
8
 
10
9
  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)
10
+ interpolated = attachment_options[:interpolator].interpolate(
11
+ most_appropriate_url, @attachment, style_name
12
+ )
13
+
14
+ escaped = escape_url_as_needed(interpolated, options)
15
+ timestamp_as_needed(escaped, options)
16
16
  end
17
17
 
18
18
  private
19
19
 
20
+ attr_reader :attachment
21
+ delegate :options, to: :attachment, prefix: true
22
+
20
23
  # This method is all over the place.
21
24
  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])
25
+ if attachment_options[:default_url].respond_to?(:call)
26
+ attachment_options[:default_url].call(@attachment)
27
+ elsif attachment_options[:default_url].is_a?(Symbol)
28
+ @attachment.instance.send(attachment_options[:default_url])
26
29
  else
27
- @attachment_options[:default_url]
30
+ attachment_options[:default_url]
28
31
  end
29
32
  end
30
33
 
@@ -32,7 +35,7 @@ module Paperclip
32
35
  if @attachment.original_filename.nil?
33
36
  default_url
34
37
  else
35
- @attachment_options[:url]
38
+ attachment_options[:url]
36
39
  end
37
40
  end
38
41
 
@@ -58,7 +61,15 @@ module Paperclip
58
61
  end
59
62
 
60
63
  def escape_url(url)
61
- (url.respond_to?(:escape) ? url.escape : URI.escape(url)).gsub(/(\/.+)\?(.+\.)/, '\1%3F\2')
64
+ if url.respond_to?(:escape)
65
+ url.escape
66
+ else
67
+ URI.escape(url).gsub(escape_regex){|m| "%#{m.ord.to_s(16).upcase}" }
68
+ end
69
+ end
70
+
71
+ def escape_regex
72
+ /[\?\(\)\[\]\+]/
62
73
  end
63
74
  end
64
75
  end
@@ -6,6 +6,10 @@ 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)
10
14
  base_attribute = attribute.to_sym
11
15
  attribute = "#{attribute}_content_type".to_sym
@@ -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
+
@@ -8,6 +8,10 @@ module Paperclip
8
8
  record.errors.add(attribute, :blank, options)
9
9
  end
10
10
  end
11
+
12
+ def self.helper_method_name
13
+ :validates_attachment_presence
14
+ end
11
15
  end
12
16
 
13
17
  module HelperMethods
@@ -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
- storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
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
@@ -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,14 +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 =~ /^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
43
  validator_options = options.delete(validator_kind)
38
44
  validator_options = {} if validator_options == true
39
- local_options = attributes + [validator_options]
40
- send(:"validates_attachment_#{validator_kind}", *local_options)
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
41
50
  end
42
51
  end
43
52
  end
@@ -1,3 +1,5 @@
1
1
  module Paperclip
2
- VERSION = "3.5.2" unless defined? Paperclip::VERSION
2
+ unless defined?(Paperclip::VERSION)
3
+ VERSION = "5.2.1".freeze
4
+ end
3
5
  end
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 'mime/types'
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
70
  require 'cocaine'
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
- :whiny => true,
80
- :image_magick_path => nil,
81
- :command_path => nil,
82
- :log => true,
83
- :log_command => true,
84
- :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,
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 publically accessible. This can just
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"
@@ -129,8 +148,9 @@ module Paperclip
129
148
  # :default_style => :normal
130
149
  # user.avatar.url # => "/avatars/23/normal_me.png"
131
150
  # * +keep_old_files+: Keep the existing attachment files (original + resized) from
132
- # being automatically deleted when an attachment is cleared or updated.
133
- # Defaults to +false+.#
151
+ # being automatically deleted when an attachment is cleared or updated. Defaults to +false+.
152
+ # * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
153
+ # record is destroyed. Defaults to +false+.
134
154
  # * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
135
155
  # to a command line error. This will override the global setting for this attachment.
136
156
  # Defaults to true.
@@ -18,7 +18,7 @@ module Paperclip
18
18
  raise "Class #{klass.name} has no attachments specified"
19
19
  end
20
20
 
21
- if !name.blank? && attachment_names.map(&:to_s).include?(name.to_s)
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 Exception => e
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
- if file = Paperclip.io_adapters.for(instance.send(name))
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
- # Force loading all model classes to never miss any has_attached_file declaration:
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,42 +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 -- {test,spec,features}/*`.split("\n")
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
 
22
- if File.exists?('UPGRADING')
20
+ if File.exist?('UPGRADING')
23
21
  s.post_install_message = File.read("UPGRADING")
24
22
  end
25
23
 
26
24
  s.requirements << "ImageMagick"
27
- s.required_ruby_version = ">= 1.9.2"
25
+ s.required_ruby_version = ">= 2.1.0"
28
26
 
29
- s.add_dependency('activemodel', '>= 3.0.0')
30
- s.add_dependency('activesupport', '>= 3.0.0')
31
- s.add_dependency('cocaine', '~> 0.5.3')
27
+ s.add_dependency('activemodel', '>= 4.2.0')
28
+ s.add_dependency('activesupport', '>= 4.2.0')
29
+ s.add_dependency('cocaine', '~> 0.5.5')
32
30
  s.add_dependency('mime-types')
31
+ s.add_dependency('mimemagic', '~> 0.3.0')
33
32
 
34
- s.add_development_dependency('activerecord', '>= 3.0.0')
33
+ s.add_development_dependency('activerecord', '>= 4.2.0')
35
34
  s.add_development_dependency('shoulda')
35
+ s.add_development_dependency('rspec', '~> 3.0')
36
36
  s.add_development_dependency('appraisal')
37
37
  s.add_development_dependency('mocha')
38
- s.add_development_dependency('aws-sdk', '>= 1.5.7')
38
+ s.add_development_dependency('aws-sdk', '>= 2.3.0', '< 3.0')
39
39
  s.add_development_dependency('bourne')
40
- s.add_development_dependency('sqlite3', '~> 1.3.4')
41
- s.add_development_dependency('cucumber', '~> 1.2.1')
42
- 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')
43
43
  s.add_development_dependency('nokogiri')
44
- # Ruby version < 1.9.3 can't install capybara > 2.0.3.
45
- s.add_development_dependency('capybara', '= 2.0.3')
44
+ s.add_development_dependency('capybara')
46
45
  s.add_development_dependency('bundler')
47
- s.add_development_dependency('fog', '~> 1.0')
46
+ s.add_development_dependency('fog-aws')
47
+ s.add_development_dependency('fog-local')
48
48
  s.add_development_dependency('launchy')
49
49
  s.add_development_dependency('rake')
50
50
  s.add_development_dependency('fakeweb')
51
51
  s.add_development_dependency('railties')
52
- s.add_development_dependency('actionmailer', '>= 3.0.0')
52
+ s.add_development_dependency('generator_spec')
53
+ s.add_development_dependency('timecop')
53
54
  end
@@ -103,7 +103,11 @@ module Paperclip
103
103
  end
104
104
  end
105
105
 
106
- if defined?(ActionController::Integration::Session)
106
+ if defined?(ActionDispatch::Integration::Session)
107
+ class ActionDispatch::IntegrationTest::Session #:nodoc:
108
+ include Paperclip::Shoulda
109
+ end
110
+ elsif defined?(ActionController::Integration::Session)
107
111
  class ActionController::Integration::Session #:nodoc:
108
112
  include Paperclip::Shoulda
109
113
  end
@@ -119,6 +123,12 @@ else
119
123
  end
120
124
  end
121
125
 
122
- class Test::Unit::TestCase #:nodoc:
123
- extend Paperclip::Shoulda
126
+ if defined?(Minitest)
127
+ class Minitest::Unit::TestCase #:nodoc:
128
+ extend Paperclip::Shoulda
129
+ end
130
+ elsif defined?(Test)
131
+ class Test::Unit::TestCase #:nodoc:
132
+ extend Paperclip::Shoulda
133
+ end
124
134
  end