paperclip 4.2.2 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -15
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +19 -8
  9. data/Gemfile +5 -9
  10. data/LICENSE +1 -1
  11. data/MIGRATING-ES.md +317 -0
  12. data/MIGRATING.md +375 -0
  13. data/NEWS +184 -31
  14. data/README.md +371 -201
  15. data/RELEASING.md +17 -0
  16. data/Rakefile +2 -2
  17. data/UPGRADING +12 -9
  18. data/features/basic_integration.feature +10 -6
  19. data/features/migration.feature +0 -24
  20. data/features/step_definitions/attachment_steps.rb +41 -35
  21. data/features/step_definitions/html_steps.rb +2 -2
  22. data/features/step_definitions/rails_steps.rb +39 -38
  23. data/features/step_definitions/s3_steps.rb +2 -2
  24. data/features/step_definitions/web_steps.rb +1 -103
  25. data/features/support/env.rb +1 -0
  26. data/features/support/file_helpers.rb +2 -2
  27. data/features/support/paths.rb +1 -1
  28. data/features/support/rails.rb +0 -24
  29. data/gemfiles/4.2.gemfile +6 -8
  30. data/gemfiles/5.0.gemfile +17 -0
  31. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  32. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  33. data/lib/paperclip/attachment.rb +51 -26
  34. data/lib/paperclip/attachment_registry.rb +3 -2
  35. data/lib/paperclip/callbacks.rb +8 -6
  36. data/lib/paperclip/content_type_detector.rb +27 -11
  37. data/lib/paperclip/errors.rb +3 -1
  38. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  39. data/lib/paperclip/filename_cleaner.rb +0 -1
  40. data/lib/paperclip/geometry_detector_factory.rb +3 -3
  41. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  42. data/lib/paperclip/glue.rb +1 -1
  43. data/lib/paperclip/has_attached_file.rb +9 -2
  44. data/lib/paperclip/helpers.rb +15 -11
  45. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  46. data/lib/paperclip/interpolations.rb +24 -14
  47. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  48. data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
  49. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  50. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  51. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  53. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  54. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  55. data/lib/paperclip/io_adapters/registry.rb +6 -2
  56. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  57. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  58. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  59. data/lib/paperclip/logger.rb +1 -1
  60. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  61. data/lib/paperclip/media_type_spoof_detector.rb +13 -9
  62. data/lib/paperclip/processor.rb +15 -6
  63. data/lib/paperclip/rails_environment.rb +25 -0
  64. data/lib/paperclip/schema.rb +4 -10
  65. data/lib/paperclip/storage/filesystem.rb +13 -2
  66. data/lib/paperclip/storage/fog.rb +33 -20
  67. data/lib/paperclip/storage/s3.rb +89 -70
  68. data/lib/paperclip/style.rb +0 -1
  69. data/lib/paperclip/thumbnail.rb +24 -12
  70. data/lib/paperclip/url_generator.rb +17 -13
  71. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  72. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
  73. data/lib/paperclip/validators.rb +1 -1
  74. data/lib/paperclip/version.rb +3 -1
  75. data/lib/paperclip.rb +27 -13
  76. data/lib/tasks/paperclip.rake +33 -3
  77. data/paperclip.gemspec +18 -15
  78. data/spec/paperclip/attachment_definitions_spec.rb +1 -1
  79. data/spec/paperclip/attachment_processing_spec.rb +2 -5
  80. data/spec/paperclip/attachment_registry_spec.rb +84 -13
  81. data/spec/paperclip/attachment_spec.rb +147 -41
  82. data/spec/paperclip/content_type_detector_spec.rb +9 -2
  83. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -2
  84. data/spec/paperclip/filename_cleaner_spec.rb +0 -1
  85. data/spec/paperclip/geometry_spec.rb +1 -1
  86. data/spec/paperclip/glue_spec.rb +44 -0
  87. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  88. data/spec/paperclip/integration_spec.rb +42 -5
  89. data/spec/paperclip/interpolations_spec.rb +21 -9
  90. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +106 -23
  91. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  92. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  93. data/spec/paperclip/io_adapters/file_adapter_spec.rb +6 -3
  94. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +51 -14
  95. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  96. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  97. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +5 -1
  98. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  99. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +126 -8
  100. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  101. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
  102. data/spec/paperclip/media_type_spoof_detector_spec.rb +75 -11
  103. data/spec/paperclip/paperclip_spec.rb +15 -40
  104. data/spec/paperclip/plural_cache_spec.rb +17 -16
  105. data/spec/paperclip/processor_spec.rb +4 -4
  106. data/spec/paperclip/rails_environment_spec.rb +33 -0
  107. data/spec/paperclip/schema_spec.rb +46 -46
  108. data/spec/paperclip/storage/fog_spec.rb +63 -3
  109. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  110. data/spec/paperclip/storage/s3_spec.rb +400 -215
  111. data/spec/paperclip/style_spec.rb +0 -1
  112. data/spec/paperclip/tempfile_factory_spec.rb +4 -0
  113. data/spec/paperclip/tempfile_spec.rb +35 -0
  114. data/spec/paperclip/thumbnail_spec.rb +59 -38
  115. data/spec/paperclip/url_generator_spec.rb +55 -45
  116. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  117. data/spec/paperclip/validators_spec.rb +5 -5
  118. data/spec/spec_helper.rb +7 -1
  119. data/spec/support/assertions.rb +12 -1
  120. data/spec/support/fake_model.rb +4 -0
  121. data/spec/support/fixtures/empty.xlsx +0 -0
  122. data/spec/support/matchers/have_column.rb +11 -2
  123. data/spec/support/mock_attachment.rb +2 -0
  124. data/spec/support/mock_url_generator_builder.rb +2 -2
  125. data/spec/support/model_reconstruction.rb +11 -3
  126. data/spec/support/reporting.rb +11 -0
  127. metadata +110 -63
  128. data/RUNNING_TESTS.md +0 -4
  129. data/cucumber/paperclip_steps.rb +0 -6
  130. data/gemfiles/3.2.gemfile +0 -19
  131. data/gemfiles/4.0.gemfile +0 -19
  132. data/gemfiles/4.1.gemfile +0 -19
  133. data/lib/paperclip/locales/de.yml +0 -18
  134. data/lib/paperclip/locales/es.yml +0 -18
  135. data/lib/paperclip/locales/ja.yml +0 -18
  136. data/lib/paperclip/locales/pt-BR.yml +0 -18
  137. data/lib/paperclip/locales/zh-CN.yml +0 -18
  138. data/lib/paperclip/locales/zh-HK.yml +0 -18
  139. data/lib/paperclip/locales/zh-TW.yml +0 -18
  140. data/spec/support/mock_model.rb +0 -2
  141. data/spec/support/rails_helpers.rb +0 -7
