Please excuse me for my poor English.
In our system, we use ActiveStorage in Rails to manage images. This time, we updated Rails from 6.0.3.6 to 6.1.3.2 and encountered the following problem. I would like to know the cause and how to solve the problem.
Problems that occurred
The URL to the images stored in S3 changed before and after the update, and images stored before the update could not be displayed.
Steps to reproduce
- Update Rails from 6.0.3.6 to 6.1.3.2
- Run
rails active_storage:update - Run
rails db:migrate - Open the page with the images that were saved before the Rails update
Source Code
class Image < ApplicationRecord
include ActiveStorageSupport::SupportForBase64
after_commit :process_variants
has_one_base64_attached :file
def provider_file_url
key = file.variant(resize_to_limit: [500, 500]).key
"#{ENV[‘BASE_URL']}/#{key}"
end
private
def process_variants
return unless file.attachment
file.variant(resize_to_limit: [500, 500]).processed
rescue StandardError
nil
end
end
# Call URL
image_instance.provider_file_url
Expected behavior
The same URL is generated before and after the Rails update.
Actual behavior
Generated URLs are different before and after Rails update.
Example:
Before update
https://sample.net/variants/zvd2z2dafhrht3yy99l8w9zw54h8/3a8af9a41326f81dcc439e015ab90b5d9d078e2b00dd7d021c4d4ee650e5c6e0
After update
https://sample.net/variants/zvd2z2dafhrht3yy99l8w9zw54h8/ac54d233b136b9f97f4d984f115d2e1c4695c2a0a3994c28d347637301445431
What I investigated
I compared the behavior of ActiveStorage 6.0.3.6 and 6.1.3.2, and found the following differences. However, I have not been able to get to the cause of this.
Added debug code in two places ↓.
① initialize of ActiveStorage::Variant class (source code)
class ActiveStorage::Variant
# omitted
def initialize(blob, variation_or_variation_key)
p '-----------------------------------------------------------------------------------------'
p 'blob'
p blob
p 'variation_or_variation_key'
p variation_or_variation_key
p '-----------------------------------------------------------------------------------------'
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key)
end
# omitted
end
② encode method of ActiveStorage::Variation class (source code)
class ActiveStorage::Variation
attr_reader :transformations
class << self
# omitted
def encode(transformations)
p '-----------------------------------------------------------------------------------------'
p transformations
p '-----------------------------------------------------------------------------------------'
ActiveStorage.verifier.generate(transformations, purpose: :variation)
end
end
# omitted
end
Run provider_file_url method
①
ActiveStorage6.0.3.6
"-----------------------------------------------------------------------------------------"
"blob"
#<ActiveStorage::Blob id: 999, key: "zvd2z2dafhrht3yy99l8w9zw54h7", filename: "sample.jpg", content_type: "image/jpeg", metadata: {"identified"=>true, "analyzed"=>true}, byte_size: 15789, checksum: "NwtXS6KRF1ty1WxXMicUew==", created_at: "2021-06-02 08:32:15">
"variation_or_variation_key"
{:resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
ActiveStorage6.1.3.2
"-----------------------------------------------------------------------------------------"
"blob"
#<ActiveStorage::Blob id: 999, key: "zvd2z2dafhrht3yy99l8w9zw54h7", filename: "sample.jpg", content_type: "image/jpeg", metadata: {"identified"=>true, "analyzed"=>true}, byte_size: 15789, checksum: "NwtXS6KRF1ty1WxXMicUew==", created_at: "2021-06-02 17:32:15.000000000 +0900", service_name: "amazon">
"variation_or_variation_key"
#<ActiveStorage::Variation:0x00007fa2db230430 @transformations={:format=>"jpg", :resize_to_limit=>[500, 500]}>
"-----------------------------------------------------------------------------------------"
②
ActiveStorage6.0.3.6
"-----------------------------------------------------------------------------------------"
{:resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
ActiveStorage6.1.3.2
"-----------------------------------------------------------------------------------------"
{:format=>"jpg", :resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
As a test, I added a process to remove the :format key in the encode method, and now the URL is generated as before the update.
System configuration
Before update
Rails: 6.0.3.6
Ruby: 2.6.6
Gemfile.lock
activestorage (6.0.3.6)
actionpack (= 6.0.3.6)
activejob (= 6.0.3.6)
activerecord (= 6.0.3.6)
marcel (~> 1.0.0)
After update
Rails version: 6.1.3.2
Ruby version: 2.6.6
Gemfile.lock
activestorage (6.1.3.2)
actionpack (= 6.1.3.2)
activejob (= 6.1.3.2)
activerecord (= 6.1.3.2)
activesupport (= 6.1.3.2)
marcel (~> 1.0.0)
mini_mime (~> 1.0.2)