diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 7a3964a7a7..db664b0006 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -56,8 +56,13 @@ module Spree def no_attachment_errors return if attachment.errors.empty? - errors.add :attachment, - "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." + if errors.all? {|e| e.type == "Paperclip::Errors::NotIdentifiedByImageMagickError"} + attachment.errors.clear + errors.add :base, I18n.t('spree.admin.products.image_upload_error') + else + errors.add :attachment, + I18n.t('spree.admin.products.paperclip_image_error', attachment_file_name: attachment_file_name) + end false end diff --git a/app/views/spree/admin/images/new.html.haml b/app/views/spree/admin/images/new.html.haml index 651bddf3e1..1b120d3f22 100644 --- a/app/views/spree/admin/images/new.html.haml +++ b/app/views/spree/admin/images/new.html.haml @@ -1,9 +1,11 @@ += render partial: 'spree/shared/error_messages', locals: { target: @image } + = form_for [:admin, @product, @image], url: admin_product_images_path(@product, @image, @url_filters), html: { multipart: true } do |f| %fieldset %legend{ align: "center" }= t('spree.new_image') = render partial: 'form', locals: { f: f } .form-buttons.filter-actions.actions - = button t('spree.actions.update'), 'icon-refresh' + = button t('actions.create'), 'icon-ok' %span.or= t('spree.or') = link_to_with_icon 'icon-remove', t('spree.actions.cancel'), admin_product_images_url(@product, @url_filters), id: 'cancel_link', class: 'button' diff --git a/config/locales/en.yml b/config/locales/en.yml index c915843a7f..f68aaf3ee2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3726,6 +3726,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using no_payment_via_admin_backend: Paypal payments cannot be captured in the Backoffice products: image_upload_error: "The product image was not recognised. Please upload an image in PNG or JPG format." + paperclip_image_error: "Paperclip returned errors for file '%{attachment_file_name}' - check ImageMagick installation or image source file." new: title: "New Product" new_product: "New Product" diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index aacc0d3b0f..83ec8afe8c 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -484,7 +484,7 @@ describe ' page.find('a#new_image_link').click attach_file('image_attachment', file_path) - click_button "Update" + click_button "Create" uri = URI.parse(current_url) expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) @@ -540,6 +540,23 @@ describe ' expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) end + it "checks error when creating product image with unsupported format", js: true do + unsupported_image_file_path = Rails.root + "README.md" + product = create(:simple_product, supplier: @supplier2) + + image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__)) + Spree::Image.create(viewable_id: product.master.id, viewable_type: 'Spree::Variant', + alt: "position 1", attachment: image, position: 1) + + visit spree.admin_product_images_path(product) + page.find('a#new_image_link').click + attach_file('image_attachment', unsupported_image_file_path) + click_button "Create" + + expect(page).to have_text "The product image was not recognised." + expect(page).to have_text "Please upload an image in PNG or JPG format." + end + it "deleting product images", js: true do product = create(:simple_product, supplier: @supplier2) image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__))