data/gemfiles/4.2.gemfile CHANGED
@@ -2,18 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "sqlite3", "1.3.8", :platforms => :ruby
6
- gem "jruby-openssl", :platforms => :jruby
7
- gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
8
- gem "rubysl", :platforms => :rbx
9
- gem "racc", :platforms => :rbx
5
+ gem "sqlite3", "~> 1.3.8", :platforms => :ruby
10
6
  gem "pry"
11
- gem "rails", "~> 4.2.0.rc2"
12
- gem "paperclip", :path => "../"
7
+ gem "rails", "~> 4.2.0"
13
8
 
14
9
  group :development, :test do
15
- gem "mime-types", "~> 1.16"
10
+ gem "activerecord-import"
11
+ gem "mime-types"
16
12
  gem "builder"
13
+ gem "rubocop", :require => false
14
+ gem "rspec"
17
15
  end
18
16
 
19
17
  gemspec :path => "../"
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", "~> 1.3.8", :platforms => :ruby
6
+ gem "pry"
7
+ gem "rails", "~> 5.0.0"
8
+
9
+ group :development, :test do
10
+ gem "activerecord-import"
11
+ gem "mime-types"
12
+ gem "builder"
13
+ gem "rubocop", :require => false
14
+ gem "rspec"
15
+ end
16
+
17
+ gemspec :path => "../"
@@ -13,7 +13,9 @@ class PaperclipGenerator < ActiveRecord::Generators::Base
13
13
  end
