Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

SD-1481 Change Image content type validation to use gem (#11419)

Kacper Mękarski81a43a54b6ad
diff --git a/core/app/models/spree/image/configuration/active_storage.rb b/core/app/models/spree/image/configuration/active_storage.rb
index 2e986b1e96..268771e6eb 100644
--- a/core/app/models/spree/image/configuration/active_storage.rb
+++ b/core/app/models/spree/image/configuration/active_storage.rb
@@ -5,11 +5,10 @@ module Spree
extend ActiveSupport::Concern
included do
- validate :check_attachment_presence
- validate :check_attachment_content_type
-
has_one_attached :attachment
+ validates :attachment, attached: true, content_type: /\Aimage\/.*\z/
+
default_scope { includes(attachment_attachment: :blob) }
def self.styles
@@ -32,22 +31,6 @@ module Spree
def default_style
:product
end
-
- def accepted_image_types
- %w(image/jpeg image/jpg image/png image/gif)
- end
-
- def check_attachment_presence
- unless attachment.attached?
- errors.add(:attachment, :attachment_must_be_present)
- end
- end
-
- def check_attachment_content_type
- if attachment.attached? && !attachment.content_type.in?(accepted_image_types)
- errors.add(:attachment, :not_allowed_content_type)
- end
- end
end
end
end
diff --git a/core/app/models/spree/taxon_image/configuration/active_storage.rb b/core/app/models/spree/taxon_image/configuration/active_storage.rb
index 2796fbebdb..bc8a05b0b3 100644
--- a/core/app/models/spree/taxon_image/configuration/active_storage.rb
+++ b/core/app/models/spree/taxon_image/configuration/active_storage.rb
@@ -5,10 +5,10 @@ module Spree
extend ActiveSupport::Concern
included do
- validate :check_attachment_content_type
-
has_one_attached :attachment
+ validates :attachment, content_type: /\Aimage\/.*\z/
+
default_scope { includes(attachment_attachment: :blob) }
def self.styles
@@ -21,16 +21,6 @@ module Spree
def default_style
:mini
end
-
- def accepted_image_types
- %w(image/jpeg image/jpg image/png image/gif)
- end
-
- def check_attachment_content_type
- if attachment.attached? && !attachment.content_type.in?(accepted_image_types)
- errors.add(:attachment, :not_allowed_content_type)
- end
- end
end
end
end