paperclip 4.3.7 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) 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 +5 -16
  5. data/.travis.yml +15 -12
  6. data/Appraisals +4 -8
  7. data/CONTRIBUTING.md +16 -5
  8. data/Gemfile +3 -8
  9. data/LICENSE +1 -1
  10. data/MIGRATING-ES.md +317 -0
  11. data/MIGRATING.md +375 -0
  12. data/NEWS +126 -31
  13. data/README.md +264 -156
  14. data/Rakefile +1 -1
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +3 -2
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +14 -14
  19. data/features/step_definitions/rails_steps.rb +29 -28
  20. data/features/step_definitions/s3_steps.rb +2 -2
  21. data/features/support/env.rb +1 -0
  22. data/features/support/paths.rb +1 -1
  23. data/features/support/rails.rb +0 -24
  24. data/gemfiles/4.2.gemfile +3 -5
  25. data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
  26. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  27. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  28. data/lib/paperclip.rb +14 -12
  29. data/lib/paperclip/attachment.rb +44 -20
  30. data/lib/paperclip/attachment_registry.rb +2 -1
  31. data/lib/paperclip/callbacks.rb +8 -6
  32. data/lib/paperclip/content_type_detector.rb +3 -2
  33. data/lib/paperclip/errors.rb +3 -1
  34. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  35. data/lib/paperclip/filename_cleaner.rb +0 -1
  36. data/lib/paperclip/geometry_detector_factory.rb +3 -3
  37. data/lib/paperclip/glue.rb +1 -1
  38. data/lib/paperclip/has_attached_file.rb +7 -1
  39. data/lib/paperclip/helpers.rb +15 -11
  40. data/lib/paperclip/interpolations.rb +7 -2
  41. data/lib/paperclip/io_adapters/abstract_adapter.rb +31 -4
  42. data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
  43. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  44. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  45. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  46. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  47. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  48. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  49. data/lib/paperclip/io_adapters/registry.rb +6 -2
  50. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  51. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  52. data/lib/paperclip/io_adapters/uri_adapter.rb +22 -17
  53. data/lib/paperclip/logger.rb +1 -1
  54. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  55. data/lib/paperclip/media_type_spoof_detector.rb +11 -7
  56. data/lib/paperclip/processor.rb +15 -6
  57. data/lib/paperclip/schema.rb +3 -9
  58. data/lib/paperclip/storage/filesystem.rb +13 -2
  59. data/lib/paperclip/storage/fog.rb +21 -14
  60. data/lib/paperclip/storage/s3.rb +81 -61
  61. data/lib/paperclip/style.rb +0 -1
  62. data/lib/paperclip/thumbnail.rb +22 -9
  63. data/lib/paperclip/url_generator.rb +17 -13
  64. data/lib/paperclip/validators.rb +1 -1
  65. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  66. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
  67. data/lib/paperclip/version.rb +3 -1
  68. data/lib/tasks/paperclip.rake +18 -4
  69. data/paperclip.gemspec +13 -10
  70. data/spec/paperclip/attachment_processing_spec.rb +2 -5
  71. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  72. data/spec/paperclip/attachment_spec.rb +89 -20
  73. data/spec/paperclip/content_type_detector_spec.rb +1 -1
  74. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
  75. data/spec/paperclip/filename_cleaner_spec.rb +0 -1
  76. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  77. data/spec/paperclip/integration_spec.rb +42 -5
  78. data/spec/paperclip/interpolations_spec.rb +9 -0
  79. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +104 -22
  80. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  81. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  82. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  83. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +47 -13
  84. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  85. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  86. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  87. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  88. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +104 -11
  89. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  90. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
  91. data/spec/paperclip/media_type_spoof_detector_spec.rb +41 -0
  92. data/spec/paperclip/paperclip_spec.rb +15 -45
  93. data/spec/paperclip/processor_spec.rb +4 -4
  94. data/spec/paperclip/schema_spec.rb +46 -46
  95. data/spec/paperclip/storage/fog_spec.rb +31 -0
  96. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  97. data/spec/paperclip/storage/s3_spec.rb +359 -192
  98. data/spec/paperclip/style_spec.rb +0 -1
  99. data/spec/paperclip/tempfile_spec.rb +35 -0
  100. data/spec/paperclip/thumbnail_spec.rb +43 -38
  101. data/spec/paperclip/url_generator_spec.rb +54 -44
  102. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  103. data/spec/paperclip/validators_spec.rb +5 -5
  104. data/spec/spec_helper.rb +5 -2
  105. data/spec/support/assertions.rb +12 -1
  106. data/spec/support/mock_attachment.rb +2 -0
  107. data/spec/support/mock_url_generator_builder.rb +2 -2
  108. data/spec/support/model_reconstruction.rb +11 -3
  109. data/spec/support/reporting.rb +11 -0
  110. metadata +64 -61
  111. data/cucumber/paperclip_steps.rb +0 -6
  112. data/gemfiles/4.1.gemfile +0 -19
  113. data/lib/paperclip/deprecations.rb +0 -42
  114. data/lib/paperclip/locales/de.yml +0 -18
  115. data/lib/paperclip/locales/es.yml +0 -18
  116. data/lib/paperclip/locales/ja.yml +0 -18
  117. data/lib/paperclip/locales/pt-BR.yml +0 -18
  118. data/lib/paperclip/locales/zh-CN.yml +0 -18
  119. data/lib/paperclip/locales/zh-HK.yml +0 -18
  120. data/lib/paperclip/locales/zh-TW.yml +0 -18
  121. data/spec/paperclip/deprecations_spec.rb +0 -65
  122. data/spec/support/deprecations.rb +0 -9
  123. data/spec/support/rails_helpers.rb +0 -7