14
14
 
15
15
  def generate_migration
16
- migration_template "paperclip_migration.rb.erb", "db/migrate/#{migration_file_name}"
16
+ migration_template("paperclip_migration.rb.erb",
17
+ "db/migrate/#{migration_file_name}",
18
+ migration_version: migration_version)
17
19
  end
18
20
 
19
21
  def migration_name
@@ -27,4 +29,10 @@ class PaperclipGenerator < ActiveRecord::Generators::Base
27
29
  def migration_class_name
28
30
  migration_name.camelize
29
31
  end
32
+
33
+ def migration_version
34
+ if Rails.version.start_with? "5"
35
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
36
+ end
37
+ end
30
38
  end
@@ -1,4 +1,4 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def self.up
3
3
  change_table :<%= table_name %> do |t|
4
4
  <% attachment_names.each do |attachment| -%>
@@ -1,7 +1,7 @@
1
- # encoding: utf-8
2
1
  require 'uri'
3
2
  require 'paperclip/url_generator'
4
3
  require 'active_support/deprecation'
4
+ require 'active_support/core_ext/string/inflections'
5
5
 
6
6
  module Paperclip
7
7
  # The Attachment class manages the files for a given attachment. It saves
@@ -32,6 +32,7 @@ module Paperclip
32
32
  :use_timestamp => true,
33
33
  :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
34
34
  :validate_media_type => true,
35
+ :adapter_options => { hash_digest: Digest::MD5 },
35
36
  :check_validity_before_processing => true
36
37
  }
37
38
  end
@@ -49,7 +50,8 @@ module Paperclip
49
50
  # +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
50
51
  # +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
51
52
  # +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
52
- # +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)
53
55
  # +default_url+ - a URL for the missing image
54
56
  # +default_style+ - the style to use when an argument is not specified e.g. #url, #path
55
57
  # +storage+ - the storage mechanism. Defaults to :filesystem
@@ -68,7 +70,8 @@ module Paperclip
68
70
  # +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
69
71
  # +escape_url+ - Perform URI escaping to URLs. Defaults to true
70
72
  def initialize(name, instance, options = {})
71
- @name = name
73
+ @name = name.to_sym
74
+ @name_string = name.to_s
72
75
  @instance = instance
73
76
 
74
77
  options = self.class.default_options.deep_merge(options)
@@ -80,7 +83,7 @@ module Paperclip
80
83
  @errors = {}
81
84
  @dirty = false
82
85
  @interpolator = options[:interpolator]
83
- @url_generator = options[:url_generator].new(self, @options)
86
+ @url_generator = options[:url_generator].new(self)
84
87
  @source_file_options = options[:source_file_options]
85
88
  @whiny = options[:whiny]
86
89
 
@@ -94,7 +97,8 @@ module Paperclip
94
97
  # attachment:
95
98
  # new_user.avatar = old_user.avatar
96
99
  def assign(uploaded_file)
97
- @file = Paperclip.io_adapters.for(uploaded_file)
100
+ @file = Paperclip.io_adapters.for(uploaded_file,
101
+ @options[:adapter_options])
98
102
  ensure_required_accessors!
99
103
  ensure_required_validations!
100
104
 
@@ -235,6 +239,10 @@ module Paperclip
235
239
  # the instance's errors and returns false, cancelling the save.
236
240
  def save
237
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
238
246
  flush_writes
239
247
  @dirty = false
240
248
  true
@@ -321,7 +329,7 @@ module Paperclip
321
329
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
322
330
  end
323
331
 
324
- # 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
325
333
  # in the paperclip:refresh rake task and that's it. It will regenerate all
326
334
  # thumbnails forcefully, by reobtaining the original file and going through
327
335
  # the post-process again.
@@ -329,8 +337,15 @@ module Paperclip
329
337
  # inconsistencies in timing of S3 commands. It's possible that calling
330
338
  # #reprocess! will lose data if the files are not kept.
331
339
  def reprocess!(*style_args)
