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,6 +1,7 @@
1
- # encoding: utf-8
2
1
  require 'uri'
3
2
  require 'paperclip/url_generator'
3
+ require 'active_support/deprecation'
4
+ require 'active_support/core_ext/string/inflections'
4
5
 
5
6
  module Paperclip
6
7
  # The Attachment class manages the files for a given attachment. It saves
@@ -14,6 +15,7 @@ module Paperclip
14
15
  :default_url => "/:attachment/:style/missing.png",
15
16
  :escape_url => true,
16
17
  :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,
18
+ :filename_cleaner => nil,
17
19
  :hash_data => ":class/:attachment/:id/:style/:updated_at",
18
20
  :hash_digest => "SHA1",
19
21
  :interpolator => Paperclip::Interpolations,
@@ -28,16 +30,19 @@ module Paperclip
28
30
  :url_generator => Paperclip::UrlGenerator,
29
31
  :use_default_time_zone => true,
30
32
  :use_timestamp => true,
31
- :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
33
+ :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
34
+ :validate_media_type => true,
35
+ :adapter_options => { hash_digest: Digest::MD5 },
36
+ :check_validity_before_processing => true
32
37
  }
33
38
  end
34
39
 
35
40
  attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny,
36
- :options, :interpolator, :source_file_options, :whiny
41
+ :options, :interpolator, :source_file_options
37
42
  attr_accessor :post_processing
38
43
 
39
44
  # Creates an Attachment object. +name+ is the name of the attachment,
40
- # +instance+ is the ActiveRecord object instance it's attached to, and
45
+ # +instance+ is the model object instance it's attached to, and
41
46
  # +options+ is the same as the hash passed to +has_attached_file+.
42
47
  #
43
48
  # Options include:
@@ -45,7 +50,8 @@ module Paperclip
45
50
  # +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
46
51
  # +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
47
52
  # +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
48
- # +only_process+ - style args to be run through the post-processor. This defaults to the empty list
53
+ # +only_process+ - style args to be run through the post-processor. This defaults to the empty list (which is
54
+ # a special case that indicates all styles should be processed)
49
55
  # +default_url+ - a URL for the missing image
50
56
  # +default_style+ - the style to use when an argument is not specified e.g. #url, #path
51
57
  # +storage+ - the storage mechanism. Defaults to :filesystem
@@ -59,14 +65,16 @@ module Paperclip
59
65
  # +source_file_options+ - flags passed to the +convert+ command that controls how the file is read
60
66
  # +processors+ - classes that transform the attachment. Defaults to [:thumbnail]
61
67
  # +preserve_files+ - whether to keep files on the filesystem when deleting or clearing the attachment. Defaults to false
68
+ # +filename_cleaner+ - An object that responds to #call(filename) that will strip unacceptable charcters from filename
62
69
  # +interpolator+ - the object used to interpolate filenames and URLs. Defaults to Paperclip::Interpolations
63
70
  # +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
64
71
  # +escape_url+ - Perform URI escaping to URLs. Defaults to true
65
72
  def initialize(name, instance, options = {})
66
- @name = name
73
+ @name = name.to_sym
74
+ @name_string = name.to_s
67
75
  @instance = instance
68
76
 
69
- options = self.class.default_options.merge(options)
77
+ options = self.class.default_options.deep_merge(options)
70
78
 
71
79
  @options = options
72
80
  @post_processing = true
@@ -75,7 +83,7 @@ module Paperclip
75
83
  @errors = {}
76
84
  @dirty = false
77
85
  @interpolator = options[:interpolator]
78
- @url_generator = options[:url_generator].new(self, @options)
86
+ @url_generator = options[:url_generator].new(self)
79
87
  @source_file_options = options[:source_file_options]
80
88
  @whiny = options[:whiny]
81
89
 
@@ -83,33 +91,30 @@ module Paperclip
83
91
  end
84
92
 
85
93
  # What gets called when you call instance.attachment = File. It clears
86
- # errors, assigns attributes, and processes the file. It
87
- # also queues up the previous file for deletion, to be flushed away on
88
- # #save of its host. In addition to form uploads, you can also assign
89
- # another Paperclip attachment:
94
+ # errors, assigns attributes, and processes the file. It also queues up the
95
+ # previous file for deletion, to be flushed away on #save of its host. In
96
+ # addition to form uploads, you can also assign another Paperclip
97
+ # attachment:
90
98
  # new_user.avatar = old_user.avatar
