paperclip 4.3.7 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) 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 +105 -31
  10. data/README.md +239 -153
  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/generators/paperclip/paperclip_generator.rb +9 -1
  24. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  25. data/lib/paperclip.rb +13 -12
  26. data/lib/paperclip/attachment.rb +25 -14
  27. data/lib/paperclip/attachment_registry.rb +2 -1
  28. data/lib/paperclip/callbacks.rb +8 -6
  29. data/lib/paperclip/content_type_detector.rb +3 -2
  30. data/lib/paperclip/errors.rb +3 -1
  31. data/lib/paperclip/file_command_content_type_detector.rb +1 -1
  32. data/lib/paperclip/geometry_detector_factory.rb +2 -2
  33. data/lib/paperclip/glue.rb +1 -1
  34. data/lib/paperclip/has_attached_file.rb +7 -1
  35. data/lib/paperclip/helpers.rb +15 -11
  36. data/lib/paperclip/interpolations.rb +1 -1
  37. data/lib/paperclip/io_adapters/abstract_adapter.rb +29 -3
  38. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  39. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  40. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  41. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  42. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  43. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  44. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  45. data/lib/paperclip/io_adapters/registry.rb +6 -2
  46. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  47. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  48. data/lib/paperclip/io_adapters/uri_adapter.rb +17 -14
  49. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  50. data/lib/paperclip/media_type_spoof_detector.rb +3 -2
  51. data/lib/paperclip/processor.rb +5 -4
  52. data/lib/paperclip/schema.rb +2 -8
  53. data/lib/paperclip/storage/filesystem.rb +13 -2
  54. data/lib/paperclip/storage/fog.rb +20 -13
  55. data/lib/paperclip/storage/s3.rb +87 -58
  56. data/lib/paperclip/thumbnail.rb +18 -8
  57. data/lib/paperclip/url_generator.rb +17 -13
  58. data/lib/paperclip/validators.rb +1 -1
  59. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  60. data/lib/paperclip/version.rb +3 -1
  61. data/lib/tasks/paperclip.rake +18 -4
  62. data/paperclip.gemspec +13 -10
  63. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  64. data/spec/paperclip/attachment_registry_spec.rb +28 -0
  65. data/spec/paperclip/attachment_spec.rb +72 -18
  66. data/spec/paperclip/content_type_detector_spec.rb +1 -1
  67. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
  68. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  69. data/spec/paperclip/integration_spec.rb +4 -3
  70. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +76 -22
  71. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  72. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  73. data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
  74. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +18 -1
  75. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  76. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  77. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
  78. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  79. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +48 -3
  80. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  81. data/spec/paperclip/media_type_spoof_detector_spec.rb +15 -0
  82. data/spec/paperclip/paperclip_spec.rb +15 -45
  83. data/spec/paperclip/processor_spec.rb +4 -4
  84. data/spec/paperclip/storage/fog_spec.rb +31 -0
  85. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  86. data/spec/paperclip/storage/s3_spec.rb +357 -190
  87. data/spec/paperclip/tempfile_spec.rb +35 -0
  88. data/spec/paperclip/thumbnail_spec.rb +38 -35
  89. data/spec/paperclip/url_generator_spec.rb +54 -43
  90. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  91. data/spec/paperclip/validators_spec.rb +5 -5
  92. data/spec/spec_helper.rb +6 -2
  93. data/spec/support/assertions.rb +12 -1
  94. data/spec/support/conditional_filter_helper.rb +5 -0
  95. data/spec/support/mock_attachment.rb +2 -0
  96. data/spec/support/mock_url_generator_builder.rb +2 -2
  97. data/spec/support/model_reconstruction.rb +9 -1
  98. data/spec/support/reporting.rb +11 -0
  99. metadata +66 -58
  100. data/cucumber/paperclip_steps.rb +0 -6
  101. data/gemfiles/4.1.gemfile +0 -19
  102. data/lib/paperclip/deprecations.rb +0 -42
  103. data/lib/paperclip/locales/de.yml +0 -18
  104. data/lib/paperclip/locales/es.yml +0 -18
  105. data/lib/paperclip/locales/ja.yml +0 -18
  106. data/lib/paperclip/locales/pt-BR.yml +0 -18
  107. data/lib/paperclip/locales/zh-CN.yml +0 -18
  108. data/lib/paperclip/locales/zh-HK.yml +0 -18
  109. data/lib/paperclip/locales/zh-TW.yml +0 -18
  110. data/spec/paperclip/deprecations_spec.rb +0 -65
  111. data/spec/support/deprecations.rb +0 -9
  112. data/spec/support/rails_helpers.rb +0 -7
@@ -2,6 +2,7 @@
2
2
  require 'uri'
3
3
  require 'paperclip/url_generator'
4
4
  require 'active_support/deprecation'
5
+ require 'active_support/core_ext/string/inflections'
5
6
 
6
7
  module Paperclip
7
8
  # The Attachment class manages the files for a given attachment. It saves
@@ -32,6 +33,7 @@ module Paperclip
32
33
  :use_timestamp => true,
33
34
  :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
34
35
  :validate_media_type => true,
36
+ :adapter_options => { hash_digest: Digest::MD5 },
35
37
  :check_validity_before_processing => true
36
38
  }
37
39
  end
@@ -49,7 +51,8 @@ module Paperclip
49
51
  # +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
50
52
  # +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
51
53
  # +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
54
+ # +only_process+ - style args to be run through the post-processor. This defaults to the empty list (which is
55
+ # a special case that indicates all styles should be processed)
53
56
  # +default_url+ - a URL for the missing image
