mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
26 lines
747 B
Ruby
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
|