paperclip 4.3.7 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.hound.yml +5 -16
  4. data/.travis.yml +15 -12
  5. data/Appraisals +4 -8
  6. data/CONTRIBUTING.md +16 -5
  7. data/Gemfile +3 -8
  8. data/LICENSE +1 -1
  9. data/NEWS +86 -31
  10. data/README.md +220 -146
  11. data/Rakefile +1 -1
  12. data/UPGRADING +12 -9
  13. data/features/basic_integration.feature +3 -2
  14. data/features/migration.feature +0 -24
  15. data/features/step_definitions/attachment_steps.rb +6 -6
  16. data/features/step_definitions/rails_steps.rb +29 -28
  17. data/features/step_definitions/s3_steps.rb +2 -2
  18. data/features/support/env.rb +1 -0
  19. data/features/support/paths.rb +1 -1
  20. data/features/support/rails.rb +0 -24
  21. data/gemfiles/4.2.gemfile +3 -5
  22. data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
  23. data/lib/paperclip.rb +12 -11
  24. data/lib/paperclip/attachment.rb +25 -14
  25. data/lib/paperclip/attachment_registry.rb +2 -1
  26. data/lib/paperclip/callbacks.rb +8 -6
  27. data/lib/paperclip/content_type_detector.rb +3 -2
  28. data/lib/paperclip/errors.rb +3 -1
  29. data/lib/paperclip/glue.rb +1 -1
  30. data/lib/paperclip/has_attached_file.rb +7 -1
  31. data/lib/paperclip/helpers.rb +14 -10
  32. data/lib/paperclip/interpolations.rb +1 -1
  33. data/lib/paperclip/io_adapters/abstract_adapter.rb +25 -3
  34. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  35. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  36. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  37. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  38. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  39. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  40. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  41. data/lib/paperclip/io_adapters/registry.rb +6 -2
  42. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  43. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  44. data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
  45. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  46. data/lib/paperclip/processor.rb +5 -4
  47. data/lib/paperclip/schema.rb +2 -8
  48. data/lib/paperclip/storage/filesystem.rb +13 -2
  49. data/lib/paperclip/storage/fog.rb +18 -12
  50. data/lib/paperclip/storage/s3.rb +85 -57
  51. data/lib/paperclip/thumbnail.rb +14 -4
  52. data/lib/paperclip/url_generator.rb +16 -13
  53. data/lib/paperclip/validators.rb +1 -1
  54. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  55. data/lib/paperclip/version.rb +3 -1
  56. data/lib/tasks/paperclip.rake +18 -4
  57. data/paperclip.gemspec +12 -9
  58. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  59. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  60. data/spec/paperclip/attachment_spec.rb +72 -18
  61. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  62. data/spec/paperclip/integration_spec.rb +4 -3
  63. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +45 -22
  64. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  65. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  66. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  67. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +18 -1
  68. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  69. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  70. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  71. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  72. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +48 -3
  73. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  74. data/spec/paperclip/paperclip_spec.rb +3 -33
  75. data/spec/paperclip/storage/fog_spec.rb +26 -0
  76. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  77. data/spec/paperclip/storage/s3_spec.rb +357 -190
  78. data/spec/paperclip/tempfile_spec.rb +35 -0
  79. data/spec/paperclip/thumbnail_spec.rb +35 -32
  80. data/spec/paperclip/url_generator_spec.rb +54 -43
  81. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  82. data/spec/paperclip/validators_spec.rb +5 -5
  83. data/spec/spec_helper.rb +6 -2
  84. data/spec/support/assertions.rb +12 -1
  85. data/spec/support/conditional_filter_helper.rb +5 -0
  86. data/spec/support/mock_attachment.rb +2 -0
  87. data/spec/support/mock_url_generator_builder.rb +2 -2
  88. data/spec/support/model_reconstruction.rb +9 -1
  89. data/spec/support/reporting.rb +11 -0
  90. metadata +61 -158
  91. data/cucumber/paperclip_steps.rb +0 -6
  92. data/gemfiles/4.1.gemfile +0 -19
  93. data/lib/paperclip/deprecations.rb +0 -42
  94. data/lib/paperclip/locales/de.yml +0 -18
  95. data/lib/paperclip/locales/es.yml +0 -18
  96. data/lib/paperclip/locales/ja.yml +0 -18
  97. data/lib/paperclip/locales/pt-BR.yml +0 -18
  98. data/lib/paperclip/locales/zh-CN.yml +0 -18
  99. data/lib/paperclip/locales/zh-HK.yml +0 -18
  100. data/lib/paperclip/locales/zh-TW.yml +0 -18
  101. data/spec/paperclip/deprecations_spec.rb +0 -65
  102. data/spec/support/deprecations.rb +0 -9
  103. data/spec/support/rails_helpers.rb +0 -7