91
- def assign uploaded_file
99
+ def assign(uploaded_file)
100
+ @file = Paperclip.io_adapters.for(uploaded_file,
101
+ @options[:adapter_options])
92
102
  ensure_required_accessors!
93
- file = Paperclip.io_adapters.for(uploaded_file)
103
+ ensure_required_validations!
94
104
 
95
- self.clear(*only_process)
96
- return nil if file.nil?
105
+ if @file.assignment?
106
+ clear(*only_process)
97
107
 
98
- @queued_for_write[:original] = file
99
- instance_write(:file_name, cleanup_filename(file.original_filename))
100
- instance_write(:content_type, file.content_type.to_s.strip)
101
- instance_write(:file_size, file.size)
102
- instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
103
- instance_write(:created_at, Time.now) if has_enabled_but_unset_created_at?
104
- instance_write(:updated_at, Time.now)
105
-
106
- @dirty = true
107
-
108
- post_process(*only_process) if post_processing && valid_assignment?
109
-
110
- # Reset the file size if the original file was reprocessed.
111
- instance_write(:file_size, @queued_for_write[:original].size)
112
- instance_write(:fingerprint, @queued_for_write[:original].fingerprint) if instance_respond_to?(:fingerprint)
108
+ if @file.nil?
109
+ nil
110
+ else
111
+ assign_attributes
112
+ post_process_file
113
+ reset_file_if_original_reprocessed
114
+ end
115
+ else
116
+ nil
117
+ end
113
118
  end
114
119
 
115
120
  # Returns the public URL of the attachment with a given style. This does
@@ -134,9 +139,8 @@ module Paperclip
134
139
  #
135
140
  # +#new(Paperclip::Attachment, options_hash)+
136
141
  # +#for(style_name, options_hash)+
137
- def url(style_name = default_style, options = {})
138
- default_options = {:timestamp => @options[:use_timestamp], :escape => @options[:escape_url]}
139
142
 
143
+ def url(style_name = default_style, options = {})
140
144
  if options == true || options == false # Backwards compatibility.
141
145
  @url_generator.for(style_name, default_options.merge(:timestamp => options))
142
146
  else
@@ -144,6 +148,20 @@ module Paperclip
144
148
  end
145
149
  end
146
150
 
151
+ def default_options
152
+ {
153
+ :timestamp => @options[:use_timestamp],
154
+ :escape => @options[:escape_url]
155
+ }
156
+ end
157
+
158
+ # Alias to +url+ that allows using the expiring_url method provided by the cloud
159
+ # storage implementations, but keep using filesystem storage for development and
160
+ # testing.
161
+ def expiring_url(time = 3600, style_name = default_style)
162
+ url(style_name)
163
+ end
164
+
147
165
  # Returns the path of the attachment as defined by the :path option. If the
148
166
  # file is stored in the filesystem the path refers to the path of the file
149
167
  # on disk. If the file is stored in S3, the path is the "key" part of the
@@ -153,6 +171,18 @@ module Paperclip
153
171
  path.respond_to?(:unescape) ? path.unescape : path
154
172
  end
155
173
 
174
+ # :nodoc:
175
+ def staged_path(style_name = default_style)
176
+ if staged?
177
+ @queued_for_write[style_name].path
178
+ end
179
+ end
180
+
181
+ # :nodoc:
182
+ def staged?
183
+ ! @queued_for_write.empty?
184
+ end
185
+
156
186
  # Alias to +url+
157
187
  def to_s style_name = default_style
158
188
  url(style_name)
@@ -209,6 +239,10 @@ module Paperclip
209
239
  # the instance's errors and returns false, cancelling the save.
210
240
  def save
211
241
  flush_deletes unless @options[:keep_old_files]
242
+ process = only_process
243
+ if process.any? && !process.include?(:original)
244
+ @queued_for_write.except!(:original)
245
+ end
212
246
  flush_writes
213
247
  @dirty = false
214
248
  true
@@ -231,10 +265,8 @@ module Paperclip
231
265
  # nil to the attachment *and saving*. This is permanent. If you wish to
232
266
  # wipe out the existing attachment but not save, use #clear.
233
267
  def destroy
234
- unless @options[:preserve_files]
235
- clear
236
- save
237
- end
268
+ clear
269
+ save
238
270
  end
239
271
 