54
57
  # +default_style+ - the style to use when an argument is not specified e.g. #url, #path
55
58
  # +storage+ - the storage mechanism. Defaults to :filesystem
@@ -81,7 +84,7 @@ module Paperclip
81
84
  @errors = {}
82
85
  @dirty = false
83
86
  @interpolator = options[:interpolator]
84
- @url_generator = options[:url_generator].new(self, @options)
87
+ @url_generator = options[:url_generator].new(self)
85
88
  @source_file_options = options[:source_file_options]
86
89
  @whiny = options[:whiny]
87
90
 
@@ -95,7 +98,8 @@ module Paperclip
95
98
  # attachment:
96
99
  # new_user.avatar = old_user.avatar
97
100
  def assign(uploaded_file)
98
- @file = Paperclip.io_adapters.for(uploaded_file)
101
+ @file = Paperclip.io_adapters.for(uploaded_file,
102
+ @options[:adapter_options])
99
103
  ensure_required_accessors!
100
104
  ensure_required_validations!
101
105
 
@@ -236,6 +240,10 @@ module Paperclip
236
240
  # the instance's errors and returns false, cancelling the save.
237
241
  def save
238
242
  flush_deletes unless @options[:keep_old_files]
243
+ process = only_process
244
+ if process.any? && !process.include?(:original)
245
+ @queued_for_write.except!(:original)
246
+ end
239
247
  flush_writes
240
248
  @dirty = false
241
249
  true
@@ -322,7 +330,7 @@ module Paperclip
322
330
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
323
331
  end
324
332
 
325
- # This method really shouldn't be called that often. It's expected use is
333
+ # This method really shouldn't be called that often. Its expected use is
326
334
  # in the paperclip:refresh rake task and that's it. It will regenerate all
327
335
  # thumbnails forcefully, by reobtaining the original file and going through
328
336
  # the post-process again.
@@ -347,7 +355,7 @@ module Paperclip
347
355
 
348
356
  # Returns true if a file has been assigned.
349
357
  def file?
350
- !original_filename.blank?
358
+ original_filename.present?
351
359
  end
352
360
 
353
361
  alias :present? :file?
@@ -426,7 +434,7 @@ module Paperclip
426
434
  def assign_attributes
427
435
  @queued_for_write[:original] = @file
428
436
  assign_file_information
429
- assign_fingerprint(@file.fingerprint)
437
+ assign_fingerprint { @file.fingerprint }
430
438
  assign_timestamps
431
439
  end
432
440
 
@@ -436,9 +444,9 @@ module Paperclip
436
444
  instance_write(:file_size, @file.size)
437
445
  end
438
446
 
439
- def assign_fingerprint(fingerprint)
447
+ def assign_fingerprint
440
448
  if instance_respond_to?(:fingerprint)
441
- instance_write(:fingerprint, fingerprint)
449
+ instance_write(:fingerprint, yield)
442
450
  end
443
451
  end
444
452
 
@@ -464,7 +472,7 @@ module Paperclip
464
472
 
465
473
  def reset_file_if_original_reprocessed
466
474
  instance_write(:file_size, @queued_for_write[:original].size)
467
- assign_fingerprint(@queued_for_write[:original].fingerprint)
475
+ assign_fingerprint { @queued_for_write[:original].fingerprint }
468
476
  reset_updater
469
477
  end
470
478
 
@@ -500,7 +508,7 @@ module Paperclip
500
508
 
501
509
  instance.run_paperclip_callbacks(:post_process) do
502
510
  instance.run_paperclip_callbacks(:"#{name}_post_process") do
503
- unless @options[:check_validity_before_processing] && instance.errors.any?
511
+ if !@options[:check_validity_before_processing] || !instance.errors.any?
504
512
  post_process_styles(*style_args)
505
513
  end
506
514
  end
@@ -518,15 +526,18 @@ module Paperclip
518
526
  begin
519
527
  raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
520
528
  intermediate_files = []
529
+ original = @queued_for_write[:original]
521
530
 
522
- @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
531
+ @queued_for_write[name] = style.processors.
532
+ reduce(original) do |file, processor|
523
533
  file = Paperclip.processor(processor).make(file, style.processor_options, self)
524
- intermediate_files << file
534
+ intermediate_files << file unless file == @queued_for_write[:original]
525
535
  file
526
536
  end
527
537
 
528
538
  unadapted_file = @queued_for_write[name]
529
- @queued_for_write[name] = Paperclip.io_adapters.for(@queued_for_write[name])
539
+ @queued_for_write[name] = Paperclip.io_adapters.
540
+ for(@queued_for_write[name], @options[:adapter_options])
530
541
  unadapted_file.close if unadapted_file.respond_to?(:close)
531
542
  @queued_for_write[name]
532
543
  rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
@@ -586,7 +597,7 @@ module Paperclip
586
597
 
587
598
  # You can either specifiy :restricted_characters or you can define your own
588
599
  # :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.
600
+ # the filename that will be cleaned. It should return the cleaned filename.
590
601
  def filename_cleaner
591
602
  @options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
592
603
  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
@@ -24,9 +24,9 @@ module Paperclip
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+.
@@ -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,22 @@ 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, Errno::EEXIST => e
65
+ Paperclip.log(
66
+ "Link failed with #{e.message}; copying link #{src} to #{dest}"
67
+ )
68
+ FileUtils.cp(src, dest)
69
+ @destination.close
70
+ @destination.open.binmode
71
+ end
46
72
  end
47
73
  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