jr-paperclip 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  4. data/.github/ISSUE_TEMPLATE/custom.md +10 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.github/workflows/reviewdog.yml +23 -0
  7. data/.github/workflows/test.yml +46 -0
  8. data/.gitignore +19 -0
  9. data/.qlty/.gitignore +7 -0
  10. data/.qlty/qlty.toml +89 -0
  11. data/.rubocop.yml +1060 -0
  12. data/Appraisals +29 -0
  13. data/CONTRIBUTING.md +85 -0
  14. data/Gemfile +17 -0
  15. data/LICENSE +25 -0
  16. data/NEWS +567 -0
  17. data/README.md +1083 -0
  18. data/RELEASING.md +17 -0
  19. data/Rakefile +52 -0
  20. data/bin/console +11 -0
  21. data/features/basic_integration.feature +85 -0
  22. data/features/migration.feature +29 -0
  23. data/features/rake_tasks.feature +62 -0
  24. data/features/step_definitions/attachment_steps.rb +121 -0
  25. data/features/step_definitions/html_steps.rb +15 -0
  26. data/features/step_definitions/rails_steps.rb +271 -0
  27. data/features/step_definitions/s3_steps.rb +16 -0
  28. data/features/step_definitions/web_steps.rb +106 -0
  29. data/features/support/env.rb +12 -0
  30. data/features/support/file_helpers.rb +34 -0
  31. data/features/support/fixtures/boot_config.txt +15 -0
  32. data/features/support/fixtures/gemfile.txt +5 -0
  33. data/features/support/fixtures/preinitializer.txt +20 -0
  34. data/features/support/paths.rb +28 -0
  35. data/features/support/rails.rb +39 -0
  36. data/features/support/selectors.rb +19 -0
  37. data/features/support/webmock_setup.rb +8 -0
  38. data/gemfiles/7.0.gemfile +20 -0
  39. data/gemfiles/7.1.gemfile +20 -0
  40. data/gemfiles/7.2.gemfile +20 -0
  41. data/gemfiles/8.0.gemfile +20 -0
  42. data/gemfiles/8.1.gemfile +20 -0
  43. data/lib/generators/paperclip/USAGE +8 -0
  44. data/lib/generators/paperclip/paperclip_generator.rb +36 -0
  45. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
  46. data/lib/jr-paperclip.rb +1 -0
  47. data/lib/paperclip/attachment.rb +634 -0
  48. data/lib/paperclip/attachment_registry.rb +60 -0
  49. data/lib/paperclip/callbacks.rb +42 -0
  50. data/lib/paperclip/content_type_detector.rb +85 -0
  51. data/lib/paperclip/errors.rb +34 -0
  52. data/lib/paperclip/file_command_content_type_detector.rb +28 -0
  53. data/lib/paperclip/filename_cleaner.rb +15 -0
  54. data/lib/paperclip/geometry.rb +157 -0
  55. data/lib/paperclip/geometry_detector_factory.rb +45 -0
  56. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  57. data/lib/paperclip/glue.rb +18 -0
  58. data/lib/paperclip/has_attached_file.rb +116 -0
  59. data/lib/paperclip/helpers.rb +60 -0
  60. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  61. data/lib/paperclip/interpolations.rb +205 -0
  62. data/lib/paperclip/io_adapters/abstract_adapter.rb +75 -0
  63. data/lib/paperclip/io_adapters/attachment_adapter.rb +56 -0
  64. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  65. data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
  66. data/lib/paperclip/io_adapters/file_adapter.rb +26 -0
  67. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  68. data/lib/paperclip/io_adapters/identity_adapter.rb +17 -0
  69. data/lib/paperclip/io_adapters/nil_adapter.rb +37 -0
  70. data/lib/paperclip/io_adapters/registry.rb +36 -0
  71. data/lib/paperclip/io_adapters/stringio_adapter.rb +36 -0
  72. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +44 -0
  73. data/lib/paperclip/io_adapters/uri_adapter.rb +78 -0
  74. data/lib/paperclip/locales/en.yml +18 -0
  75. data/lib/paperclip/locales/fr.yml +18 -0
  76. data/lib/paperclip/locales/gd.yml +20 -0
  77. data/lib/paperclip/logger.rb +21 -0
  78. data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
  79. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +101 -0
  80. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
  81. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +97 -0
  82. data/lib/paperclip/matchers.rb +64 -0
  83. data/lib/paperclip/media_type_spoof_detector.rb +93 -0
  84. data/lib/paperclip/missing_attachment_styles.rb +84 -0
  85. data/lib/paperclip/processor.rb +56 -0
  86. data/lib/paperclip/processor_helpers.rb +52 -0
  87. data/lib/paperclip/rails_environment.rb +21 -0
  88. data/lib/paperclip/railtie.rb +31 -0
  89. data/lib/paperclip/schema.rb +104 -0
  90. data/lib/paperclip/storage/filesystem.rb +99 -0
  91. data/lib/paperclip/storage/fog.rb +262 -0
  92. data/lib/paperclip/storage/s3.rb +497 -0
  93. data/lib/paperclip/storage.rb +3 -0
  94. data/lib/paperclip/style.rb +106 -0
  95. data/lib/paperclip/tempfile.rb +42 -0
  96. data/lib/paperclip/tempfile_factory.rb +22 -0
  97. data/lib/paperclip/thumbnail.rb +131 -0
  98. data/lib/paperclip/url_generator.rb +83 -0
  99. data/lib/paperclip/validators/attachment_content_type_validator.rb +95 -0
  100. data/lib/paperclip/validators/attachment_file_name_validator.rb +82 -0
  101. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +28 -0
  102. data/lib/paperclip/validators/attachment_presence_validator.rb +28 -0
  103. data/lib/paperclip/validators/attachment_size_validator.rb +126 -0
  104. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +29 -0
  105. data/lib/paperclip/validators.rb +73 -0
  106. data/lib/paperclip/version.rb +3 -0
  107. data/lib/paperclip.rb +215 -0
  108. data/lib/tasks/paperclip.rake +140 -0
  109. data/paperclip.gemspec +51 -0
  110. data/shoulda_macros/paperclip.rb +134 -0
  111. data/spec/database.yml +4 -0
  112. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  113. data/spec/paperclip/attachment_processing_spec.rb +79 -0
  114. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  115. data/spec/paperclip/attachment_spec.rb +1617 -0
  116. data/spec/paperclip/content_type_detector_spec.rb +58 -0
  117. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  118. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  119. data/spec/paperclip/geometry_detector_spec.rb +47 -0
  120. data/spec/paperclip/geometry_parser_spec.rb +73 -0
  121. data/spec/paperclip/geometry_spec.rb +267 -0
  122. data/spec/paperclip/glue_spec.rb +63 -0
  123. data/spec/paperclip/has_attached_file_spec.rb +78 -0
  124. data/spec/paperclip/integration_spec.rb +702 -0
  125. data/spec/paperclip/interpolations_spec.rb +270 -0
  126. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  127. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +167 -0
  128. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +88 -0
  129. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  130. data/spec/paperclip/io_adapters/file_adapter_spec.rb +134 -0
  131. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +142 -0
  132. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  133. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
  134. data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
  135. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  136. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  137. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +231 -0
  138. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  139. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +108 -0
  140. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  141. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  142. data/spec/paperclip/media_type_spoof_detector_spec.rb +126 -0
  143. data/spec/paperclip/meta_class_spec.rb +30 -0
  144. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +88 -0
  145. data/spec/paperclip/paperclip_spec.rb +196 -0
  146. data/spec/paperclip/plural_cache_spec.rb +37 -0
  147. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  148. data/spec/paperclip/processor_spec.rb +26 -0
  149. data/spec/paperclip/rails_environment_spec.rb +30 -0
  150. data/spec/paperclip/rake_spec.rb +103 -0
  151. data/spec/paperclip/schema_spec.rb +298 -0
  152. data/spec/paperclip/storage/filesystem_spec.rb +102 -0
  153. data/spec/paperclip/storage/fog_spec.rb +606 -0
  154. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  155. data/spec/paperclip/storage/s3_spec.rb +1974 -0
  156. data/spec/paperclip/style_spec.rb +251 -0
  157. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  158. data/spec/paperclip/tempfile_spec.rb +35 -0
  159. data/spec/paperclip/thumbnail_spec.rb +504 -0
  160. data/spec/paperclip/url_generator_spec.rb +231 -0
  161. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +410 -0
  162. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +249 -0
  163. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
  164. data/spec/paperclip/validators/attachment_size_validator_spec.rb +325 -0
  165. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +48 -0
  166. data/spec/paperclip/validators_spec.rb +179 -0
  167. data/spec/spec_helper.rb +52 -0
  168. data/spec/support/assertions.rb +84 -0
  169. data/spec/support/fake_model.rb +24 -0
  170. data/spec/support/fake_rails.rb +12 -0
  171. data/spec/support/fixtures/12k.png +0 -0
  172. data/spec/support/fixtures/50x50.png +0 -0
  173. data/spec/support/fixtures/5k.png +0 -0
  174. data/spec/support/fixtures/animated +0 -0
  175. data/spec/support/fixtures/animated.gif +0 -0
  176. data/spec/support/fixtures/animated.unknown +0 -0
  177. data/spec/support/fixtures/aws_s3.yml +13 -0
  178. data/spec/support/fixtures/bad.png +1 -0
  179. data/spec/support/fixtures/empty.html +1 -0
  180. data/spec/support/fixtures/empty.xlsx +0 -0
  181. data/spec/support/fixtures/fog.yml +8 -0
  182. data/spec/support/fixtures/rotated.jpg +0 -0
  183. data/spec/support/fixtures/s3.yml +8 -0
  184. data/spec/support/fixtures/sample.xlsm +0 -0
  185. data/spec/support/fixtures/spaced file.jpg +0 -0
  186. data/spec/support/fixtures/spaced file.png +0 -0
  187. data/spec/support/fixtures/text.txt +1 -0
  188. data/spec/support/fixtures/twopage.pdf +0 -0
  189. data/spec/support/fixtures/uppercase.PNG +0 -0
  190. data/spec/support/matchers/accept.rb +5 -0
  191. data/spec/support/matchers/exist.rb +5 -0
  192. data/spec/support/matchers/have_column.rb +23 -0
  193. data/spec/support/mock_attachment.rb +24 -0
  194. data/spec/support/mock_interpolator.rb +24 -0
  195. data/spec/support/mock_url_generator_builder.rb +26 -0
  196. data/spec/support/model_reconstruction.rb +72 -0
  197. data/spec/support/reporting.rb +11 -0
  198. data/spec/support/test_data.rb +13 -0
  199. data/spec/support/version_helper.rb +9 -0
  200. metadata +702 -0