240
272
  # Returns the uploaded file if present.
@@ -297,12 +329,23 @@ module Paperclip
297
329
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
298
330
  end
299
331
 
300
- # This method really shouldn't be called that often. It's expected use is
332
+ # This method really shouldn't be called that often. Its expected use is
301
333
  # in the paperclip:refresh rake task and that's it. It will regenerate all
302
334
  # thumbnails forcefully, by reobtaining the original file and going through
303
335
  # the post-process again.
336
+ # NOTE: Calling reprocess WILL NOT delete existing files. This is due to
337
+ # inconsistencies in timing of S3 commands. It's possible that calling
338
+ # #reprocess! will lose data if the files are not kept.
304
339
  def reprocess!(*style_args)
305
- saved_only_process, @options[:only_process] = @options[:only_process], style_args
340
+ saved_flags = @options.slice(
341
+ :only_process,
342
+ :preserve_files,
343
+ :check_validity_before_processing
344
+ )
345
+ @options[:only_process] = style_args
346
+ @options[:preserve_files] = true
347
+ @options[:check_validity_before_processing] = false
348
+
306
349
  begin
307
350
  assign(self)
308
351
  save
@@ -311,13 +354,13 @@ module Paperclip
311
354
  warn "#{e} - skipping file."
312
355
  false
313
356
  ensure
314
- @options[:only_process] = saved_only_process
357
+ @options.merge!(saved_flags)
315
358
  end
316
359
  end
317
360
 
318
361
  # Returns true if a file has been assigned.
319
362
  def file?
320
- !original_filename.blank?
363
+ original_filename.present?
321
364
  end
322
365
 
323
366
  alias :present? :file?
@@ -336,7 +379,7 @@ module Paperclip
336
379
  # instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's
337
380
  # "avatar_file_name" field (assuming the attachment is called avatar).
338
381
  def instance_write(attr, value)
339
- setter = :"#{name}_#{attr}="
382
+ setter = :"#{@name_string}_#{attr}="
340
383
  if instance.respond_to?(setter)
341
384
  instance.send(setter, value)
342
385
  end
@@ -345,7 +388,7 @@ module Paperclip
345
388
  # Reads the attachment-specific attribute on the instance. See instance_write
346
389
  # for more details.
347
390
  def instance_read(attr)
348
- getter = :"#{name}_#{attr}"
391
+ getter = :"#{@name_string}_#{attr}"
349
392
  if instance.respond_to?(getter)
350
393
  instance.send(getter)
351
394
  end
@@ -357,10 +400,24 @@ module Paperclip
357
400
  @options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path]
358
401
  end
359
402
 
403
+ def active_validator_classes
404
+ @instance.class.validators.map(&:class)
405
+ end
406
+
407
+ def missing_required_validator?
408
+ (active_validator_classes.flat_map(&:ancestors) & Paperclip::REQUIRED_VALIDATORS).empty?
409
+ end
410
+
411
+ def ensure_required_validations!
412
+ if missing_required_validator?
413
+ raise Paperclip::Errors::MissingRequiredValidatorError
414
+ end
415
+ end
416
+
360
417
  def ensure_required_accessors! #:nodoc:
361
418
  %w(file_name).each do |field|
362
- unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
363
- raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{name}_#{field}'")
419
+ unless @instance.respond_to?("#{@name_string}_#{field}") && @instance.respond_to?("#{@name_string}_#{field}=")
420
+ raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{@name_string}_#{field}'")
364
421
  end
365
422
  end
366
423
  end
@@ -369,16 +426,6 @@ module Paperclip
369
426
  Paperclip.log(message)
370
427
  end
371
428
 
372
- def valid_assignment? #:nodoc:
373
- if instance.valid?
374
- true
375
- else
376
- instance.errors.none? do |attr, message|
377
- attr.to_s.start_with?(@name.to_s)
378
- end
379
- end
380
- end
381
-
382
429
  def initialize_storage #:nodoc:
383
430
  storage_class_name = @options[:storage].to_s.downcase.camelize
384
431
  begin
@@ -389,19 +436,73 @@ module Paperclip
389
436
  self.extend(storage_module)
390
437
  end
391
438
 
