Don't try to render image preview if image is corrupt on S3 and its URL can be generated

This commit is contained in:
Cillian O'Ruanaidh
2025-05-10 12:35:40 +01:00
parent d8959fd7a5
commit 119218b2b5
4 changed files with 43 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
= render "admin/shared/attachment_field", object: @enterprise, attachment_url: @enterprise.logo_url(:thumb), attachment_name: :logo, f: f
= render "admin/shared/attachment_field", attachment_url: @enterprise.logo_url(:thumb), attachment_name: :logo, f: f
= render "admin/shared/attachment_field", object: @enterprise, attachment_url: @enterprise.promo_image_url(:large), attachment_name: :promo_image, f: f do
= render "admin/shared/attachment_field", attachment_url: @enterprise.promo_image_url(:large), attachment_name: :promo_image, f: f do
%br/
= t('.promo_image_placeholder')
%br

View File

@@ -7,7 +7,7 @@
= f.check_box :hide_ofn_navigation, { "data-controller": "checkbox-display", "data-checkbox-display-target-id-value": "white_label_logo" }
%div{id: "white_label_logo"}
= render "admin/shared/attachment_field", object: @object, attachment_url: @object.white_label_logo_url, attachment_name: :white_label_logo, f: f
= render "admin/shared/attachment_field", attachment_url: @object.white_label_logo_url, attachment_name: :white_label_logo, f: f
- if @object.white_label_logo.present?
.row

View File

@@ -7,14 +7,15 @@
- else
= t(".#{attachment_name}_hint")
.thirteen.columns
= image_tag attachment_url, class: "image-field-group__preview-image" if object.send(attachment_name).present?
- if f.object.send(attachment_name).present? && attachment_url.present?
= image_tag attachment_url, class: "image-field-group__preview-image"
%br
= f.file_field attachment_name, accept: "image/*"
- if object.send(attachment_name).present?
- if f.object.send(attachment_name).present?
%button.button.small.red{ type: "button", "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "remove_#{attachment_name}" }
= t(".#{attachment_name}_remove")
- if object.send(attachment_name).present?
- if f.object.send(attachment_name).present?
- # add to admin footer to avoid nesting forms
- content_for :admin_footer do
= render ModalComponent.new(id: "remove_#{attachment_name}", close_button: false, modal_class: "tiny") do
@@ -22,4 +23,4 @@
= t(".#{attachment_name}_remove_confirm")
%div{ class: "modal-actions justify-space-around" }
%input{ class: "button icon-plus secondary", type: 'button', value: I18n.t('js.admin.modals.cancel'), "data-action": "click->modal#close" }
= button_to I18n.t('js.admin.modals.confirm'), admin_enterprise_path(object), method: :patch, params: { enterprise: { "remove_#{attachment_name}": 1 } }, form: { "data-turbo": true }
= button_to I18n.t('js.admin.modals.confirm'), admin_enterprise_path(f.object), method: :patch, params: { enterprise: { "remove_#{attachment_name}": 1 } }, form: { "data-turbo": true }

View File

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe "admin/shared/_attachment_field.html.haml" do
include FileHelper
let(:enterprise) { create(:distributor_enterprise, white_label_logo: black_logo_file) }
let(:f) { ActionView::Helpers::FormBuilder.new(:enterprise, enterprise, view, {}) }
it "includes a preview of the image if one was already uploaded" do
allow(view).to receive_messages(
attachment_name: :white_label_logo,
attachment_url: enterprise.white_label_logo_url,
f:
)
render
expect(rendered).to include("<img class=\"image-field-group__preview-image\"")
end
it "handles when a corrupt image was uploaded to S3 i.e. the file is present but a URL cannot be
generated" do
allow(view).to receive_messages(
attachment_name: :white_label_logo,
attachment_url: nil,
f:
)
render
expect(rendered).not_to include("<img class=\"image-field-group__preview-image\"")
end
end