paperclip 4.2.2 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -15
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +19 -8
  9. data/Gemfile +5 -9
  10. data/LICENSE +1 -1
  11. data/MIGRATING-ES.md +317 -0
  12. data/MIGRATING.md +375 -0
  13. data/NEWS +184 -31
  14. data/README.md +371 -201
  15. data/RELEASING.md +17 -0
  16. data/Rakefile +2 -2
  17. data/UPGRADING +12 -9
  18. data/features/basic_integration.feature +10 -6
  19. data/features/migration.feature +0 -24
  20. data/features/step_definitions/attachment_steps.rb +41 -35
  21. data/features/step_definitions/html_steps.rb +2 -2
  22. data/features/step_definitions/rails_steps.rb +39 -38
  23. data/features/step_definitions/s3_steps.rb +2 -2
  24. data/features/step_definitions/web_steps.rb +1 -103
  25. data/features/support/env.rb +1 -0
  26. data/features/support/file_helpers.rb +2 -2
  27. data/features/support/paths.rb +1 -1
  28. data/features/support/rails.rb +0 -24
  29. data/gemfiles/4.2.gemfile +6 -8
  30. data/gemfiles/5.0.gemfile +17 -0
  31. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  32. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  33. data/lib/paperclip/attachment.rb +51 -26
  34. data/lib/paperclip/attachment_registry.rb +3 -2
  35. data/lib/paperclip/callbacks.rb +8 -6
  36. data/lib/paperclip/content_type_detector.rb +27 -11
  37. data/lib/paperclip/errors.rb +3 -1
  38. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  39. data/lib/paperclip/filename_cleaner.rb +0 -1
  40. data/lib/paperclip/geometry_detector_factory.rb +3 -3
  41. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  42. data/lib/paperclip/glue.rb +1 -1
  43. data/lib/paperclip/has_attached_file.rb +9 -2
  44. data/lib/paperclip/helpers.rb +15 -11
  45. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  46. data/lib/paperclip/interpolations.rb +24 -14
  47. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  48. data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
  49. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  50. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  51. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  53. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  54. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  55. data/lib/paperclip/io_adapters/registry.rb +6 -2
  56. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  57. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  58. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  59. data/lib/paperclip/logger.rb +1 -1
  60. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  61. data/lib/paperclip/media_type_spoof_detector.rb +13 -9
  62. data/lib/paperclip/processor.rb +15 -6
  63. data/lib/paperclip/rails_environment.rb +25 -0
  64. data/lib/paperclip/schema.rb +4 -10
  65. data/lib/paperclip/storage/filesystem.rb +13 -2
  66. data/lib/paperclip/storage/fog.rb +33 -20
  67. data/lib/paperclip/storage/s3.rb +89 -70
  68. data/lib/paperclip/style.rb +0 -1
  69. data/lib/paperclip/thumbnail.rb +24 -12
  70. data/lib/paperclip/url_generator.rb +17 -13
  71. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  72. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
  73. data/lib/paperclip/validators.rb +1 -1
  74. data/lib/paperclip/version.rb +3 -1
  75. data/lib/paperclip.rb +27 -13
  76. data/lib/tasks/paperclip.rake +33 -3
  77. data/paperclip.gemspec +18 -15
  78. data/spec/paperclip/attachment_definitions_spec.rb +1 -1
  79. data/spec/paperclip/attachment_processing_spec.rb +2 -5
  80. data/spec/paperclip/attachment_registry_spec.rb +84 -13
  81. data/spec/paperclip/attachment_spec.rb +147 -41
  82. data/spec/paperclip/content_type_detector_spec.rb +9 -2
  83. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -2
  84. data/spec/paperclip/filename_cleaner_spec.rb +0 -1
  85. data/spec/paperclip/geometry_spec.rb +1 -1
  86. data/spec/paperclip/glue_spec.rb +44 -0
  87. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  88. data/spec/paperclip/integration_spec.rb +42 -5
  89. data/spec/paperclip/interpolations_spec.rb +21 -9
  90. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +106 -23
  91. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  92. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  93. data/spec/paperclip/io_adapters/file_adapter_spec.rb +6 -3
  94. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +51 -14
  95. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  96. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  97. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +5 -1
  98. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  99. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +126 -8
  100. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  101. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
  102. data/spec/paperclip/media_type_spoof_detector_spec.rb +75 -11
  103. data/spec/paperclip/paperclip_spec.rb +15 -40
  104. data/spec/paperclip/plural_cache_spec.rb +17 -16
  105. data/spec/paperclip/processor_spec.rb +4 -4
  106. data/spec/paperclip/rails_environment_spec.rb +33 -0
  107. data/spec/paperclip/schema_spec.rb +46 -46
  108. data/spec/paperclip/storage/fog_spec.rb +63 -3
  109. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  110. data/spec/paperclip/storage/s3_spec.rb +400 -215
  111. data/spec/paperclip/style_spec.rb +0 -1
  112. data/spec/paperclip/tempfile_factory_spec.rb +4 -0
  113. data/spec/paperclip/tempfile_spec.rb +35 -0
  114. data/spec/paperclip/thumbnail_spec.rb +59 -38
  115. data/spec/paperclip/url_generator_spec.rb +55 -45
  116. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  117. data/spec/paperclip/validators_spec.rb +5 -5
  118. data/spec/spec_helper.rb +7 -1
  119. data/spec/support/assertions.rb +12 -1
  120. data/spec/support/fake_model.rb +4 -0
  121. data/spec/support/fixtures/empty.xlsx +0 -0
  122. data/spec/support/matchers/have_column.rb +11 -2
  123. data/spec/support/mock_attachment.rb +2 -0
  124. data/spec/support/mock_url_generator_builder.rb +2 -2
  125. data/spec/support/model_reconstruction.rb +11 -3
  126. data/spec/support/reporting.rb +11 -0
  127. metadata +110 -63
  128. data/RUNNING_TESTS.md +0 -4
  129. data/cucumber/paperclip_steps.rb +0 -6
  130. data/gemfiles/3.2.gemfile +0 -19
  131. data/gemfiles/4.0.gemfile +0 -19
  132. data/gemfiles/4.1.gemfile +0 -19
  133. data/lib/paperclip/locales/de.yml +0 -18
  134. data/lib/paperclip/locales/es.yml +0 -18
  135. data/lib/paperclip/locales/ja.yml +0 -18
  136. data/lib/paperclip/locales/pt-BR.yml +0 -18
  137. data/lib/paperclip/locales/zh-CN.yml +0 -18
  138. data/lib/paperclip/locales/zh-HK.yml +0 -18
  139. data/lib/paperclip/locales/zh-TW.yml +0 -18
  140. data/spec/support/mock_model.rb +0 -2
  141. data/spec/support/rails_helpers.rb +0 -7