332
- saved_only_process, @options[:only_process] = @options[:only_process], style_args
333
- saved_preserve_files, @options[:preserve_files] = @options[:preserve_files], true
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
+
334
349
  begin
335
350
  assign(self)
336
351
  save
@@ -339,14 +354,13 @@ module Paperclip
339
354
  warn "#{e} - skipping file."
340
355
  false
341
356
  ensure
342
- @options[:only_process] = saved_only_process
343
- @options[:preserve_files] = saved_preserve_files
357
+ @options.merge!(saved_flags)
344
358
  end
345
359
  end
346
360
 
347
361
  # Returns true if a file has been assigned.
348
362
  def file?
349
- !original_filename.blank?
363
+ original_filename.present?
350
364
  end
351
365
 
352
366
  alias :present? :file?
@@ -365,7 +379,7 @@ module Paperclip
365
379
  # instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's
366
380
  # "avatar_file_name" field (assuming the attachment is called avatar).
367
381
  def instance_write(attr, value)
368
- setter = :"#{name}_#{attr}="
382
+ setter = :"#{@name_string}_#{attr}="
369
383
  if instance.respond_to?(setter)
370
384
  instance.send(setter, value)
371
385
  end
@@ -374,7 +388,7 @@ module Paperclip
374
388
  # Reads the attachment-specific attribute on the instance. See instance_write
375
389
  # for more details.
376
390
  def instance_read(attr)
377
- getter = :"#{name}_#{attr}"
391
+ getter = :"#{@name_string}_#{attr}"
378
392
  if instance.respond_to?(getter)
379
393
  instance.send(getter)
380
394
  end
@@ -402,8 +416,8 @@ module Paperclip
402
416
 
403
417
  def ensure_required_accessors! #:nodoc:
404
418
  %w(file_name).each do |field|
405
- unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
406
- 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}'")
407
421
  end
408
422
  end
409
423
  end
@@ -425,7 +439,7 @@ module Paperclip
425
439
  def assign_attributes
426
440
  @queued_for_write[:original] = @file
427
441
  assign_file_information
428
- assign_fingerprint(@file.fingerprint)
442
+ assign_fingerprint { @file.fingerprint }
429
443
  assign_timestamps
430
444
  end
431
445
 
@@ -435,9 +449,9 @@ module Paperclip
435
449
  instance_write(:file_size, @file.size)
436
450
  end
437
451
 
438
- def assign_fingerprint(fingerprint)
452
+ def assign_fingerprint
439
453
  if instance_respond_to?(:fingerprint)
440
- instance_write(:fingerprint, fingerprint)
454
+ instance_write(:fingerprint, yield)
441
455
  end
442
456
  end
443
457
 
@@ -463,7 +477,7 @@ module Paperclip
463
477
 
464
478
  def reset_file_if_original_reprocessed
465
479
  instance_write(:file_size, @queued_for_write[:original].size)
466
- assign_fingerprint(@queued_for_write[:original].fingerprint)
480
+ assign_fingerprint { @queued_for_write[:original].fingerprint }
467
481
  reset_updater
468
482
  end
469
483
 
@@ -499,7 +513,7 @@ module Paperclip
499
513
 
500
514
  instance.run_paperclip_callbacks(:post_process) do
501
515
  instance.run_paperclip_callbacks(:"#{name}_post_process") do
502
- unless @options[:check_validity_before_processing] && instance.errors.any?
516
+ if !@options[:check_validity_before_processing] || !instance.errors.any?
503
517
  post_process_styles(*style_args)
504
518
  end
505
519
  end
@@ -517,18 +531,25 @@ module Paperclip
517
531
  begin
518
532
  raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
519
533
  intermediate_files = []
534
+ original = @queued_for_write[:original]
520
535
 
521
- @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
536
+ @queued_for_write[name] = style.processors.
537
+ reduce(original) do |file, processor|
522
538
  file = Paperclip.processor(processor).make(file, style.processor_options, self)
523
- intermediate_files << file
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
524
544
  file
525
545
  end
526
546
 
527
547
  unadapted_file = @queued_for_write[name]
