paperclip 3.5.4 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.gitignore +0 -6
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -20
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +29 -13
  9. data/Gemfile +11 -3
  10. data/LICENSE +1 -3
  11. data/NEWS +241 -49
  12. data/README.md +471 -166
  13. data/RELEASING.md +17 -0
  14. data/Rakefile +6 -8
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +27 -8
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +36 -28
  19. data/features/step_definitions/html_steps.rb +2 -2
  20. data/features/step_definitions/rails_steps.rb +68 -37
  21. data/features/step_definitions/s3_steps.rb +2 -2
  22. data/features/step_definitions/web_steps.rb +1 -103
  23. data/features/support/env.rb +3 -2
  24. data/features/support/file_helpers.rb +2 -2
  25. data/features/support/fixtures/gemfile.txt +1 -1
  26. data/features/support/paths.rb +1 -1
  27. data/features/support/rails.rb +2 -25
  28. data/gemfiles/4.2.gemfile +17 -0
  29. data/gemfiles/5.0.gemfile +17 -0
  30. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  31. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  32. data/lib/paperclip/attachment.rb +151 -46
  33. data/lib/paperclip/attachment_registry.rb +3 -2
  34. data/lib/paperclip/callbacks.rb +13 -1
  35. data/lib/paperclip/content_type_detector.rb +26 -22
  36. data/lib/paperclip/errors.rb +8 -1
  37. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  38. data/lib/paperclip/geometry_detector_factory.rb +5 -3
  39. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  40. data/lib/paperclip/glue.rb +1 -1
  41. data/lib/paperclip/has_attached_file.rb +17 -1
  42. data/lib/paperclip/helpers.rb +15 -11
  43. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  44. data/lib/paperclip/interpolations.rb +26 -13
  45. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  46. data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
  47. data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
  48. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  49. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  50. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  51. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  53. data/lib/paperclip/io_adapters/registry.rb +6 -2
  54. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  55. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  56. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  57. data/lib/paperclip/locales/en.yml +1 -0
  58. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  59. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  60. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  61. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  62. data/lib/paperclip/media_type_spoof_detector.rb +90 -0
  63. data/lib/paperclip/processor.rb +5 -41
  64. data/lib/paperclip/processor_helpers.rb +50 -0
  65. data/lib/paperclip/rails_environment.rb +25 -0
  66. data/lib/paperclip/schema.rb +9 -7
  67. data/lib/paperclip/storage/filesystem.rb +14 -3
  68. data/lib/paperclip/storage/fog.rb +37 -19
  69. data/lib/paperclip/storage/s3.rb +129 -69
  70. data/lib/paperclip/style.rb +8 -2
  71. data/lib/paperclip/tempfile_factory.rb +5 -1
  72. data/lib/paperclip/thumbnail.rb +30 -18
  73. data/lib/paperclip/url_generator.rb +26 -14
  74. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  75. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  76. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  77. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  78. data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
  79. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  80. data/lib/paperclip/validators.rb +11 -4
  81. data/lib/paperclip/version.rb +3 -1
  82. data/lib/paperclip.rb +30 -11
  83. data/lib/tasks/paperclip.rake +34 -5
  84. data/paperclip.gemspec +21 -16
  85. data/shoulda_macros/paperclip.rb +0 -1
  86. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  87. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
  88. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  89. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +502 -407
  90. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
  91. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  92. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  93. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  94. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  95. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  96. data/spec/paperclip/glue_spec.rb +44 -0
  97. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  98. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +137 -128
  99. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  100. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +132 -0
  101. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
  102. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  103. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  104. data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
  105. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
  106. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  107. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  108. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  109. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  110. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  111. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
  112. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  113. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  114. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  115. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  116. data/spec/paperclip/media_type_spoof_detector_spec.rb +94 -0
  117. data/spec/paperclip/meta_class_spec.rb +30 -0
  118. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  119. data/spec/paperclip/paperclip_spec.rb +192 -0
  120. data/spec/paperclip/plural_cache_spec.rb +37 -0
  121. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  122. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  123. data/spec/paperclip/rails_environment_spec.rb +33 -0
  124. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  125. data/spec/paperclip/schema_spec.rb +248 -0
  126. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  127. data/spec/paperclip/storage/fog_spec.rb +566 -0
  128. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  129. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  130. data/spec/paperclip/style_spec.rb +255 -0
  131. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  132. data/spec/paperclip/tempfile_spec.rb +35 -0
  133. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +153 -134
  134. data/spec/paperclip/url_generator_spec.rb +222 -0
  135. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  136. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  137. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  138. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  139. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  140. data/spec/paperclip/validators_spec.rb +164 -0
  141. data/spec/spec_helper.rb +47 -0
  142. data/spec/support/assertions.rb +82 -0
  143. data/spec/support/conditional_filter_helper.rb +5 -0
  144. data/spec/support/fake_model.rb +25 -0
  145. data/spec/support/fake_rails.rb +12 -0
  146. data/spec/support/fixtures/empty.html +1 -0
  147. data/spec/support/fixtures/empty.xlsx +0 -0
  148. data/spec/support/fixtures/spaced file.jpg +0 -0
  149. data/spec/support/matchers/accept.rb +5 -0
  150. data/spec/support/matchers/exist.rb +5 -0
  151. data/spec/support/matchers/have_column.rb +23 -0
  152. data/{test → spec}/support/mock_attachment.rb +2 -0
  153. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  154. data/spec/support/model_reconstruction.rb +68 -0
  155. data/spec/support/reporting.rb +11 -0
  156. data/spec/support/test_data.rb +13 -0
  157. data/spec/support/version_helper.rb +9 -0
  158. metadata +348 -225
  159. data/RUNNING_TESTS.md +0 -4
  160. data/cucumber/paperclip_steps.rb +0 -6
  161. data/gemfiles/3.0.gemfile +0 -11
  162. data/gemfiles/3.1.gemfile +0 -11
  163. data/gemfiles/3.2.gemfile +0 -11
  164. data/gemfiles/4.0.gemfile +0 -11
  165. data/test/attachment_definitions_test.rb +0 -12
  166. data/test/attachment_registry_test.rb +0 -88
  167. data/test/file_command_content_type_detector_test.rb +0 -27
  168. data/test/filename_cleaner_test.rb +0 -14
  169. data/test/generator_test.rb +0 -84
  170. data/test/geometry_detector_test.rb +0 -24
  171. data/test/has_attached_file_test.rb +0 -125
  172. data/test/helper.rb +0 -232
  173. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  174. data/test/io_adapters/data_uri_adapter_test.rb +0 -74
  175. data/test/io_adapters/empty_string_adapter_test.rb +0 -18
  176. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  177. data/test/io_adapters/identity_adapter_test.rb +0 -8
  178. data/test/io_adapters/uri_adapter_test.rb +0 -102
  179. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  180. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  181. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  182. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  183. data/test/meta_class_test.rb +0 -32
  184. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  185. data/test/paperclip_test.rb +0 -217
  186. data/test/plural_cache_test.rb +0 -36
  187. data/test/schema_test.rb +0 -200
  188. data/test/storage/fog_test.rb +0 -473
  189. data/test/storage/s3_live_test.rb +0 -179
  190. data/test/storage/s3_test.rb +0 -1356
  191. data/test/style_test.rb +0 -213
  192. data/test/support/mock_model.rb +0 -2
  193. data/test/tempfile_factory_test.rb +0 -17
  194. data/test/url_generator_test.rb +0 -187
  195. data/test/validators/attachment_content_type_validator_test.rb +0 -324
  196. data/test/validators_test.rb +0 -61
  197. /data/{test → spec}/database.yml +0 -0
  198. /data/{test → spec/support}/fixtures/12k.png +0 -0
  199. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  200. /data/{test → spec/support}/fixtures/5k.png +0 -0
  201. /data/{test → spec/support}/fixtures/animated +0 -0
  202. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  203. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  204. /data/{test → spec/support}/fixtures/bad.png +0 -0
  205. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  206. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  207. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  208. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  209. /data/{test → spec/support}/fixtures/text.txt +0 -0
  210. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  211. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  212. /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -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