data/MIGRATING.md ADDED
@@ -0,0 +1,375 @@
1
+ # Migrating from Paperclip to ActiveStorage
2
+
3
+ Paperclip and ActiveStorage solve similar problems with similar solutions, so
4
+ transitioning from one to the other is straightforward data re-writing.
5
+
6
+ The process of going from Paperclip to ActiveStorage is as follows:
7
+
8
+ 1. Apply the ActiveStorage database migrations.
9
+ 2. Configure storage.
10
+ 3. Copy the database data over.
11
+ 4. Copy the files over.
12
+ 5. Update your tests.
13
+ 6. Update your views.
14
+ 7. Update your controllers.
15
+ 8. Update your models.
16
+
17
+ ## Apply the ActiveStorage database migrations
18
+
19
+ Follow [the instructions for installing ActiveStorage]. You'll very likely want
20
+ to add the `mini_magick` gem to your Gemfile.
21
+
22
+ ```sh
23
+ rails active_storage:install
24
+ ```
25
+
26
+ [the instructions for installing ActiveStorage]: https://github.com/rails/rails/blob/master/activestorage/README.md#installation
27
+
28
+ ## Configure storage
29
+
30
+ Again, follow [the instructions for configuring ActiveStorage].
31
+
32
+ [the instructions for configuring ActiveStorage]: http://edgeguides.rubyonrails.org/active_storage_overview.html#setup
33
+
34
+ ## Copy the database data over
35
+
36
+ The `active_storage_blobs` and `active_storage_attachments` tables are where
37
+ ActiveStorage expects to find file metadata. Paperclip stores the file metadata
38
+ directly on the associated object's table.
39
+
40
+ You'll need to write a migration for this conversion. Because the models for
41
+ your domain are involved, it's tricky to supply a simple script. But we'll try!
42
+
43
+ Here's how it would go for a `User` with an `avatar`, that is this in
44
+ Paperclip:
45
+
46
+ ```ruby
47
+ class User < ApplicationRecord
48
+ has_attached_file :avatar
49
+ end
50
+ ```
51
+
52
+ Your Paperclip migrations will produce a table like so:
53
+
54
+ ```ruby
55
+ create_table "users", force: :cascade do |t|
56
+ t.string "avatar_file_name"
57
+ t.string "avatar_content_type"
58
+ t.integer "avatar_file_size"
59
+ t.datetime "avatar_updated_at"
60
+ end
61
+ ```
62
+
63
+ And you'll be converting into these tables:
64
+
65
+ ```ruby
66
+ create_table "active_storage_attachments", force: :cascade do |t|
67
+ t.string "name", null: false
68
+ t.string "record_type", null: false
69
+ t.integer "record_id", null: false
70
+ t.integer "blob_id", null: false
71
+ t.datetime "created_at", null: false
72
+ t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
73
+ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
74
+ end
75
+ ```
76
+
77
+ ```ruby
78
+ create_table "active_storage_blobs", force: :cascade do |t|
79
+ t.string "key", null: false
80
+ t.string "filename", null: false
81
+ t.string "content_type"
82
+ t.text "metadata"
83
+ t.bigint "byte_size", null: false
84
+ t.string "checksum", null: false
85
+ t.datetime "created_at", null: false
86
+ t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
87
+ end
88
+ ```
89
+
90
+ So, assuming you want to leave the files in the exact same place, _this is
91
+ your migration_. Otherwise, see the next section first and modify the migration
92
+ to taste.
93
+
94
+ ```ruby
95
+ Dir[Rails.root.join("app/models/**/*.rb")].sort.each { |file| require file }
96
+
97
+ class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
98
+ require 'open-uri'
99
+
100
+ def up
101
+ # postgres
102
+ get_blob_id = 'LASTVAL()'
103
+ # mariadb
104
+ # get_blob_id = 'LAST_INSERT_ID()'
105
+ # sqlite
106
+ # get_blob_id = 'LAST_INSERT_ROWID()'
107
+
108
+ active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL)
109
+ INSERT INTO active_storage_blobs (
110
+ `key`, filename, content_type, metadata, byte_size, checksum, created_at
111
+ ) VALUES (?, ?, ?, '{}', ?, ?, ?)
112
+ SQL
113
+
114
+ active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL)
115
+ INSERT INTO active_storage_attachments (
116
+ name, record_type, record_id, blob_id, created_at
117
+ ) VALUES (?, ?, ?, #{get_blob_id}, ?)
118
+ SQL
119
+
120
+ models = ActiveRecord::Base.descendants.reject(&:abstract_class?)
121
+
122
+ transaction do
123
+ models.each do |model|
124
+ attachments = model.column_names.map do |c|
125
+ if c =~ /(.+)_file_name$/
126
+ $1
127
+ end
128
+ end.compact
129
+
130
+ model.find_each.each do |instance|
131
+ attachments.each do |attachment|
132
+ active_storage_blob_statement.execute(
133
+ key(instance, attachment),
134
+ instance.send("#{attachment}_file_name"),
135
+ instance.send("#{attachment}_content_type"),
136
+ instance.send("#{attachment}_file_size"),
137
+ checksum(instance.send(attachment)),
138
+ instance.updated_at.iso8601
139
+ )
140
+
141
+ active_storage_attachment_statement.
142
+ execute(attachment, model.name, instance.id, instance.updated_at.iso8601)
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ active_storage_attachment_statement.close
149
+ active_storage_blob_statement.close
150
+ end
151
+
152
+ def down
153
+ raise ActiveRecord::IrreversibleMigration
154
+ end
155
+
156
+ private
157
+
158
+ def key(instance, attachment)
159
+ SecureRandom.uuid
160
+ # Alternatively:
161
+ # instance.send("#{attachment}_file_name")
162
+ end
163
+
164
+ def checksum(attachment)
165
+ # local files stored on disk:
166
+ url = attachment.path
167
+ Digest::MD5.base64digest(File.read(url))
168
+
169
+ # remote files stored on another person's computer:
170
+ # url = attachment.url
171
+ # Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
172
+ end
173
+ end
174
+ ```
175
+
176
+ ## Copy the files over
177
+
178
+ The above migration leaves the files as they are. However, the default
179
+ Paperclip and ActiveStorage storage services use different locations.
180
+
181
+ By default, Paperclip looks like this:
182
+
183
+ ```
184
+ public/system/users/avatars/000/000/004/original/the-mystery-of-life.png
185
+ ```
186
+
187
+ And ActiveStorage looks like this:
188
+
189
+ ```
190
+ storage/xM/RX/xMRXuT6nqpoiConJFQJFt6c9
191
+ ```
192
+
193
+ That `xMRXuT6nqpoiConJFQJFt6c9` is the `active_storage_blobs.key` value. In the
194
+ migration above we simply used the filename but you may wish to use a UUID
195
+ instead.
196
+
197
+
198
+ ### Moving local storage files
199
+
200
+ ```ruby
201
+ #!bin/rails runner
202
+
203
+ class ActiveStorageBlob < ActiveRecord::Base
204
+ end
205
+
206
+ class ActiveStorageAttachment < ActiveRecord::Base
207
+ belongs_to :blob, class_name: 'ActiveStorageBlob'
208
+ belongs_to :record, polymorphic: true
209
+ end
210
+
211
+ ActiveStorageAttachment.find_each do |attachment|
212
+ name = attachment.name
213
+
214
+ source = attachment.record.send(name).path
215
+ dest_dir = File.join(
216
+ "storage",
217
+ attachment.blob.key.first(2),
218
+ attachment.blob.key.first(4).last(2))
219
+ dest = File.join(dest_dir, attachment.blob.key)
220
+
221
+ FileUtils.mkdir_p(dest_dir)
222
+ puts "Moving #{source} to #{dest}"
223
+ FileUtils.cp(source, dest)
224
+ end
225
+ ```
226
+
227
+ ### Moving files on a remote host (S3, Azure Storage, GCS, etc.)
228
+
229
+ One of the most straightforward ways to move assets stored on a remote host is
230
+ to use a rake task that regenerates the file names and places them in the
231
+ proper file structure/hierarchy.
232
+
233
+ Assuming you have a model configured similarly to the example below:
234
+
235
+ ```ruby
236
+ class Organization < ApplicationRecord
237
+ # New ActiveStorage declaration
238
+ has_one_attached :logo
239
+
240
+ # Old Paperclip config
241
+ # must be removed BEFORE to running the rake task so that
242
+ # all of the new ActiveStorage goodness can be used when
243
+ # calling organization.logo
244
+ has_attached_file :logo,
245
+ path: "/organizations/:id/:basename_:style.:extension",
246
+ default_url: "https://s3.amazonaws.com/xxxxx/organizations/missing_:style.jpg",
247
+ default_style: :normal,
248
+ styles: { thumb: "64x64#", normal: "400x400>" },
249
+ convert_options: { thumb: "-quality 100 -strip", normal: "-quality 75 -strip" }
250
+ end
251
+ ```
252
+
253
+ The following rake task would migrate all of your assets:
254
+
255
+ ```ruby
256
+ namespace :organizations do
257
+ task migrate_to_active_storage: :environment do
258
+ Organization.where.not(logo_file_name: nil).find_each do |organization|
259
+ # This step helps us catch any attachments we might have uploaded that
260
+ # don't have an explicit file extension in the filename
261
+ image = organization.logo_file_name
262
+ ext = File.extname(image)
263
+ image_original = URI.unescape(image.gsub(ext, "_original#{ext}"))
264
+
265
+ # this url pattern can be changed to reflect whatever service you use
266
+ logo_url = "https://s3.amazonaws.com/xxxxx/organizations/#{organization.id}/#{image_original}"
267
+ organization.logo.attach(io: open(logo_url),
268
+ filename: organization.logo_file_name,
269
+ content_type: organization.logo_content_type)
270
+ end
271
+ end
272
+ end
273
+ ```
274
+
275
+ An added advantage of this method is that you're creating a copy of all assets,
276
+ which is handy in the event you need to rollback your deploy.
277
+
278
+ This also means that you can run the rake task from your development machine
279
+ and completely migrate the assets before your deploy, minimizing the chances
280
+ that you'll have a timed-out deployment.
281
+
282
+ The main drawback of this method is the same as its benefit - you are
283
+ essentially duplicating all of your assets. These days storage and bandwidth
284
+ are relatively cheap, but in some instances where you have a huge volume of
285
+ files, or very large file sizes, this might get a little less feasible.
286
+
287
+ In my experience I was able to move tens of thousands of images in a matter of
288
+ a couple of hours, just by running the migration overnight on my MacBook Pro.
289
+
290
+ Once you've confirmed that the migration and deploy have gone successfully you
291
+ can safely delete the old assets from your remote host.
292
+
293
+ ## Update your tests
294
+
295
+ Instead of the `have_attached_file` matcher, you'll need to write your own.
296
+ Here's one that is similar in spirit to the Paperclip-supplied matcher:
297
+
298
+ ```ruby
299
+ RSpec::Matchers.define :have_attached_file do |name|
300
+ matches do |record|
301
+ file = record.send(name)
302
+ file.respond_to?(:variant) && file.respond_to?(:attach)
303
+ end
304
+ end
305
+ ```
306
+
307
+ ## Update your views
308
+
309
+ In Paperclip it looks like this:
310
+
311
+ ```ruby
312
+ image_tag @user.avatar.url(:medium)
313
+ ```
314
+
315
+ In ActiveStorage it looks like this:
316
+
317
+ ```ruby
318
+ image_tag @user.avatar.variant(resize: "250x250")
319
+ ```
320
+
321
+ ## Update your controllers
322
+
323
+ This should _require_ no update. However, if you glance back at the database
324
+ schema above, you may notice a join.
325
+
326
+ For example, if your controller has
327
+
328
+ ```ruby
329
+ def index
330
+ @users = User.all.order(:name)
331
+ end
332
+ ```
333
+
334
+ And your view has
335
+
336
+ ```
337
+ <ul>
338
+ <% @users.each do |user| %>
339
+ <li><%= image_tag user.avatar.variant(resize: "10x10"), alt: user.name %></li>
340
+ <% end %>
341
+ </ul>
342
+ ```
343
+
344
+ Then you'll end up with an n+1 as you load each attachment in the loop.
345
+
346
+ So while the controller and model will work without change, you will want to
347
+ double-check your loops and add `includes` as needed. ActiveStorage adds an
348
+ `avatar_attachment` and `avatar_blob` relationship to has-one relations, and
349
+ `avatar_attachments` and `avatar_blobs` to has-many:
350
+
351
+ ```ruby
352
+ def index
353
+ @users = User.all.order(:name).includes(:avatar_attachment)
354
+ end
355
+ ```
356
+
357
+ ## Update your models
358
+
359
+ Follow [the guide on attaching files to records]. For example, a `User` with an
360
+ `avatar` is represented as:
361
+
362
+ ```ruby
363
+ class User < ApplicationRecord
364
+ has_one_attached :avatar
365
+ end
366
+ ```
367
+
368
+ Any resizing is done in the view as a variant.
369
+
370
+ [the guide on attaching files to records]: http://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-files-to-records
371
+
372
+ ## Remove Paperclip
373
+
374
+ Remove the Gem from your `Gemfile` and run `bundle`. Run your tests because
375
+ you're done!