528
- @queued_for_write[name] = Paperclip.io_adapters.for(@queued_for_write[name])
548
+ @queued_for_write[name] = Paperclip.io_adapters.
549
+ for(@queued_for_write[name], @options[:adapter_options])
529
550
  unadapted_file.close if unadapted_file.respond_to?(:close)
530
551
  @queued_for_write[name]
531
- rescue Paperclip::Error => e
552
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
532
553
  log("An error was received while processing: #{e.inspect}")
533
554
  (@errors[:processing] ||= []) << e.message if @options[:whiny]
534
555
  ensure
@@ -579,13 +600,17 @@ module Paperclip
579
600
  def unlink_files(files)
580
601
  Array(files).each do |file|
581
602
  file.close unless file.closed?
582
- file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
603
+
604
+ begin
605
+ file.unlink if file.respond_to?(:unlink)
606
+ rescue Errno::ENOENT
607
+ end
583
608
  end
584
609
  end
585
610
 
586
611
  # You can either specifiy :restricted_characters or you can define your own
587
612
  # :filename_cleaner object. This object needs to respond to #call and takes
588
- # the filename that will be cleaned. It should return the cleaned filenme.
613
+ # the filename that will be cleaned. It should return the cleaned filename.
589
614
  def filename_cleaner
590
615
  @options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
591
616
  end
@@ -51,8 +51,9 @@ module Paperclip
51
51
  end
52
52
 
53
53
  def definitions_for(klass)
54
- klass.ancestors.each_with_object({}) do |ancestor, inherited_definitions|
55
- inherited_definitions.merge! @attachments[ancestor]
54
+ parent_classes = klass.ancestors.reverse
55
+ parent_classes.each_with_object({}) do |ancestor, inherited_definitions|
56
+ inherited_definitions.deep_merge! @attachments[ancestor]
56
57
  end
57
58
  end
58
59
  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 => callback_terminator}].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)
@@ -22,11 +22,13 @@ module Paperclip
22
22
 
23
23
  private
24
24
 
25
- def callback_terminator
26
- if ::ActiveSupport::VERSION::STRING >= '4.1'
27
- lambda { |target, result| result == false }
28
- else
29
- 'result == false'
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
30
32
  end
31
33
  end
32
34
  end
@@ -2,7 +2,7 @@ module Paperclip
2
2
  class ContentTypeDetector
3
3
  # The content-type detection strategy is as follows:
4
4
  #
5
- # 1. Blank/Empty files: If there's no filename or the file is empty,
5
+ # 1. Blank/Empty files: If there's no filepath or the file is empty,
6
6
  # provide a sensible default (application/octet-stream or inode/x-empty)
7
7
  #
8
8
  # 2. Calculated match: Return the first result that is found by both the
@@ -20,8 +20,8 @@ module Paperclip
20
20
  EMPTY_TYPE = "inode/x-empty"
21
21
  SENSIBLE_DEFAULT = "application/octet-stream"
22
22
 
23
- def initialize(filename)
24
- @filename = filename
23
+ def initialize(filepath)
24
+ @filepath = filepath
25
25
  end
26
26
 
27
27
  # Returns a String describing the file's content type
@@ -33,32 +33,48 @@ module Paperclip
33
33
  elsif calculated_type_matches.any?
34
34
  calculated_type_matches.first
35
35
  else
36
- type_from_file_command || SENSIBLE_DEFAULT
36
+ type_from_file_contents || SENSIBLE_DEFAULT
37
37
  end.to_s
38
38
  end
39
39
 
40
40
  private
41
41
 
42
+ def blank_name?
43
+ @filepath.nil? || @filepath.empty?
44
+ end
45
+
42
46
  def empty_file?
43
- File.exist?(@filename) && File.size(@filename) == 0
47
+ File.exist?(@filepath) && File.size(@filepath) == 0
44
48
  end
45
49
 
46
50
  alias :empty? :empty_file?
47
51
 
48
- def blank_name?
49
- @filename.nil? || @filename.empty?
52
+ def calculated_type_matches
53
+ possible_types.select do |content_type|
54
+ content_type == type_from_file_contents
55
+ end
50
56
  end
