Files
openfoodnetwork/app/services/image_importer.rb
Prikesh Savla 631306cfb3 Extended imageImport and ImageBuilder to get the content type of the file for the attacment for avoiding issues for files without extensions.
Updated config/locale/en.yml for the active_storage_validations related error messages
2025-12-09 08:06:29 +05:30

26 lines
747 B
Ruby

# frozen_string_literal: true
require "private_address_check"
require "private_address_check/tcpsocket_ext"
class ImageImporter
def import(url, product)
valid_url = URI.parse(url)
filename = File.basename(valid_url.path)
metadata = { custom: { origin: url } }
image = Spree::Image.create do |img|
PrivateAddressCheck.only_public_connections do
io = valid_url.open
content_type = Marcel::MimeType.for(io)
img.attachment.attach(io:, filename:, metadata:, content_type:)
end
end
product.image = image if image
rescue StandardError
# Any URL parsing or network error shouldn't impact the product import
# at all. Maybe we'll add UX for error handling later.
nil
end
end