Files
openfoodnetwork/lib/tasks/import_product_images.rake
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

40 lines
1.4 KiB
Ruby

# frozen_string_literal: true
namespace :ofn do
namespace :import do
desc "Importing images for products from CSV"
task :product_images, [:filename] => [:environment] do |_task, args|
COLUMNS = [:producer, :name, :image_url]
puts "Warning: use only with trusted URLs. This script will download whatever it can, including local secrets, and expose the file as an image file."
raise "Filename required" if args[:filename].blank?
csv = CSV.read(args[:filename], headers: true, header_converters: :symbol)
raise "CSV columns reqired: #{COLUMNS.map(&:to_s)}" if (COLUMNS - csv.headers).present?
csv.each.with_index do |entry, index|
puts "#{index} #{entry[:producer]}, #{entry[:name]}"
enterprise = Enterprise.find_by_name! entry[:producer]
product = Spree::Product.where(supplier: enterprise,
name: entry[:name],
deleted_at: nil).first
if product.nil?
puts " product not found."
next
end
if product.images.first.nil?
ImageImporter.new.import(entry[:image_url], product)
puts " image added."
else
# image = product.images.first
# image.update(attachment: entry[:image_url])
puts " image exists, not updated."
end
end
end
end
end