Files
openfoodnetwork/app/services/image_importer.rb
David Cook c156d0c2e6 Add script to import product images from URL
With new ImageImporter.

Co-authored-by: Maikel <maikel@email.org.au>
2021-07-16 11:36:49 +10:00

23 lines
412 B
Ruby

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