@@ -32,45 +32,49 @@ module Paperclip
32
32
  EMPTY_TYPE
33
33
  elsif calculated_type_matches.any?
34
34
  calculated_type_matches.first
35
- elsif official_type_matches.any?
36
- official_type_matches.first
37
- elsif unofficial_type_matches.any?
38
- unofficial_type_matches.first
39
35
  else
40
- type_from_file_command || SENSIBLE_DEFAULT
36
+ type_from_file_contents || SENSIBLE_DEFAULT
41
37
  end.to_s
42
38
  end
43
39
 
44
40
  private
45
41
 
42
+ def blank_name?
43
+ @filepath.nil? || @filepath.empty?
44
+ end
45
+
46
46
  def empty_file?
47
- File.exists?(@filename) && File.size(@filename) == 0
47
+ File.exist?(@filepath) && File.size(@filepath) == 0
48
48
  end
49
-
49
+
50
50
  alias :empty? :empty_file?
51
51
 
52
- def blank_name?
53
- @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
54
56
  end
55
57
 
56
58
  def possible_types
57
- MIME::Types.type_for(@filename).collect(&:content_type)
58
- end
59
-
60
- def calculated_type_matches
61
- possible_types.select{|content_type| content_type == type_from_file_command }
59
+ MIME::Types.type_for(@filepath).collect(&:content_type)
62
60
  end