51
57
 
52
58
  def possible_types
53
- MIME::Types.type_for(@filename).collect(&:content_type)
59
+ MIME::Types.type_for(@filepath).collect(&:content_type)
54
60
  end
55
61
 
56
- def calculated_type_matches
57
- possible_types.select{|content_type| content_type == type_from_file_command }
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
67
+ end
68
+
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
58
73
  end
59
74
 
60
75
  def type_from_file_command
61
- @type_from_file_command ||= FileCommandContentTypeDetector.new(@filename).detect
76
+ @type_from_file_command ||=
77
+ FileCommandContentTypeDetector.new(@filepath).detect
62
78
  end
63
79
  end
64
80
  end
@@ -19,7 +19,9 @@ module Paperclip
19
19
  end
20
20
 
21
21
  # Will be thrown when ImageMagic cannot determine the uploaded file's
22
- # metadata, usually this would mean the file is not an image.
22
+ # metadata, usually this would mean the file is not an image. If you are
23
+ # consistently receiving this error on PDFs make sure that you have
24
+ # installed Ghostscript.
23
25
  class NotIdentifiedByImageMagickError < Paperclip::Error
24
26
  end
25
27
 
@@ -13,20 +13,18 @@ module Paperclip
13
13
  private
14
14
 
15
15
  def type_from_file_command
16
+ # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
16
17
  type = begin
17
- # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
18
- Paperclip.run("file", "-b --mime :file", :file => @filename)
19
- rescue Cocaine::CommandLineError => e
20
- Paperclip.log("Error while determining content type: #{e}")
21
- SENSIBLE_DEFAULT
22
- end
18
+ Paperclip.run("file", "-b --mime :file", file: @filename)
19
+ rescue Terrapin::CommandLineError => e
20
+ Paperclip.log("Error while determining content type: #{e}")
21
+ SENSIBLE_DEFAULT
22
+ end
23
23
 
24
24
  if type.nil? || type.match(/\(.*?\)/)
25
25
  type = SENSIBLE_DEFAULT
26
26
  end
27
27
  type.split(/[:;\s]+/)[0]
28
28
  end
29
-
30
29
  end
31
30
  end
32
-
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  module Paperclip
3
2
  class FilenameCleaner
4
3
  def initialize(invalid_character_regex)
@@ -17,16 +17,16 @@ module Paperclip
17
17
  orientation = Paperclip.options[:use_exif_orientation] ?
18
18
  "%[exif:orientation]" : "1"
19
19
  Paperclip.run(
20
- "identify",
20
+ Paperclip.options[:is_windows] ? "magick identify" : "identify",
21
21
  "-format '%wx%h,#{orientation}' :file", {
22
22
  :file => "#{path}[0]"
23
23
  }, {
24
24
  :swallow_stderr => true
25
25
  }
26
26
  )
27
- rescue Cocaine::ExitStatusError
27
+ rescue Terrapin::ExitStatusError
28
28
  ""
29
- rescue Cocaine::CommandNotFoundError => e
29
+ rescue Terrapin::CommandNotFoundError => e
30
30
  raise_because_imagemagick_missing
31
31
  end
32
32
  end
@@ -1,6 +1,6 @@
1
1
  module Paperclip
2
2
  class GeometryParser