392
- def extra_options_for(style) #:nodoc:
393
- all_options = @options[:convert_options][:all]
394
- all_options = all_options.call(instance) if all_options.respond_to?(:call)
395
- style_options = @options[:convert_options][style]
396
- style_options = style_options.call(instance) if style_options.respond_to?(:call)
439
+ def assign_attributes
440
+ @queued_for_write[:original] = @file
441
+ assign_file_information
442
+ assign_fingerprint { @file.fingerprint }
443
+ assign_timestamps
444
+ end
397
445
 
398
- [ style_options, all_options ].compact.join(" ")
446
+ def assign_file_information
447
+ instance_write(:file_name, cleanup_filename(@file.original_filename))
448
+ instance_write(:content_type, @file.content_type.to_s.strip)
449
+ instance_write(:file_size, @file.size)
450
+ end
451
+
452
+ def assign_fingerprint
453
+ if instance_respond_to?(:fingerprint)
454
+ instance_write(:fingerprint, yield)
455
+ end
456
+ end
457
+
458
+ def assign_timestamps
459
+ if has_enabled_but_unset_created_at?
460
+ instance_write(:created_at, Time.now)
461
+ end
462
+
463
+ instance_write(:updated_at, Time.now)
464
+ end
465
+
466
+ def post_process_file
467
+ dirty!
468
+
469
+ if post_processing
470
+ post_process(*only_process)
471
+ end
472
+ end
473
+
474
+ def dirty!
475
+ @dirty = true
476
+ end
477
+
478
+ def reset_file_if_original_reprocessed
479
+ instance_write(:file_size, @queued_for_write[:original].size)
480
+ assign_fingerprint { @queued_for_write[:original].fingerprint }
481
+ reset_updater
482
+ end
483
+
484
+ def reset_updater
485
+ if instance.respond_to?(updater)
486
+ instance.send(updater)
487
+ end
488
+ end
489
+
490
+ def updater
491
+ :"#{name}_file_name_will_change!"
492
+ end
493
+
494
+ def extra_options_for(style) #:nodoc:
495
+ process_options(:convert_options, style)
399
496
  end
400
497
 
401
498
  def extra_source_file_options_for(style) #:nodoc:
402
- all_options = @options[:source_file_options][:all]
499
+ process_options(:source_file_options, style)
500
+ end
501
+
502
+ def process_options(options_type, style) #:nodoc:
503
+ all_options = @options[options_type][:all]
403
504
  all_options = all_options.call(instance) if all_options.respond_to?(:call)
404
- style_options = @options[:source_file_options][style]
505
+ style_options = @options[options_type][style]
405
506
  style_options = style_options.call(instance) if style_options.respond_to?(:call)
406
507
 
407
508
  [ style_options, all_options ].compact.join(" ")
@@ -412,7 +513,9 @@ module Paperclip
412
513
 
413
514
  instance.run_paperclip_callbacks(:post_process) do
414
515
  instance.run_paperclip_callbacks(:"#{name}_post_process") do
415
- post_process_styles(*style_args)
516
+ if !@options[:check_validity_before_processing] || !instance.errors.any?
517
+ post_process_styles(*style_args)
518
+ end
416
519
  end
417
520
  end
418
521
  end
@@ -427,13 +530,30 @@ module Paperclip
427
530
  def post_process_style(name, style) #:nodoc:
428
531
  begin
429
532
  raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
430
- @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
431
- Paperclip.processor(processor).make(file, style.processor_options, self)
533
+ intermediate_files = []
534
+ original = @queued_for_write[:original]
535
+
536
+ @queued_for_write[name] = style.processors.
537
+ reduce(original) do |file, processor|
538
+ file = Paperclip.processor(processor).make(file, style.processor_options, self)
539
+ intermediate_files << file unless file == @queued_for_write[:original]
540
+ # if we're processing the original, close + unlink the source tempfile
541
+ if name == :original
542
+ @queued_for_write[:original].close(true)
543
+ end
544
+ file
432
545
  end
433
- @queued_for_write[name] = Paperclip.io_adapters.for(@queued_for_write[name])
434
- rescue Paperclip::Error => e
546
+
547
+ unadapted_file = @queued_for_write[name]
548
+ @queued_for_write[name] = Paperclip.io_adapters.
549
+ for(@queued_for_write[name], @options[:adapter_options])
550
+ unadapted_file.close if unadapted_file.respond_to?(:close)
551
+ @queued_for_write[name]
552
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
435
553
  log("An error was received while processing: #{e.inspect}")
436
554
  (@errors[:processing] ||= []) << e.message if @options[:whiny]