63
61
 
64
- def official_type_matches
65
- possible_types.reject{|content_type| content_type.match(/\/x-/) }
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
66
67
  end
67
68
 
68
- def unofficial_type_matches
69
- possible_types.select{|content_type| content_type.match(/\/x-/) }
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
70
73
  end
71
74
 
72
75
  def type_from_file_command
73
- @type_from_file_command ||= FileCommandContentTypeDetector.new(@filename).detect
76
+ @type_from_file_command ||=
77
+ FileCommandContentTypeDetector.new(@filepath).detect
74
78
  end
75
79
  end
76
80
  end
@@ -13,8 +13,15 @@ module Paperclip
13
13
  class CommandNotFoundError < Paperclip::Error
14
14
  end
15
15
 
16
+ # Attachments require a content_type or file_name validator,
17
+ # or to have explicitly opted out of them.
18
+ class MissingRequiredValidatorError < Paperclip::Error
19
+ end
20
+
16
21
  # Will be thrown when ImageMagic cannot determine the uploaded file's
17
- # 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.
18
25
  class NotIdentifiedByImageMagickError < Paperclip::Error
19
26
  end
20
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
-
@@ -14,17 +14,19 @@ module Paperclip
14
14
 
15
15
  def geometry_string
16
16
  begin
17
+ orientation = Paperclip.options[:use_exif_orientation] ?
18
+ "%[exif:orientation]" : "1"
17
19
  Paperclip.run(
18
20
  "identify",
19
- "-format '%wx%h,%[exif:orientation]' :file", {
21
+ "-format '%wx%h,#{orientation}' :file", {
20
22
  :file => "#{path}[0]"
21
23
  }, {
22
24
  :swallow_stderr => true
23
25
  }
24
26
  )
25
- rescue Cocaine::ExitStatusError
27
+ rescue Terrapin::ExitStatusError
26
28
  ""
27
- rescue Cocaine::CommandNotFoundError => e
29
+ rescue Terrapin::CommandNotFoundError => e
28
30
  raise_because_imagemagick_missing
29
31
  end
30
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)
@@ -18,6 +18,7 @@ module Paperclip
18
18
  register_new_attachment
19
19
  add_active_record_callbacks
20
20
  add_paperclip_callbacks
21
+ add_required_validations
21
22
  end
22
23
 
23
24
  private
@@ -77,11 +78,26 @@ module Paperclip
77
78
  Paperclip::AttachmentRegistry.register(@klass, @name, @options)
78
79
  end
79
80
 
81
+ def add_required_validations
82
+ options = Paperclip::Attachment.default_options.deep_merge(@options)
83
+ if options[:validate_media_type] != false
84
+ name = @name
85
+ @klass.validates_media_type_spoof_detection name,
86
+ :if => ->(instance){ instance.send(name).dirty? }
87
+ end
88
+ end
89
+
80
90
  def add_active_record_callbacks
