Restore thumbnails for photos recorded without dimensions

Photos uploaded before image processing was installed are marked analyzed
with no width, so the "unreadable image" check showed them as file links.
Every resizable image gets a thumbnail again; one that can't be read still
falls back to the original when resizing fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-14 13:26:28 -05:00
1 parent 908c943e21
commit a2672b99a3
4 files changed
+17 -20

No files matched your search

+3 -2
View File
@@ -87,9 +87,10 @@ class Aircraft < ApplicationRecord
def picture_shown? = picture.attached? && picture.attachment.persisted? def picture_shown? = picture.attached? && picture.attachment.persisted?
# The photo at a page's size (:thumb or :card), or the original when it # The photo at a page's size (:thumb or :card), or the original when it
# can't be resized (see ActiveStorage::Blob#resizable_image?). # isn't a kind that can be resized. (One that can't be read is sent as the
# original too: see config/initializers/active_storage_images.rb.)
def picture_for(size) def picture_for(size)
picture.blob.resizable_image? ? picture.variant(size) : picture picture.variable? ? picture.variant(size) : picture
end end
private private
+1 -2
View File
@@ -2,8 +2,7 @@
(which has Download); other files open in a new tab and have a download link. %> (which has Download); other files open in a new tab and have a download link. %>
<% files = squawk.attachments.to_a %> <% files = squawk.attachments.to_a %>
<% if files.any? %> <% if files.any? %>
<%# Thumbnails for images that can be resized; others are listed as files. %> <% photos, others = files.partition { _1.image? && _1.variable? } %>
<% photos, others = files.partition { _1.image? && _1.blob.resizable_image? } %>
<div class="mt-1" data-controller="gallery"> <div class="mt-1" data-controller="gallery">
<ul class="flex flex-wrap gap-1.5"> <ul class="flex flex-wrap gap-1.5">
<% photos.each do |file| %> <% photos.each do |file| %>
+2 -10
View File
@@ -1,15 +1,7 @@
# Uploads that claim to be images but can't be read as one (empty, corrupt, # Uploads that claim to be images but can't be read as one (empty, corrupt,
# or another kind of file with an image name). # or another kind of file with an image name): if resizing fails, send the
# original rather than an error.
Rails.application.config.to_prepare do Rails.application.config.to_prepare do
ActiveStorage::Blob.class_eval do
# Worth resizing: an image kind that the upload check didn't find
# unreadable. (Until that check has run, assume it's fine.)
def resizable_image?
variable? && !(metadata[:analyzed] && metadata[:width].blank?)
end
end
# If resizing fails anyway, send the original rather than an error.
ActiveStorage::Representations::BaseController.rescue_from(Vips::Error, ActiveStorage::InvariableError) do ActiveStorage::Representations::BaseController.rescue_from(Vips::Error, ActiveStorage::InvariableError) do
redirect_to Rails.application.routes.url_helpers.rails_blob_path(@blob, disposition: params[:disposition], only_path: true) redirect_to Rails.application.routes.url_helpers.rails_blob_path(@blob, disposition: params[:disposition], only_path: true)
end end
+11 -6
View File
@@ -75,13 +75,18 @@ class MalformedInputTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity assert_response :unprocessable_entity
end end
test "an image that can't be read is shown as the file, never resized" do test "photos measured without dimensions (uploaded before image processing) still get thumbnails" do
@a.picture.attach(io: StringIO.new("GIF89a not really"), filename: "broken.gif", content_type: "image/gif") jpeg = Vips::Image.black(400, 300).jpegsave_buffer
@a.picture.blob.update!(metadata: { identified: true, analyzed: true }) # analysis found no dimensions @a.picture.attach(io: StringIO.new(jpeg), filename: "old.jpg", content_type: "image/jpeg")
assert_kind_of ActiveStorage::Attached::One, @a.picture_for(:thumb) @a.picture.blob.update!(metadata: { identified: true, analyzed: true }) # as older uploads were recorded
get aircraft_path(@a) get aircraft_path(@a)
assert_response :success assert_select "img[src*='/representations/']"
assert_select "img[src*='/representations/']", 0
squawk = @a.squawks.create!(issue_reported: "Leak", reported_by: @owner)
squawk.attachments.attach(io: StringIO.new(jpeg), filename: "PXL_1.jpg", content_type: "image/jpeg")
squawk.attachments.first.blob.update!(metadata: { identified: true, analyzed: true })
get aircraft_squawks_path(@a)
assert_select "a[data-gallery-target=item] img[src*='/representations/']"
end end
test "if resizing a photo fails anyway, the original is sent instead of an error" do test "if resizing a photo fails anyway, the original is sent instead of an error" do