mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
25 lines
443 B
Ruby
25 lines
443 B
Ruby
# frozen_string_literal: true
|
|
|
|
class ImageImporter
|
|
def import(url, product)
|
|
attach(download(url), product)
|
|
end
|
|
|
|
private
|
|
|
|
def download(url)
|
|
local_file = Tempfile.new
|
|
remote_file = open(url)
|
|
IO.copy_stream(remote_file, local_file)
|
|
local_file
|
|
end
|
|
|
|
def attach(file, product)
|
|
Spree::Image.create(
|
|
attachment: file,
|
|
viewable_id: product.master.id,
|
|
viewable_type: Spree::Variant,
|
|
)
|
|
end
|
|
end
|