81
91
  name = @name
82
92
  @klass.send(:after_save) { send(name).send(:save) }
83
93
  @klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
84
- @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
85
101
  end
86
102
 
87
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
@@ -10,6 +10,7 @@ module Paperclip
10
10
  # and is not intended for normal use.
11
11
  def self.[]= name, block
12
12
  define_method(name, &block)
13
+ @interpolators_cache = nil
13
14
  end
14
15
 
15
16
  # Hash access of interpolations. Included only for compatibility,
@@ -20,7 +21,7 @@ module Paperclip
20
21
 
21
22
  # Returns a sorted list of all interpolations.
22
23
  def self.all
23
- self.instance_methods(false).sort
24
+ self.instance_methods(false).sort!
24
25
  end
25
26
 
26
27
  # Perform the actual interpolation. Takes the pattern to interpolate
@@ -29,11 +30,15 @@ module Paperclip
29
30
  # an interpolation pattern for Paperclip to use.
30
31
  def self.interpolate pattern, *args
31
32
  pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
32
- all.reverse.inject(pattern) do |result, tag|
33
- result.gsub(/:#{tag}/) do |match|
34
- send( tag, *args )
35
- end
33
+ result = pattern.dup
34
+ interpolators_cache.each do |method, token|
35
+ result.gsub!(token) { send(method, *args) } if result.include?(token)
36
36
  end
37
+ result
38
+ end
39
+
40
+ def self.interpolators_cache
41
+ @interpolators_cache ||= all.reverse!.map! { |method| [method, ":#{method}"] }
37
42
  end
38
43
 
39
44
  def self.plural_cache
@@ -42,7 +47,7 @@ module Paperclip
42
47
 
43
48
  # Returns the filename, the same way as ":basename.:extension" would.
44
49
  def filename attachment, style_name
45
- [ basename(attachment, style_name), extension(attachment, style_name) ].reject(&:blank?).join(".")
50
+ [ basename(attachment, style_name), extension(attachment, style_name) ].delete_if(&:empty?).join(".".freeze)
46
51
  end
47
52
 
48
53
  # Returns the interpolated URL. Will raise an error if the url itself
@@ -85,12 +90,12 @@ module Paperclip
85
90
  # all class names. Calling #class will return the expected class.
86
91
  def class attachment = nil, style_name = nil
87
92
  return super() if attachment.nil? && style_name.nil?
88
- plural_cache.underscore_and_pluralize(attachment.instance.class.to_s)
93
+ plural_cache.underscore_and_pluralize_class(attachment.instance.class)
89
94
  end
90
95
 
91
96
  # Returns the basename of the file. e.g. "file" for "file.jpg"
92
97
  def basename attachment, style_name
93
- attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}\Z/, "")
98
+ File.basename(attachment.original_filename, ".*".freeze)
94
99
  end
95
100
 
96
101
  # Returns the extension of the file. e.g. "jpg" for "file.jpg"
@@ -98,7 +103,15 @@ module Paperclip
98
103
  # of the actual extension.
99
104
  def extension attachment, style_name