@@ -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
@@ -81,7 +83,7 @@ module Paperclip
81
83
  @errors = {}
82
84
  @dirty = false
83
85
  @interpolator = options[:interpolator]
84
- @url_generator = options[:url_generator].new(self, @options)
86
+ @url_generator = options[:url_generator].new(self)
85
87
  @source_file_options = options[:source_file_options]
86
88
  @whiny = options[:whiny]
87
89
 
@@ -95,7 +97,8 @@ module Paperclip
95
97
  # attachment:
96
98
  # new_user.avatar = old_user.avatar
97
99
  def assign(uploaded_file)
98
- @file = Paperclip.io_adapters.for(uploaded_file)
100
+ @file = Paperclip.io_adapters.for(uploaded_file,
101
+ @options[:adapter_options])
99
102
  ensure_required_accessors!
100
103
  ensure_required_validations!
101
104
 
@@ -236,6 +239,10 @@ module Paperclip
236
239
  # the instance's errors and returns false, cancelling the save.
237
240
  def save
238
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
239
246
  flush_writes
240
247
  @dirty = false
241
248
  true
@@ -322,7 +329,7 @@ module Paperclip
322
329
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
323
330
  end
324
331
 
325
- # 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
326
333
  # in the paperclip:refresh rake task and that's it. It will regenerate all
327
334
  # thumbnails forcefully, by reobtaining the original file and going through
328
335
  # the post-process again.
@@ -330,8 +337,15 @@ module Paperclip
330
337
  # inconsistencies in timing of S3 commands. It's possible that calling
331
338
  # #reprocess! will lose data if the files are not kept.
332
339
  def reprocess!(*style_args)
333
- saved_only_process, @options[:only_process] = @options[:only_process], style_args
334
- 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
+
335
349
  begin
336
350
  assign(self)
337
351
  save
@@ -340,14 +354,13 @@ module Paperclip
340
354
  warn "#{e} - skipping file."
341
355
  false
342
356
  ensure
343
- @options[:only_process] = saved_only_process
344
- @options[:preserve_files] = saved_preserve_files
357
+ @options.merge!(saved_flags)
345
358
  end
346
359
  end
347
360
 
348
361
  # Returns true if a file has been assigned.
349
362
  def file?
350
- !original_filename.blank?
363
+ original_filename.present?
351
364
  end
352
365
 
353
366
  alias :present? :file?
@@ -426,7 +439,7 @@ module Paperclip
426
439
  def assign_attributes
427
440
  @queued_for_write[:original] = @file
428
441
  assign_file_information
429
- assign_fingerprint(@file.fingerprint)
442
+ assign_fingerprint { @file.fingerprint }
430
443
  assign_timestamps
431
444
  end
432
445
 
@@ -436,9 +449,9 @@ module Paperclip
436
449
  instance_write(:file_size, @file.size)