555
+ ensure
556
+ unlink_files(intermediate_files)
437
557
  end
438
558
  end
439
559
 
@@ -452,10 +572,12 @@ module Paperclip
452
572
  end
453
573
 
454
574
  def queue_all_for_delete #:nodoc:
455
- return if @options[:preserve_files] || !file?
456
- @queued_for_delete += [:original, *styles.keys].uniq.map do |style|
457
- path(style) if exists?(style)
458
- end.compact
575
+ return if !file?
576
+ unless @options[:preserve_files]
577
+ @queued_for_delete += [:original, *styles.keys].uniq.map do |style|
578
+ path(style) if exists?(style)
579
+ end.compact
580
+ end
459
581
  instance_write(:file_name, nil)
460
582
  instance_write(:content_type, nil)
461
583
  instance_write(:file_size, nil)
@@ -470,22 +592,33 @@ module Paperclip
470
592
  end
471
593
  end
472
594
 
473
- # called by storage after the writes are flushed and before @queued_for_writes is cleared
595
+ # called by storage after the writes are flushed and before @queued_for_write is cleared
474
596
  def after_flush_writes
475
- @queued_for_write.each do |style, file|
476
- file.close unless file.closed?
477
- file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
478
- end
597
+ unlink_files(@queued_for_write.values)
479
598
  end
480
599
 
481
- def cleanup_filename(filename)
482
- if @options[:restricted_characters]
483
- filename.gsub(@options[:restricted_characters], '_')
484
- else
485
- filename
600
+ def unlink_files(files)
601
+ Array(files).each do |file|
602
+ file.close unless file.closed?
603
+
604
+ begin
605
+ file.unlink if file.respond_to?(:unlink)
606
+ rescue Errno::ENOENT
607
+ end
486
608
  end
487
609
  end
488
610
 
611
+ # You can either specifiy :restricted_characters or you can define your own
612
+ # :filename_cleaner object. This object needs to respond to #call and takes
613
+ # the filename that will be cleaned. It should return the cleaned filename.
614
+ def filename_cleaner
615
+ @options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
616
+ end
617
+
618
+ def cleanup_filename(filename)
619
+ filename_cleaner.call(filename)
620
+ end
621
+
489
622
  # Check if attachment database table has a created_at field
490
623
  def able_to_store_created_at?
491
624
  @instance.respond_to?("#{name}_created_at".to_sym)
@@ -0,0 +1,60 @@
1
+ require 'singleton'
2
+
3
+ module Paperclip
4
+ class AttachmentRegistry
5
+ include Singleton
6
+
7
+ def self.register(klass, attachment_name, attachment_options)
8
+ instance.register(klass, attachment_name, attachment_options)
9
+ end
10
+
11
+ def self.clear
12
+ instance.clear
13
+ end
14
+
15
+ def self.names_for(klass)
16
+ instance.names_for(klass)
17
+ end
18
+
19
+ def self.each_definition(&block)
20
+ instance.each_definition(&block)
21
+ end
22
+
23
+ def self.definitions_for(klass)
24
+ instance.definitions_for(klass)
25
+ end
26
+
27
+ def initialize
28
+ clear
29
+ end
30
+
31
+ def register(klass, attachment_name, attachment_options)
32
+ @attachments ||= {}
33
+ @attachments[klass] ||= {}
34
+ @attachments[klass][attachment_name] = attachment_options
35
+ end
36
+
37
+ def clear
38
+ @attachments = Hash.new { |h,k| h[k] = {} }
39
+ end
40
+
41
+ def names_for(klass)
42
+ @attachments[klass].keys
43
+ end
44
+
45
+ def each_definition
46
+ @attachments.each do |klass, attachments|
47
+ attachments.each do |name, options|
48
+ yield klass, name, options
49
+ end
50
+ end
51
+ end
52
+
53
+ def definitions_for(klass)
54
+ parent_classes = klass.ancestors.reverse
55
+ parent_classes.each_with_object({}) do |ancestor, inherited_definitions|
56
+ inherited_definitions.deep_merge! @attachments[ancestor]
57
+ end
58
+ end
59
+ end
60
+ end
@@ -7,7 +7,7 @@ module Paperclip
7
7
 
8
8
  module Defining
9
9
  def define_paperclip_callbacks(*callbacks)