100
105
  ((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
101
- File.extname(attachment.original_filename).gsub(/\A\.+/, "")
106
+ File.extname(attachment.original_filename).sub(/\A\.+/, "".freeze)
107
+ end
108
+
109
+ # Returns the dot+extension of the file. e.g. ".jpg" for "file.jpg"
110
+ # If the style has a format defined, it will return the format instead
111
+ # of the actual extension. If the extension is empty, no dot is added.
112
+ def dotextension attachment, style_name
113
+ ext = extension(attachment, style_name)
114
+ ext.empty? ? ext : ".#{ext}"
102
115
  end
103
116
 
104
117
  # Returns an extension based on the content type. e.g. "jpeg" for
@@ -128,7 +141,7 @@ module Paperclip
128
141
  # It's possible, though unlikely, that the mime type is not in the
129
142
  # database, so just use the part after the '/' in the mime type as the
130
143
  # extension.
131
- %r{/([^/]*)\Z}.match(attachment.content_type)[1]
144
+ %r{/([^/]*)\z}.match(attachment.content_type)[1]
132
145
  end
133
146
  end
134
147
 
@@ -162,9 +175,9 @@ module Paperclip
162
175
  def id_partition attachment, style_name
163
176
  case id = attachment.instance.id
164
177
  when Integer
165
- ("%09d" % id).scan(/\d{3}/).join("/")
178
+ ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
166
179
  when String
167
- id.scan(/.{3}/).first(3).join("/")
180
+ id.scan(/.{3}/).first(3).join("/".freeze)
168
181
  else
169
182
  nil
170
183
  end
@@ -173,7 +186,7 @@ module Paperclip
173
186
  # Returns the pluralized form of the attachment name. e.g.
174
187
  # "avatars" for an attachment of :avatar
175
188
  def attachment attachment, style_name
176
- plural_cache.pluralize(attachment.name.to_s.downcase)
189
+ plural_cache.pluralize_symbol(attachment.name)
177
190
  end
178
191
 
179
192
  # Returns the style, or the default style if nil is supplied.
@@ -5,10 +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
+ alias :length :size
10
+
11
+ def initialize(target, options = {})
12
+ @target = target
13
+ @options = options
14
+ end
9
15
 
10
16
  def fingerprint
11
- @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
12
25
  end
13
26
 
14
27
  def read(length = nil, buffer = nil)
@@ -20,6 +33,7 @@ module Paperclip
20
33
  end
21
34
 
22
35
  def original_filename=(new_filename)
36
+ return unless new_filename
23
37
  @original_filename = new_filename.gsub(OS_RESTRICTED_CHARACTERS, "_")
24
38
  end
25
39
 
@@ -34,12 +48,26 @@ module Paperclip
34
48
  private
35
49
 
36
50
  def destination
37
- @destination ||= TempfileFactory.new.generate(original_filename)
51
+ @destination ||= TempfileFactory.new.generate(@original_filename.to_s)
38
52
  end
39
53
 
40
54
  def copy_to_tempfile(src)
41
- FileUtils.cp(src.path, destination.path)
55
+ link_or_copy_file(src.path, destination.path)
42
56
  destination
43
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
44
72
  end
45
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]
@@ -20,17 +27,15 @@ module Paperclip
20
27
  @size = @tempfile.size || @target.size
21
28
  end
22
29
 
23
- def copy_to_tempfile(src)
24
- if src.respond_to? :copy_to_local_file
25
- src.copy_to_local_file(@style, destination.path)
30
+ def copy_to_tempfile(source)
31
+ if source.staged?
32
+ link_or_copy_file(source.staged_path(@style), destination.path)
26
33
  else
27
- FileUtils.cp(src.path(@style), destination.path)
34
+ source.copy_to_local_file(@style, destination.path)
28
35
  end
29
36
  destination
30
37
  end
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,27 +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
- REGEXP = /\Adata:([-\w]+\/[-\w\+]+);base64,(.*)/m
9
+ REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m
5
10
 
6
- def initialize(target_uri)
7
- @target_uri = target_uri
8
- cache_current_values
9
- @tempfile = copy_to_tempfile
11
+ def initialize(target_uri, options = {})
12
+ super(extract_target(target_uri), options)
10
13
  end
11
14
 
12
15
  private
13
16
 
14
- def cache_current_values
15
- self.original_filename = 'base64.txt'
16
- data_uri_parts ||= @target_uri.match(REGEXP) || []
17
- @content_type = data_uri_parts[1] || 'text/plain'
18
- @target = StringIO.new(Base64.decode64(data_uri_parts[2] || ''))
19
- @size = @target.size
17
+ def extract_target(uri)
18
+ data_uri_parts = uri.match(REGEXP) || []
19
+ StringIO.new(Base64.decode64(data_uri_parts[2] || ""))
20
20
  end
21
-
22
21
  end
23
22
  end
24
-
25
- Paperclip.io_adapters.register Paperclip::DataUriAdapter do |target|
26
- String === target && target =~ Paperclip::DataUriAdapter::REGEXP
27
- 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