@@ -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
 
@@ -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,23 +8,27 @@ 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
+ cocaine_path_array = Cocaine::CommandLine.path.try(:split, Cocaine::OS.path_separator)
31
+ Cocaine::CommandLine.path = [cocaine_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
@@ -141,7 +141,7 @@ module Paperclip
141
141
  # It's possible, though unlikely, that the mime type is not in the
142
142
  # database, so just use the part after the '/' in the mime type as the
143
143
  # extension.
144
- %r{/([^/]*)\Z}.match(attachment.content_type)[1]
144
+ %r{/([^/]*)\z}.match(attachment.content_type)[1]
145
145
  end
146
146
  end
147
147
 
@@ -5,11 +5,23 @@ module Paperclip
5
5
  OS_RESTRICTED_CHARACTERS = %r{[/:]}
6
6
 
7
7
  attr_reader :content_type, :original_filename, :size
8
- delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :rewind, :unlink, :to => :@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,18 @@ 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
+ Paperclip.log("Trying to link #{src} to #{dest}")
61
+ FileUtils.ln(src, dest, force: true) # overwrite existing
62
+ @destination.close
63
+ @destination.open.binmode
64
+ rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT => e
65
+ Paperclip.log("Link failed with #{e.message}; copying link #{src} to #{dest}")
66
+ FileUtils.cp(src, dest)
67
+ end
46
68
  end
47
69
  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,7 +29,7 @@ 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
34
  source.copy_to_local_file(@style, destination.path)
28
35
  end
@@ -31,6 +38,4 @@ module Paperclip
31
38
  end
32
39
  end
33
40
 
34
- Paperclip.io_adapters.register Paperclip::AttachmentAdapter do |target|
35
- Paperclip::Attachment === target || Paperclip::Style === target
36
- end
41
+ Paperclip::AttachmentAdapter.register
@@ -1,22 +1,22 @@
1
1
  module Paperclip
2
2
  class DataUriAdapter < StringioAdapter
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ String === target && target =~ REGEXP
6
+ end
7
+ end
3
8
 
4
9
  REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m
5
10
 
6
- def initialize(target_uri)
7
- super(extract_target(target_uri))
11
+ def initialize(target_uri, options = {})
12
+ super(extract_target(target_uri), options)
8
13
  end
9
14
 
10
15
  private
11
16
 
12
17
  def extract_target(uri)
13
18
  data_uri_parts = uri.match(REGEXP) || []
14
- StringIO.new(Base64.decode64(data_uri_parts[2] || ''))
19
+ StringIO.new(Base64.decode64(data_uri_parts[2] || ""))
15
20
  end
16
-
17
21
  end
18
22
  end
19
-
20
- Paperclip.io_adapters.register Paperclip::DataUriAdapter do |target|
21
- String === target && target =~ Paperclip::DataUriAdapter::REGEXP
22
- end
@@ -1,6 +1,9 @@
1
1
  module Paperclip
2
2
  class EmptyStringAdapter < AbstractAdapter
3
- def initialize(target)
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ target.is_a?(String) && target.empty?
6
+ end
4
7
  end
5
8
 
6
9
  def nil?
@@ -13,6 +16,4 @@ module Paperclip
13
16
  end
14
17
  end
15
18
 
16
- Paperclip.io_adapters.register Paperclip::EmptyStringAdapter do |target|
17
- target.is_a?(String) && target.empty?
18
- end
19
+ Paperclip::EmptyStringAdapter.register
@@ -1,14 +1,22 @@
1
1
  module Paperclip
2
2
  class FileAdapter < AbstractAdapter
3
- def initialize(target)
4
- @target = target
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ File === target || ::Tempfile === target
6
+ end
7
+ end
8
+
9
+ def initialize(target, options = {})
10
+ super
5
11
  cache_current_values
6
12
  end
7
13
 
8
14
  private
9
15
 
10
16
  def cache_current_values
11
- self.original_filename = @target.original_filename if @target.respond_to?(:original_filename)
17
+ if @target.respond_to?(:original_filename)
18
+ self.original_filename = @target.original_filename
19
+ end
12
20
  self.original_filename ||= File.basename(@target.path)
13
21
  @tempfile = copy_to_tempfile(@target)
14
22
  @content_type = ContentTypeDetector.new(@target.path).detect
@@ -17,6 +25,4 @@ module Paperclip
17
25
  end
18
26
  end
19
27
 
20
- Paperclip.io_adapters.register Paperclip::FileAdapter do |target|
21
- File === target || Tempfile === target
22
- end
28
+ Paperclip::FileAdapter.register
@@ -1,15 +1,15 @@
1
1
  module Paperclip
2
2
  class HttpUrlProxyAdapter < UriAdapter
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ String === target && target =~ REGEXP
6
+ end
7
+ end
3
8
 
4
9
  REGEXP = /\Ahttps?:\/\//
5
10
 
6
- def initialize(target)
7
- super(URI(target))
11
+ def initialize(target, options = {})
12
+ super(URI(URI.escape(target)), options)
8
13
  end
9
-
10
14
  end
11
15
  end
12
-
13
- Paperclip.io_adapters.register Paperclip::HttpUrlProxyAdapter do |target|
14
- String === target && target =~ Paperclip::HttpUrlProxyAdapter::REGEXP
15
- end
@@ -1,12 +1,18 @@
1
1
  module Paperclip
2
2
  class IdentityAdapter < AbstractAdapter
3
- def new(adapter)
4
- adapter
3
+ def self.register
4
+ Paperclip.io_adapters.register Paperclip::IdentityAdapter.new do |target|
5
+ Paperclip.io_adapters.registered?(target)
6
+ end
7
+ end
8
+
9
+ def initialize
5
10
  end
6
- end
7
- end
8
11
 
9
- Paperclip.io_adapters.register Paperclip::IdentityAdapter.new do |target|
10
- Paperclip.io_adapters.registered?(target)
12
+ def new(target, _)
13
+ target
14
+ end
15
+ end
11
16
  end
12
17
 
18
+ Paperclip::IdentityAdapter.register
@@ -1,8 +1,13 @@
1
1
  module Paperclip
2
2
  class NilAdapter < AbstractAdapter
3
- def initialize(target)
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ target.nil? || ((Paperclip::Attachment === target) && !target.present?)
6
+ end
4
7
  end
5
8
 
9
+ def initialize(_target, _options = {}); end
10
+
6
11
  def original_filename
7
12
  ""
8
13
  end
@@ -19,7 +24,7 @@ module Paperclip
19
24
  true
20
25
  end
21
26
 
22
- def read(*args)
27
+ def read(*_args)
23
28
  nil
24
29
  end
25
30
 
@@ -29,6 +34,4 @@ module Paperclip
29
34
  end
30
35
  end
31
36
 
32
- Paperclip.io_adapters.register Paperclip::NilAdapter do |target|
33
- target.nil? || ( (Paperclip::Attachment === target) && !target.present? )
34
- end
37
+ Paperclip::NilAdapter.register
@@ -12,6 +12,10 @@ module Paperclip
12
12
  @registered_handlers << [block, handler_class]
13
13
  end
14
14
 
15
+ def unregister(handler_class)
16
+ @registered_handlers.reject! { |_, klass| klass == handler_class }
17
+ end
18
+
15
19
  def handler_for(target)
16
20
  @registered_handlers.each do |tester, handler|
17
21
  return handler if tester.call(target)
@@ -25,8 +29,8 @@ module Paperclip
25
29
  end
26
30
  end
27
31
 
28
- def for(target)
29
- handler_for(target).new(target)
32
+ def for(target, options = {})
33
+ handler_for(target).new(target, options)
30
34
  end
31
35
  end
32
36
  end
@@ -1,7 +1,13 @@
1
1
  module Paperclip
2
2
  class StringioAdapter < AbstractAdapter
3
- def initialize(target)
4
- @target = target
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ StringIO === target
6
+ end
7
+ end
8
+
9
+ def initialize(target, options = {})
10
+ super
5
11
  cache_current_values
6
12
  end
7
13
 
@@ -24,10 +30,7 @@ module Paperclip
24
30
  destination.rewind
25
31
  destination
26
32
  end
27
-
28
33
  end
29
34
  end
30
35
 
31
- Paperclip.io_adapters.register Paperclip::StringioAdapter do |target|
32
- StringIO === target
33
- end
36
+ Paperclip::StringioAdapter.register
@@ -1,7 +1,13 @@
1
1
  module Paperclip
2
2
  class UploadedFileAdapter < AbstractAdapter
3
- def initialize(target)
4
- @target = target
3
+ def self.register
4
+ Paperclip.io_adapters.register self do |target|
5
+ target.class.name.include?("UploadedFile")
6
+ end
7
+ end
8
+
9
+ def initialize(target, options = {})
10
+ super
5
11
  cache_current_values
6
12
 
7
13
  if @target.respond_to?(:tempfile)
@@ -24,7 +30,7 @@ module Paperclip
24
30
  end
25
31
 
26
32
  def content_type_detector
27
- self.class.content_type_detector
33
+ self.class.content_type_detector || Paperclip::ContentTypeDetector
28
34
  end
29
35
 
30
36
  def determine_content_type
@@ -37,6 +43,4 @@ module Paperclip
37
43
  end
38
44
  end
39
45
 
40
- Paperclip.io_adapters.register Paperclip::UploadedFileAdapter do |target|
41
- target.class.name.include?("UploadedFile")
42
- end
46
+ Paperclip::UploadedFileAdapter.register