437
450
  end
438
451
 
439
- def assign_fingerprint(fingerprint)
452
+ def assign_fingerprint
440
453
  if instance_respond_to?(:fingerprint)
441
- instance_write(:fingerprint, fingerprint)
454
+ instance_write(:fingerprint, yield)
442
455
  end
443
456
  end
444
457
 
@@ -464,7 +477,7 @@ module Paperclip
464
477
 
465
478
  def reset_file_if_original_reprocessed
466
479
  instance_write(:file_size, @queued_for_write[:original].size)
467
- assign_fingerprint(@queued_for_write[:original].fingerprint)
480
+ assign_fingerprint { @queued_for_write[:original].fingerprint }
468
481
  reset_updater
469
482
  end
470
483
 
@@ -500,7 +513,7 @@ module Paperclip
500
513
 
501
514
  instance.run_paperclip_callbacks(:post_process) do
502
515
  instance.run_paperclip_callbacks(:"#{name}_post_process") do
503
- unless @options[:check_validity_before_processing] && instance.errors.any?
516
+ if !@options[:check_validity_before_processing] || !instance.errors.any?
504
517
  post_process_styles(*style_args)
505
518
  end
506
519
  end
@@ -518,15 +531,22 @@ module Paperclip
518
531
  begin
519
532
  raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
520
533
  intermediate_files = []
534
+ original = @queued_for_write[:original]
521
535
 
522
- @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|
523
538
  file = Paperclip.processor(processor).make(file, style.processor_options, self)
524
- 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
525
544
  file
526
545
  end
527
546
 
528
547
  unadapted_file = @queued_for_write[name]
529
- @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])
530
550
  unadapted_file.close if unadapted_file.respond_to?(:close)
531
551
  @queued_for_write[name]
532
552
  rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
@@ -580,13 +600,17 @@ module Paperclip
580
600
  def unlink_files(files)
581
601
  Array(files).each do |file|
582
602
  file.close unless file.closed?
583
- 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
584
608
  end
585
609
  end
586
610
 
587
611
  # You can either specifiy :restricted_characters or you can define your own
588
612
  # :filename_cleaner object. This object needs to respond to #call and takes
589
- # 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.
590
614
  def filename_cleaner
591
615
  @options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
592
616
  end
@@ -51,7 +51,8 @@ module Paperclip
51
51
  end
52
52
 
53
53
  def definitions_for(klass)
54
- klass.ancestors.each_with_object({}) do |ancestor, inherited_definitions|
54
+ parent_classes = klass.ancestors.reverse
55
+ parent_classes.each_with_object({}) do |ancestor, inherited_definitions|
55
56
  inherited_definitions.deep_merge! @attachments[ancestor]
56
57
  end
57
58
  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
@@ -67,8 +67,9 @@ module Paperclip
67
67
  end
68
68
 
69
69
  def type_from_mime_magic
70
- @type_from_mime_magic ||=
71
- MimeMagic.by_magic(File.open(@filepath)).try(:type)
70
+ @type_from_mime_magic ||= File.open(@filepath) do |file|
71
+ MimeMagic.by_magic(file).try(:type)
72
+ end
72
73
  end
73
74
 
74
75
  def type_from_file_command
@@ -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
 
@@ -16,7 +16,7 @@ module Paperclip
16
16
  # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
17
17
  type = begin
18
18
  Paperclip.run("file", "-b --mime :file", file: @filename)
19
- rescue Cocaine::CommandLineError => e
19
+ rescue Terrapin::CommandLineError => e
20
20
  Paperclip.log("Error while determining content type: #{e}")
21
21
  SENSIBLE_DEFAULT
22
22
  end
@@ -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
@@ -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)
@@ -91,7 +91,13 @@ module Paperclip
91
91
  name = @name
92
92
  @klass.send(:after_save) { send(name).send(:save) }
93
93
  @klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
94
- @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
95
101
  end
96
102
 
97
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+.
@@ -5,6 +5,7 @@ module Paperclip
5
5
  # Paperclip.interpolates method.
6
6
  module Interpolations
7
7
  extend self
8
+ ID_PARTITION_LIMIT = 1_000_000_000
8
9
 
9
10
  # Hash assignment of interpolations. Included only for compatibility,