3
- FORMAT = /\b(\d*)x?(\d*)\b(?:,(\d?))?([\>\<\#\@\%^!])?/i
3
+ FORMAT = /\b(\d*)x?(\d*)\b(?:,(\d?))?(\@\>|\>\@|[\>\<\#\@\%^!])?/i
4
4
  def initialize(string)
5
5
  @string = string
6
6
  end
@@ -8,7 +8,7 @@ module Paperclip
8
8
  base.extend ClassMethods
9
9
  base.send :include, Callbacks
10
10
  base.send :include, Validators
11
- base.send :include, Schema if defined? ActiveRecord
11
+ base.send :include, Schema if defined? ActiveRecord::Base
12
12
 
13
13
  locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
14
14
  I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
@@ -79,7 +79,8 @@ module Paperclip
79
79
  end
80
80
 
81
81
  def add_required_validations
82
- if @options[:validate_media_type] != false
82
+ options = Paperclip::Attachment.default_options.deep_merge(@options)
83
+ if options[:validate_media_type] != false
83
84
  name = @name
84
85
  @klass.validates_media_type_spoof_detection name,
85
86
  :if => ->(instance){ instance.send(name).dirty? }
@@ -90,7 +91,13 @@ module Paperclip
90
91
  name = @name
91
92
  @klass.send(:after_save) { send(name).send(:save) }
92
93
  @klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
93
- @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) }
94
+ if @klass.respond_to?(:after_commit)
95
+ @klass.send(:after_commit, on: :destroy) do
96
+ send(name).send(:flush_deletes)
97
+ end
98
+ else
99
+ @klass.send(:after_destroy) { send(name).send(:flush_deletes) }
100
+ end
94
101
  end
95
102
 
96
103
  def add_paperclip_callbacks
@@ -8,27 +8,31 @@ module Paperclip
8
8
  Paperclip::Interpolations[key] = block
9
9
  end
10
10
 
11
- # The run method takes the name of a binary to run, the arguments to that binary
12
- # and some options:
11
+ # The run method takes the name of a binary to run, the arguments
12
+ # to that binary, the values to interpolate and some local options.
13
13
  #
14
- # :command_path -> A $PATH-like variable that defines where to look for the binary
15
- # on the filesystem. Colon-separated, just like $PATH.
14
+ # :cmd -> The name of a binary to run.
16
15
  #
17
- # :expected_outcodes -> An array of integers that defines the expected exit codes
18
- # of the binary. Defaults to [0].
16
+ # :arguments -> The command line arguments to that binary.
19
17
  #
20
- # :log_command -> Log the command being run when set to true (defaults to true).
21
- # This will only log if logging in general is set to true as well.
18
+ # :interpolation_values -> Values to be interpolated into the arguments.
22
19
  #
23
- # :swallow_stderr -> Set to true if you don't care what happens on STDERR.
20
+ # :local_options -> The options to be used by Cocain::CommandLine.
21
+ # These could be: runner
22
+ # logger
23
+ # swallow_stderr
24
+ # expected_outcodes
25
+ # environment
26
+ # runner_options
24
27
  #
25
28
  def run(cmd, arguments = "", interpolation_values = {}, local_options = {})
26
29
  command_path = options[:command_path]
27
- Cocaine::CommandLine.path = [Cocaine::CommandLine.path, command_path].flatten.compact.uniq
30
+ terrapin_path_array = Terrapin::CommandLine.path.try(:split, Terrapin::OS.path_separator)
31
+ Terrapin::CommandLine.path = [terrapin_path_array, command_path].flatten.compact.uniq
28
32
  if logging? && (options[:log_command] || local_options[:log_command])
29
33
  local_options = local_options.merge(:logger => logger)
30
34
  end
31
- Cocaine::CommandLine.new(cmd, arguments, local_options).run(interpolation_values)
35
+ Terrapin::CommandLine.new(cmd, arguments, local_options).run(interpolation_values)
32
36
  end
33
37
 
34
38
  # Find all instances of the given Active Record model +klass+ with attachment +name+.
@@ -2,15 +2,16 @@ module Paperclip
2
2
  module Interpolations
3
3
  class PluralCache
4
4
  def initialize
5
- @cache = {}
5
+ @symbol_cache = {}.compare_by_identity
6
+ @klass_cache = {}.compare_by_identity
6
7
  end
7
8
 
8
- def pluralize(word)
9
- @cache[word] ||= word.pluralize
9
+ def pluralize_symbol(symbol)
10
+ @symbol_cache[symbol] ||= symbol.to_s.downcase.pluralize
10
11
  end
11
12
 
12
- def underscore_and_pluralize(word)
13
- @cache[word] ||= word.underscore.pluralize
13
+ def underscore_and_pluralize_class(klass)
14
+ @klass_cache[klass] ||= klass.name.underscore.pluralize
14
15
  end
15
16
  end
16
17
  end