diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 8c5750148..42ca79d6c 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -62,7 +62,12 @@ class Attachment < ApplicationRecord def thumb_url return '' unless file.attached? && image? - url_for(file.representation(resize_to_fill: [250, nil])) + begin + url_for(file.representation(resize_to_fill: [250, nil])) + rescue ActiveStorage::UnrepresentableError => e + Rails.logger.warn "Unrepresentable image attachment: #{id} (#{file.filename}) - #{e.message}" + '' + end end def with_attached_file? diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb index 0b03a56ad..cc00eab5d 100644 --- a/spec/models/attachment_spec.rb +++ b/spec/models/attachment_spec.rb @@ -82,6 +82,16 @@ RSpec.describe Attachment do expect(attachment.thumb_url).to be_present end + + it 'handles unrepresentable images gracefully' do + attachment = message.attachments.create!(account_id: message.account_id, file_type: :image) + attachment.file.attach(io: StringIO.new('fake image'), filename: 'test.jpg', content_type: 'image/jpeg') + + allow(attachment.file).to receive(:representation).and_raise(ActiveStorage::UnrepresentableError.new('Cannot represent')) + + expect(Rails.logger).to receive(:warn).with(/Unrepresentable image attachment: #{attachment.id}/) + expect(attachment.thumb_url).to eq('') + end end describe 'meta data handling' do