I have one Rails 7.1 app running in production on Fly.io using ActiveStorage with S3.
The app allows profile creation from Google Oauth flow, so right after the successful authentication for the first time I have the following methods which reads the google oauth image url, request the 2500width resize and then attach the avatar, called from the OmniauthCallbacksController:
def set_avatar_from_auth(auth)
if auth.dig(:info, :image).present?
image_url = auth.dig(:info, :image)
# Replace the size parameter in the URL to request a larger image
# This example changes the size to 2500x2500 pixels
large_image_url = image_url.sub(/bsd{2,4}-cb/, 's2500-c')
attach_image_from_url(large_image_url)
else
attach_default_avatar(profile.female?)
end
end
def attach_image_from_url(image_url)
begin
profile.create_avatar.image.attach(
io: URI.open(image_url),
filename: "#{SecureRandom.hex}.jpg",
content_type: 'image/jpeg'
)
rescue => e
Rails.logger.error("[User] - Error attaching image por User with id #{id}from URL: #{e.message}")
attach_default_avatar(profile.female?)
end
end
Then I redirect the user to the onboarding page, where at the 3rd step, I have the avatar confirmation/replacement screen:
<%= picture_tag(class: "img-fluid shadow-lg border-0 p-0 align-self-center") do %>
<%= tag :source, srcset: url_for(current_profile.avatar&.image&.representation(:thumb) || "") %>
<%= image_tag (current_profile.female? ? 'avatar-girl.png' : 'avatar-boy.png'), alt: current_profile.username, size: '340x340' %>
<% end %>
Then, not every time but randomly I have this exception:
ActiveRecord::RecordNotFound active_storage/representations/redirect#show Couldn't find ActiveStorage::Blob with 'id'=xxxx
I suspect there is a race condition but I’m not sure and I can’t reproduce on development.
Can anybody who had similar issue help me please?
Thanks in advance