data/lib/paperclip.rb ADDED
@@ -0,0 +1,215 @@
1
+ # Paperclip allows file attachments that are stored in the filesystem. All graphical
2
+ # transformations are done using the Graphics/ImageMagick command line utilities and
3
+ # are stored in Tempfiles until the record is saved. Paperclip does not require a
4
+ # separate model for storing the attachment's information, instead adding a few simple
5
+ # columns to your table.
6
+ #
7
+ # Author:: Jon Yurek
8
+ # Copyright:: Copyright (c) 2008-2011 thoughtbot, inc.
9
+ # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
10
+ #
11
+ # Paperclip defines an attachment as any file, though it makes special considerations
12
+ # for image files. You can declare that a model has an attached file with the
13
+ # +has_attached_file+ method:
14
+ #
15
+ # class User < ActiveRecord::Base
16
+ # has_attached_file :avatar, :styles => { :thumb => "100x100" }
17
+ # end
18
+ #
19
+ # user = User.new
20
+ # user.avatar = params[:user][:avatar]
21
+ # user.avatar.url
22
+ # # => "/users/avatars/4/original_me.jpg"
23
+ # user.avatar.url(:thumb)
24
+ # # => "/users/avatars/4/thumb_me.jpg"
25
+ #
26
+ # See the +has_attached_file+ documentation for more details.
27
+
28
+ require "erb"
29
+ require "digest"
30
+ require "tempfile"
31
+ require "paperclip/version"
32
+ require "paperclip/geometry_parser_factory"
33
+ require "paperclip/geometry_detector_factory"
34
+ require "paperclip/geometry"
35
+ require "paperclip/processor"
36
+ require "paperclip/processor_helpers"
37
+ require "paperclip/tempfile"
38
+ require "paperclip/thumbnail"
39
+ require "paperclip/interpolations/plural_cache"
40
+ require "paperclip/interpolations"
41
+ require "paperclip/tempfile_factory"
42
+ require "paperclip/style"
43
+ require "paperclip/attachment"
44
+ require "paperclip/storage"
45
+ require "paperclip/callbacks"
46
+ require "paperclip/file_command_content_type_detector"
47
+ require "paperclip/media_type_spoof_detector"
48
+ require "paperclip/content_type_detector"
49
+ require "paperclip/glue"
50
+ require "paperclip/errors"
51
+ require "paperclip/missing_attachment_styles"
52
+ require "paperclip/validators"
53
+ require "paperclip/logger"
54
+ require "paperclip/helpers"
55
+ require "paperclip/has_attached_file"
56
+ require "paperclip/attachment_registry"
57
+ require "paperclip/filename_cleaner"
58
+ require "paperclip/rails_environment"
59
+
60
+ begin
61
+ # Use mime/types/columnar if available, for reduced memory usage
62
+ require "mime/types/columnar"
63
+ rescue LoadError
64
+ require "mime/types"
65
+ end
66
+
67
+ require "marcel"
68
+ require "logger"
69
+ require "terrapin"
70
+
71
+ require "paperclip/railtie" if defined?(Rails::Railtie)
72
+
73
+ # The base module that gets included in ActiveRecord::Base. See the
74
+ # documentation for Paperclip::ClassMethods for more useful information.
75
+ module Paperclip
76
+ extend Helpers
77
+ extend Logger
78
+ extend ProcessorHelpers
79
+
80
+ # Provides configurability to Paperclip. The options available are:
81
+ # * whiny: Will raise an error if Paperclip cannot process thumbnails of
82
+ # an uploaded image. Defaults to true.
83
+ # * log: Logs progress to the Rails log. Uses ActiveRecord's logger, so honors
84
+ # log levels, etc. Defaults to true.
85
+ # * command_path: Defines the path at which to find the command line
86
+ # programs if they are not visible to Rails the system's search path. Defaults to
87
+ # nil, which uses the first executable found in the user's search path.
88
+ # * use_exif_orientation: Whether to inspect EXIF data to determine an
89
+ # image's orientation. Defaults to true.
90
+ def self.options
91
+ @options ||= {
92
+ command_path: nil,
93
+ content_type_mappings: {},
94
+ log: true,
95
+ log_command: true,
96
+ read_timeout: nil,
97
+ swallow_stderr: true,
98
+ use_exif_orientation: true,
99
+ whiny: true,
100
+ is_windows: Gem.win_platform?,
101
+ add_validation_errors_to: :both
102
+ }
103
+ end
104
+
105
+ def self.io_adapters=(new_registry)
106
+ @io_adapters = new_registry
107
+ end
108
+
109
+ def self.io_adapters
110
+ @io_adapters ||= Paperclip::AdapterRegistry.new
111
+ end
112
+
113
+ module ClassMethods
114
+ # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
115
+ # is typically a file stored somewhere on the filesystem and has been uploaded by a user.
116
+ # The attribute returns a Paperclip::Attachment object which handles the management of
117
+ # that file. The intent is to make the attachment as much like a normal attribute. The
118
+ # thumbnails will be created when the new file is assigned, but they will *not* be saved
119
+ # until +save+ is called on the record. Likewise, if the attribute is set to +nil+ is
120
+ # called on it, the attachment will *not* be deleted until +save+ is called. See the
121
+ # Paperclip::Attachment documentation for more specifics. There are a number of options
122
+ # you can set to change the behavior of a Paperclip attachment:
123
+ # * +url+: The full URL of where the attachment is publicly accessible. This can just
124
+ # as easily point to a directory served directly through Apache as it can to an action
125
+ # that can control permissions. You can specify the full domain and path, but usually
126
+ # just an absolute path is sufficient. The leading slash *must* be included manually for
127
+ # absolute paths. The default value is
128
+ # "/system/:class/:attachment/:id_partition/:style/:filename". See
129
+ # Paperclip::Attachment#interpolate for more information on variable interpolaton.
130
+ # :url => "/:class/:attachment/:id/:style_:filename"
131
+ # :url => "http://some.other.host/stuff/:class/:id_:extension"
132
+ # Note: When using the +s3+ storage option, the +url+ option expects
133
+ # particular values. See the Paperclip::Storage::S3#url documentation for
134
+ # specifics.
135
+ # * +default_url+: The URL that will be returned if there is no attachment assigned.
136
+ # This field is interpolated just as the url is. The default value is
137
+ # "/:attachment/:style/missing.png"
138
+ # has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png"
139
+ # User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
140
+ # * +styles+: A hash of thumbnail styles and their geometries. You can find more about
141
+ # geometry strings at the ImageMagick website
142
+ # (http://www.imagemagick.org/script/command-line-options.php#resize). Paperclip
143
+ # also adds the "#" option (e.g. "50x50#"), which will resize the image to fit maximally
144
+ # inside the dimensions and then crop the rest off (weighted at the center). The
145
+ # default value is to generate no thumbnails.
146
+ # * +default_style+: The thumbnail style that will be used by default URLs.
147
+ # Defaults to +original+.
148
+ # has_attached_file :avatar, :styles => { :normal => "100x100#" },
149
+ # :default_style => :normal
150
+ # user.avatar.url # => "/avatars/23/normal_me.png"
151
+ # * +keep_old_files+: Keep the existing attachment files (original + resized) from
152
+ # being automatically deleted when an attachment is cleared or updated. Defaults to +false+.
153
+ # * +preserve_files+: Keep the existing attachment files in all cases, even if the parent
154
+ # record is destroyed. Defaults to +false+.
155
+ # * +whiny+: Will raise an error if Paperclip cannot post_process an uploaded file due
156
+ # to a command line error. This will override the global setting for this attachment.
157
+ # Defaults to true.
158
+ # * +convert_options+: When creating thumbnails, use this free-form options
159
+ # array to pass in various convert command options. Typical options are "-strip" to
160
+ # remove all Exif data from the image (save space for thumbnails and avatars) or
161
+ # "-depth 8" to specify the bit depth of the resulting conversion. See ImageMagick
162
+ # convert documentation for more options: (http://www.imagemagick.org/script/convert.php)
163
+ # Note that this option takes a hash of options, each of which correspond to the style
164
+ # of thumbnail being generated. You can also specify :all as a key, which will apply
165
+ # to all of the thumbnails being generated. If you specify options for the :original,
166
+ # it would be best if you did not specify destructive options, as the intent of keeping
167
+ # the original around is to regenerate all the thumbnails when requirements change.
168
+ # has_attached_file :avatar, :styles => { :large => "300x300", :negative => "100x100" }
169
+ # :convert_options => {
170
+ # :all => "-strip",
171
+ # :negative => "-negate"
172
+ # }
173
+ # NOTE: While not deprecated yet, it is not recommended to specify options this way.
174
+ # It is recommended that :convert_options option be included in the hash passed to each
175
+ # :styles for compatibility with future versions.
176
+ # NOTE: Strings supplied to :convert_options are split on space in order to undergo
177
+ # shell quoting for safety. If your options require a space, please pre-split them
178
+ # and pass an array to :convert_options instead.
179
+ # * +storage+: Chooses the storage backend where the files will be stored. The current
180
+ # choices are :filesystem, :fog and :s3. The default is :filesystem. Make sure you read the
181
+ # documentation for Paperclip::Storage::Filesystem, Paperclip::Storage::Fog and Paperclip::Storage::S3
182
+ # for backend-specific options.
183
+ #
184
+ # It's also possible for you to dynamically define your interpolation string for :url,
185
+ # :default_url, and :path in your model by passing a method name as a symbol as a argument
186
+ # for your has_attached_file definition:
187
+ #
188
+ # class Person
189
+ # has_attached_file :avatar, :default_url => :default_url_by_gender
190
+ #
191
+ # private
192
+ #
193
+ # def default_url_by_gender
194
+ # "/assets/avatars/default_#{gender}.png"
195
+ # end
196
+ # end
197
+ def has_attached_file(name, options = {})
198
+ HasAttachedFile.define_on(self, name, options)
199
+ end
200
+ end
201
+ end
202
+
203
+ # This stuff needs to be run after Paperclip is defined.
204
+ require "paperclip/io_adapters/registry"
205
+ require "paperclip/io_adapters/abstract_adapter"
206
+ require "paperclip/io_adapters/empty_string_adapter"
207
+ require "paperclip/io_adapters/identity_adapter"
208
+ require "paperclip/io_adapters/file_adapter"
209
+ require "paperclip/io_adapters/stringio_adapter"
210
+ require "paperclip/io_adapters/data_uri_adapter"
211
+ require "paperclip/io_adapters/nil_adapter"
212
+ require "paperclip/io_adapters/attachment_adapter"
213
+ require "paperclip/io_adapters/uploaded_file_adapter"
214
+ require "paperclip/io_adapters/uri_adapter"
215
+ require "paperclip/io_adapters/http_url_proxy_adapter"
@@ -0,0 +1,140 @@
1
+ require "paperclip/attachment_registry"
2
+
3
+ module Paperclip
4
+ module Task
5
+ def self.obtain_class
6
+ class_name = ENV["CLASS"] || ENV["class"]
7
+ raise "Must specify CLASS" unless class_name
8
+
9
+ class_name
10
+ end
11
+
12
+ def self.obtain_attachments(klass)
13
+ klass = Paperclip.class_for(klass.to_s)
14
+ name = ENV["ATTACHMENT"] || ENV["attachment"]
15
+
16
+ attachment_names = Paperclip::AttachmentRegistry.names_for(klass)
17
+
18
+ raise "Class #{klass.name} has no attachments specified" if attachment_names.empty?
19
+
20
+ if name.present? && attachment_names.map(&:to_s).include?(name.to_s)
21
+ [name]
22
+ else
23
+ attachment_names
24
+ end
25
+ end
26
+
27
+ def self.log_error(error)
28
+ $stderr.puts error
29
+ end
30
+ end
31
+ end
32
+
33
+ namespace :paperclip do
34
+ desc "Refreshes both metadata and thumbnails."
35
+ task refresh: ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
36
+
37
+ namespace :refresh do
38
+ desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT and STYLES splitted by comma)."
39
+ task thumbnails: :environment do
40
+ klass = Paperclip::Task.obtain_class
41
+ names = Paperclip::Task.obtain_attachments(klass)
42
+ styles = (ENV["STYLES"] || ENV["styles"] || "").split(",").map(&:to_sym)
43
+ names.each do |name|
44
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
45
+ attachment = instance.send(name)
46
+ begin
47
+ attachment.reprocess!(*styles)
48
+ rescue StandardError => e
49
+ Paperclip::Task.log_error("exception while processing #{klass} ID #{instance.id}:")
50
+ Paperclip::Task.log_error(" " + e.message + "\n")
51
+ end
52
+ unless instance.errors.blank?
53
+ Paperclip::Task.log_error("errors while processing #{klass} ID #{instance.id}:")
54
+ Paperclip::Task.log_error(" " + instance.errors.full_messages.join("\n ") + "\n")
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
61
+ task metadata: :environment do
62
+ klass = Paperclip::Task.obtain_class
63
+ names = Paperclip::Task.obtain_attachments(klass)
64
+ names.each do |name|
65
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
66
+ attachment = instance.send(name)
67
+ if file = Paperclip.io_adapters.for(attachment, attachment.options[:adapter_options])
68
+ instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
69
+ instance.send("#{name}_content_type=", file.content_type.to_s.strip)
70
+ instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
71
+ instance.save(validate: false)
72
+ else
73
+ true
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ desc "Regenerates missing thumbnail styles for all classes using Paperclip."
80
+ task missing_styles: :environment do
81
+ Rails.application.eager_load!
82
+ Paperclip.missing_attachments_styles.each do |klass, attachment_definitions|
83
+ attachment_definitions.each do |attachment_name, missing_styles|
84
+ puts "Regenerating #{klass} -> #{attachment_name} -> #{missing_styles.inspect}"
85
+ ENV["CLASS"] = klass.to_s
86
+ ENV["ATTACHMENT"] = attachment_name.to_s
87
+ ENV["STYLES"] = missing_styles.join(",")
88
+ Rake::Task["paperclip:refresh:thumbnails"].execute
89
+ end
90
+ end
91
+ Paperclip.save_current_attachments_styles!
92
+ end
93
+
94
+ desc "Regenerates fingerprints for a given CLASS (and optional ATTACHMENT). Useful when changing digest."
95
+ task fingerprints: :environment do
96
+ klass = Paperclip::Task.obtain_class
97
+ names = Paperclip::Task.obtain_attachments(klass)
98
+ names.each do |name|
99
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
100
+ attachment = instance.send(name)
101
+ attachment.assign(attachment)
102
+ instance.save(validate: false)
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ desc "Cleans out invalid attachments. Useful after you've added new validations."
109
+ task clean: :environment do
110
+ klass = Paperclip::Task.obtain_class
111
+ names = Paperclip::Task.obtain_attachments(klass)
112
+ names.each do |name|
113
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
114
+ unless instance.valid?
115
+ attributes = %w(file_size file_name content_type).map { |suffix| "#{name}_#{suffix}".to_sym }
116
+ if attributes.any? { |attribute| instance.errors[attribute].present? }
117
+ instance.send("#{name}=", nil)
118
+ instance.save(validate: false)
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ desc "find missing attachments. Useful to know which attachments are broken"
126
+ task find_broken_attachments: :environment do
127
+ klass = Paperclip::Task.obtain_class
128
+ names = Paperclip::Task.obtain_attachments(klass)
129
+ names.each do |name|
130
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
131
+ attachment = instance.send(name)
132
+ if attachment.exists?
133
+ print "."
134
+ else
135
+ Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
data/paperclip.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
+ require "paperclip/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "jr-paperclip"
6
+ s.version = Paperclip::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.author = "Jukka Rautanen"
9
+ s.email = ["jukka.rautanen@gmail.com"]
10
+ s.homepage = "https://github.com/jukra/jr-paperclip"
11
+ s.summary = "File attachments as attributes for ActiveRecord"
12
+ s.description = "Easy upload management for ActiveRecord"
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.post_install_message = File.read("UPGRADING") if File.exist?("UPGRADING")
21
+
22
+ s.requirements << "ImageMagick"
23
+ s.required_ruby_version = ">= 3.2.0"
24
+
25
+ s.add_dependency("activemodel", ">= 7.0.0")
26
+ s.add_dependency("activesupport", ">= 7.0.0")
27
+ s.add_dependency("marcel", ">= 1.0.1")
28
+ s.add_dependency("mime-types")
29
+ s.add_dependency("terrapin", ">= 0.6.0", "< 2.0")
30
+
31
+ s.add_development_dependency("activerecord", ">= 7.0.0")
32
+ s.add_development_dependency("appraisal")
33
+ s.add_development_dependency("aruba", "~> 2.0")
34
+ s.add_development_dependency("aws-sdk-s3")
35
+ s.add_development_dependency("bundler")
36
+ s.add_development_dependency("capybara")
37
+ s.add_development_dependency("cucumber", "~> 9.0")
38
+ s.add_development_dependency("cucumber-rails", "~> 3.0")
39
+ s.add_development_dependency("fog-aws")
40
+ s.add_development_dependency("fog-local")
41
+ s.add_development_dependency("generator_spec")
42
+ s.add_development_dependency("launchy")
43
+ s.add_development_dependency("nokogiri")
44
+ s.add_development_dependency("ostruct")
45
+ s.add_development_dependency("railties")
46
+ s.add_development_dependency("rake")
47
+ s.add_development_dependency("rspec", "~> 3.0")
48
+ s.add_development_dependency("shoulda")
49
+ s.add_development_dependency("timecop")
50
+ s.add_development_dependency("webmock")
51
+ end
@@ -0,0 +1,134 @@
1
+ require "paperclip/matchers"
2
+
3
+ module Paperclip
4
+ # =Paperclip Shoulda Macros
5
+ #
6
+ # These macros are intended for use with shoulda, and will be included into
7
+ # your tests automatically. All of the macros use the standard shoulda
8
+ # assumption that the name of the test is based on the name of the model
9
+ # you're testing (that is, UserTest is the test for the User model), and
10
+ # will load that class for testing purposes.
11
+ module Shoulda
12
+ include Matchers
13
+ # This will test whether you have defined your attachment correctly by
14
+ # checking for all the required fields exist after the definition of the
15
+ # attachment.
16
+ def should_have_attached_file(name)
17
+ klass = self.name.gsub(/Test$/, "").constantize
18
+ matcher = have_attached_file name
19
+ should matcher.description do
20
+ assert_accepts(matcher, klass)
21
+ end
22
+ end
23
+
24
+ # Tests for validations on the presence of the attachment.
25
+ def should_validate_attachment_presence(name)
26
+ klass = self.name.gsub(/Test$/, "").constantize
27
+ matcher = validate_attachment_presence name
28
+ should matcher.description do
29
+ assert_accepts(matcher, klass)
30
+ end
31
+ end
32
+
33
+ # Tests that you have content_type validations specified. There are two
34
+ # options, :valid and :invalid. Both accept an array of strings. The
35
+ # strings should be a list of content types which will pass and fail
36
+ # validation, respectively.
37
+ def should_validate_attachment_content_type(name, options = {})
38
+ klass = self.name.gsub(/Test$/, "").constantize
39
+ valid = [options[:valid]].flatten
40
+ invalid = [options[:invalid]].flatten
41
+ matcher = validate_attachment_content_type(name).allowing(valid).rejecting(invalid)
42
+ should matcher.description do
43
+ assert_accepts(matcher, klass)
44
+ end
45
+ end
46
+
47
+ # Tests to ensure that you have file size validations turned on. You
48
+ # can pass the same options to this that you can to
49
+ # validate_attachment_file_size - :less_than, :greater_than, and :in.
50
+ # :less_than checks that a file is less than a certain size, :greater_than
51
+ # checks that a file is more than a certain size, and :in takes a Range or
52
+ # Array which specifies the lower and upper limits of the file size.
53
+ def should_validate_attachment_size(name, options = {})
54
+ klass = self.name.gsub(/Test$/, "").constantize
55
+ min = options[:greater_than] || (options[:in] && options[:in].first) || 0
56
+ max = options[:less_than] || (options[:in] && options[:in].last) || (1.0 / 0)
57
+ range = (min..max)
58
+ matcher = validate_attachment_size(name).in(range)
59
+ should matcher.description do
60
+ assert_accepts(matcher, klass)
61
+ end
62
+ end
63
+
64
+ # Stubs the HTTP PUT for an attachment using S3 storage.
65
+ #
66
+ # @example
67
+ # stub_paperclip_s3('user', 'avatar', 'png')
68
+ def stub_paperclip_s3(model, attachment, _extension)
69
+ definition = model.gsub(" ", "_").classify.constantize.
70
+ attachment_definitions[attachment.to_sym]
71
+
72
+ path = "http://s3.amazonaws.com/:id/#{definition[:path]}"
73
+ path.gsub!(/:([^\/\.]+)/) do |_match|
74
+ "([^\/\.]+)"
75
+ end
76
+
77
+ begin
78
+ FakeWeb.register_uri(:put, Regexp.new(path), body: "OK")
79
+ rescue NameError
80
+ raise NameError, "the stub_paperclip_s3 shoulda macro requires the fakeweb gem."
81
+ end
82
+ end
83
+
84
+ # Stub S3 and return a file for attachment. Best with Factory Girl.
85
+ # Uses a strict directory convention:
86
+ #
87
+ # features/support/paperclip
88
+ #
89
+ # This method is used by the Paperclip-provided Cucumber step:
90
+ #
91
+ # When I attach a "demo_tape" "mp3" file to a "band" on S3
92
+ #
93
+ # @example
94
+ # Factory.define :band_with_demo_tape, :parent => :band do |band|
95
+ # band.demo_tape { band.paperclip_fixture("band", "demo_tape", "png") }
96
+ # end
97
+ def paperclip_fixture(model, attachment, extension)
98
+ stub_paperclip_s3(model, attachment, extension)
99
+ base_path = File.join(File.dirname(__FILE__), "..", "..",
100
+ "features", "support", "paperclip")
101
+ File.new(File.join(base_path, model, "#{attachment}.#{extension}"))
102
+ end
103
+ end
104
+ end
105
+
106
+ if defined?(ActionDispatch::Integration::Session)
107
+ class ActionDispatch::IntegrationTest::Session #:nodoc:
108
+ include Paperclip::Shoulda
109
+ end
110
+ elsif defined?(ActionController::Integration::Session)
111
+ class ActionController::Integration::Session #:nodoc:
112
+ include Paperclip::Shoulda
113
+ end
114
+ end
115
+
116
+ if defined?(FactoryGirl::Factory)
117
+ class FactoryGirl::Factory
118
+ include Paperclip::Shoulda #:nodoc:
119
+ end
120
+ else
121
+ class Factory
122
+ include Paperclip::Shoulda #:nodoc:
123
+ end
124
+ end
125
+
126
+ if defined?(Minitest)
127
+ class Minitest::Unit::TestCase #:nodoc:
128
+ extend Paperclip::Shoulda
129
+ end
130
+ elsif defined?(Test)
131
+ class Test::Unit::TestCase #:nodoc:
132
+ extend Paperclip::Shoulda
133
+ end
134
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe "Attachment Definitions" do
4
+ it "returns all of the attachments on the class" do
5
+ reset_class "Dummy"
6
+ Dummy.has_attached_file :avatar, path: "abc"
7
+ Dummy.has_attached_file :other_attachment, url: "123"
8
+ Dummy.do_not_validate_attachment_file_type :avatar
9
+ expected = { avatar: { path: "abc" }, other_attachment: { url: "123" } }
10
+
11
+ expect(Dummy.attachment_definitions).to eq expected
12
+ end
13
+ end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+
3
+ describe "Attachment Processing" do
4
+ before { rebuild_class }
5
+
6
+ context "using validates_attachment_content_type" do
7
+ it "processes attachments given a valid assignment" do
8
+ file = File.new(fixture_file("5k.png"))
9
+ Dummy.validates_attachment_content_type :avatar, content_type: "image/png"
10
+ instance = Dummy.new
11
+ attachment = instance.avatar
12
+ expect(attachment).to receive(:post_process_styles)
13
+
14
+ attachment.assign(file)
15
+ end
16
+
17
+ it "does not process attachments given an invalid assignment with :not" do
18
+ file = File.new(fixture_file("5k.png"))
19
+ Dummy.validates_attachment_content_type :avatar, not: "image/png"
20
+ instance = Dummy.new
21
+ attachment = instance.avatar
22
+ expect(attachment).not_to receive(:post_process_styles)
23
+
24
+ attachment.assign(file)
25
+ end
26
+
27
+ it "does not process attachments given an invalid assignment with :content_type" do
28
+ file = File.new(fixture_file("5k.png"))
29
+ Dummy.validates_attachment_content_type :avatar, content_type: "image/tiff"
30
+ instance = Dummy.new
31
+ attachment = instance.avatar
32
+ expect(attachment).not_to receive(:post_process_styles)
33
+
34
+ attachment.assign(file)
35
+ end
36
+
37
+ it "allows what would be an invalid assignment when validation :if clause returns false" do
38
+ invalid_assignment = File.new(fixture_file("5k.png"))
39
+ Dummy.validates_attachment_content_type :avatar, content_type: "image/tiff", if: lambda { false }
40
+ instance = Dummy.new
41
+ attachment = instance.avatar
42
+ expect(attachment).to receive(:post_process_styles)
43
+
44
+ attachment.assign(invalid_assignment)
45
+ end
46
+ end
47
+
48
+ context "using validates_attachment" do
49
+ it "processes attachments given a valid assignment" do
50
+ file = File.new(fixture_file("5k.png"))
51
+ Dummy.validates_attachment :avatar, content_type: { content_type: "image/png" }
52
+ instance = Dummy.new
53
+ attachment = instance.avatar
54
+ expect(attachment).to receive(:post_process_styles)
55
+
56
+ attachment.assign(file)
57
+ end
58
+
59
+ it "does not process attachments given an invalid assignment with :not" do
60
+ file = File.new(fixture_file("5k.png"))
61
+ Dummy.validates_attachment :avatar, content_type: { not: "image/png" }
62
+ instance = Dummy.new
63
+ attachment = instance.avatar
64
+ expect(attachment).not_to receive(:post_process_styles)
65
+
66
+ attachment.assign(file)
67
+ end
68
+
69
+ it "does not process attachments given an invalid assignment with :content_type" do
70
+ file = File.new(fixture_file("5k.png"))
71
+ Dummy.validates_attachment :avatar, content_type: { content_type: "image/tiff" }
72
+ instance = Dummy.new
73
+ attachment = instance.avatar
74
+ expect(attachment).not_to receive(:post_process_styles)
75
+
76
+ attachment.assign(file)
77
+ end
78
+ end
79
+ end