10
11
  # and is not intended for normal use.
@@ -141,7 +142,7 @@ module Paperclip
141
142
  # It's possible, though unlikely, that the mime type is not in the
142
143
  # database, so just use the part after the '/' in the mime type as the
143
144
  # extension.
144
- %r{/([^/]*)\Z}.match(attachment.content_type)[1]
145
+ %r{/([^/]*)\z}.match(attachment.content_type)[1]
145
146
  end
146
147
  end
147
148
 
@@ -175,7 +176,11 @@ module Paperclip
175
176
  def id_partition attachment, style_name
176
177
  case id = attachment.instance.id
177
178
  when Integer
178
- ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
179
+ if id < ID_PARTITION_LIMIT
180
+ ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
181
+ else
182
+ ("%012d".freeze % id).scan(/\d{3}/).join("/".freeze)
183
+ end
179
184
  when String
180
185
  id.scan(/.{3}/).first(3).join("/".freeze)
181
186
  else
@@ -4,12 +4,24 @@ module Paperclip
4
4
  class AbstractAdapter
5
5
  OS_RESTRICTED_CHARACTERS = %r{[/:]}
6
6
 
7
- attr_reader :content_type, :original_filename, :size
8
- delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile
7
+ attr_reader :content_type, :original_filename, :size, :tempfile
8
+ delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :readbyte, :rewind, :unlink, :to => :@tempfile
9
9
  alias :length :size
10
10
 
11
+ def initialize(target, options = {})
12
+ @target = target
13
+ @options = options
14
+ end
15
+
11
16
  def fingerprint
12
- @fingerprint ||= Digest::MD5.file(path).to_s
17
+ @fingerprint ||= begin
18
+ digest = @options.fetch(:hash_digest).new
19
+ File.open(path, "rb") do |f|
20
+ buf = ""
21
+ digest.update(buf) while f.read(16384, buf)
22
+ end
23
+ digest.hexdigest
24
+ end
13
25
  end
14
26
 
15
27
  def read(length = nil, buffer = nil)
@@ -40,8 +52,23 @@ module Paperclip
40
52
  end
41
53
 
42
54
  def copy_to_tempfile(src)
43
- FileUtils.cp(src.path, destination.path)
55
+ link_or_copy_file(src.path, destination.path)
44
56
  destination
45
57
  end
58
+
59
+ def link_or_copy_file(src, dest)
60
+ begin
61
+ Paperclip.log("Trying to link #{src} to #{dest}")
62
+ FileUtils.ln(src, dest, force: true) # overwrite existing
63
+ rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e
64
+ Paperclip.log(
65
+ "Link failed with #{e.message}; copying link #{src} to #{dest}"
66
+ )
67
+ FileUtils.cp(src, dest)
68
+ end
69
+
70
+ @destination.close
71
+ @destination.open.binmode
72
+ end
46
73
  end
47
74
  end
@@ -1,6 +1,13 @@
1
1
  module Paperclip
2
2
  class AttachmentAdapter < AbstractAdapter
3
- def initialize(target)
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ Paperclip::Attachment === target || Paperclip::Style === target
6
+ end
7
+ end
8
+
9
+ def initialize(target, options = {})
10
+ super
4
11
  @target, @style = case target
5
12
  when Paperclip::Attachment
6
13
  [target, :original]
@@ -22,15 +29,19 @@ module Paperclip
22
29
 
23
30
  def copy_to_tempfile(source)
24
31
  if source.staged?
25
- FileUtils.cp(source.staged_path(@style), destination.path)
32
+ link_or_copy_file(source.staged_path(@style), destination.path)
26
33
  else
27
- source.copy_to_local_file(@style, destination.path)
34
+ begin
35
+ source.copy_to_local_file(@style, destination.path)
36
+ rescue Errno::EACCES
37
+ # clean up lingering tempfile if we cannot access source file
38
+ destination.close(true)
39
+ raise
40
+ end
28
41
  end
29
42
  destination
30
43
  end
31
44
  end
32
45
  end
33
46
 
34
- Paperclip.io_adapters.register Paperclip::AttachmentAdapter do |target|
35
- Paperclip::Attachment === target || Paperclip::Style === target
36
- end
47
+ Paperclip::AttachmentAdapter.register