10
- define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
10
+ define_callbacks(*[callbacks, { terminator: hasta_la_vista_baby }].flatten)
11
11
  callbacks.each do |callback|
12
12
  eval <<-end_callbacks
13
13
  def before_#{callback}(*args, &blk)
@@ -19,6 +19,18 @@ module Paperclip
19
19
  end_callbacks
20
20
  end
21
21
  end
22
+
23
+ private
24
+
25
+ def hasta_la_vista_baby
26
+ lambda do |_, result|
27
+ if result.respond_to?(:call)
28
+ result.call == false
29
+ else
30
+ result == false
31
+ end
32
+ end
33
+ end
22
34
  end
23
35
 
24
36
  module Running
@@ -1,56 +1,80 @@
1
1
  module Paperclip
2
2
  class ContentTypeDetector
3
+ # The content-type detection strategy is as follows:
4
+ #
5
+ # 1. Blank/Empty files: If there's no filepath or the file is empty,
6
+ # provide a sensible default (application/octet-stream or inode/x-empty)
7
+ #
8
+ # 2. Calculated match: Return the first result that is found by both the
9
+ # `file` command and MIME::Types.
10
+ #
11
+ # 3. Standard types: Return the first standard (without an x- prefix) entry
12
+ # in MIME::Types
13
+ #
14
+ # 4. Experimental types: If there were no standard types in MIME::Types
15
+ # list, try to return the first experimental one
16
+ #
17
+ # 5. Raw `file` command: Just use the output of the `file` command raw, or
18
+ # a sensible default. This is cached from Step 2.
19
+
3
20
  EMPTY_TYPE = "inode/x-empty"
4
21
  SENSIBLE_DEFAULT = "application/octet-stream"
5
22
 
6
- def initialize(filename)
7
- @filename = filename
23
+ def initialize(filepath)
24
+ @filepath = filepath
8
25
  end
9
26
 
27
+ # Returns a String describing the file's content type
10
28
  def detect
11
- if blank?
29
+ if blank_name?
12
30
  SENSIBLE_DEFAULT
13
- elsif empty?
31
+ elsif empty_file?
14
32
  EMPTY_TYPE
15
- elsif !match?
16
- type_from_file_command
17
- elsif !multiple?
18
- possible_types.first
33
+ elsif calculated_type_matches.any?
34
+ calculated_type_matches.first
19
35
  else
20
- best_type_match
36
+ type_from_file_contents || SENSIBLE_DEFAULT
21
37
  end.to_s
22
38
  end
23
39
 
24
40
  private
25
41
 
26
- def empty?
27
- File.exists?(@filename) && File.size(@filename) == 0
42
+ def blank_name?
43
+ @filepath.nil? || @filepath.empty?
28
44
  end
29
45
 
30
- def blank?
31
- @filename.nil? || @filename.empty?
46
+ def empty_file?
47
+ File.exist?(@filepath) && File.size(@filepath) == 0
32
48
  end
33
49
 
34
- def possible_types
35
- @possible_types ||= MIME::Types.type_for(@filename)
50
+ alias :empty? :empty_file?
51
+
52
+ def calculated_type_matches
53
+ possible_types.select do |content_type|
54
+ content_type == type_from_file_contents
55
+ end
36
56
  end
37
57
 
38
- def match?
39
- possible_types.length > 0
58
+ def possible_types
59
+ MIME::Types.type_for(@filepath).collect(&:content_type)
40
60
  end
41
61
 
42
- def multiple?
43
- possible_types.length > 1
62
+ def type_from_file_contents
63
+ type_from_mime_magic || type_from_file_command
64
+ rescue Errno::ENOENT => e
65
+ Paperclip.log("Error while determining content type: #{e}")
66
+ SENSIBLE_DEFAULT
44
67
  end
45
68
 
46
- def best_type_match
47
- official_types = possible_types.reject {|type| type.content_type.match(/\/x-/) }
48
- (official_types.first || possible_types.first).content_type
69
+ def type_from_mime_magic
70
+ @type_from_mime_magic ||= File.open(@filepath) do |file|
71
+ MimeMagic.by_magic(file).try(:type)
72
+ end
49
73
  end
50
74
 
51
75
  def type_from_file_command
52
- FileCommandContentTypeDetector.new(@filename).detect
76
+ @type_from_file_command ||=
77
+ FileCommandContentTypeDetector.new(@filepath).detect
53
78
  end
54
-
55
79
  end
56
80
  end