From c3d274c84f4e5205eddaadb0b0d52caacdfcc3e7 Mon Sep 17 00:00:00 2001 From: saunmanoj888 Date: Tue, 27 Dec 2022 21:16:06 +0530 Subject: [PATCH] Fix corrupt and invalid image upload issue --- app/models/enterprise.rb | 8 ++++++-- app/models/enterprise_group.rb | 8 ++++++-- app/models/spree/image.rb | 6 ++++-- config/locales/en.yml | 1 + spec/system/admin/products_spec.rb | 2 +- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 7654348123..2716bde87c 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -85,8 +85,12 @@ class Enterprise < ApplicationRecord has_one_attached :promo_image has_one_attached :terms_and_conditions - validates :logo, content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} - validates :promo_image, content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + validates :logo, + processable_image: true, + content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + validates :promo_image, + processable_image: true, + content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} validates :terms_and_conditions, content_type: { in: "application/pdf", message: I18n.t(:enterprise_terms_and_conditions_type_error), diff --git a/app/models/enterprise_group.rb b/app/models/enterprise_group.rb index a791405965..9616c24722 100644 --- a/app/models/enterprise_group.rb +++ b/app/models/enterprise_group.rb @@ -28,8 +28,12 @@ class EnterpriseGroup < ApplicationRecord has_one_attached :logo has_one_attached :promo_image - validates :logo, content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} - validates :promo_image, content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + validates :logo, + processable_image: true, + content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + validates :promo_image, + processable_image: true, + content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} scope :by_position, -> { order('position ASC') } scope :on_front_page, -> { where(on_front_page: true) } diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 75eb56af69..e328eea790 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -11,8 +11,10 @@ module Spree has_one_attached :attachment - validates :attachment, attached: true, - content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} + validates :attachment, + attached: true, + processable_image: true, + content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z} validate :no_attachment_errors def variant(name) diff --git a/config/locales/en.yml b/config/locales/en.yml index 60e29711e7..e0aab6e77e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -125,6 +125,7 @@ en: aspect_ratio_not_landscape: "must be a landscape image" aspect_ratio_is_not: "must have an aspect ratio of %{aspect_ratio}" aspect_ratio_unknown: "has an unknown aspect ratio" + image_not_processable: "is not a valid image" stripe: error_code: diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index ebaf2723c8..28abf72e1c 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -618,7 +618,7 @@ describe ' click_button "Create" expect(page).to have_text "Attachment has an invalid content type" - expect(page).to have_text "Please upload the image in JPG, PNG, GIF, SVG or WEBP format." + expect(page).to have_text "Attachment is not a valid image" end it "deleting product images" do