Skip to content

Commit

Permalink
Merge pull request #7938 from dacook/product-image-import
Browse files Browse the repository at this point in the history
Add script to import product images from URL
  • Loading branch information
mkllnk authored Sep 20, 2021
2 parents 58c0d30 + c156d0c commit 02a10ed
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/services/image_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
39 changes: 39 additions & 0 deletions lib/tasks/import_product_images.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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
21 changes: 21 additions & 0 deletions spec/services/image_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: false

require 'spec_helper'

describe ImageImporter do
let(:url) { Rails.root.join("spec/fixtures/files/logo.png").to_s }
let(:product) { create(:product) }

describe "#import" do
it "downloads and attaches to the product" do
expect {
subject.import(url, product)
}.to change {
Spree::Image.count
}.by(1)

expect(product.images.count).to eq 1
expect(product.images.first.attachment.size).to eq 6274
end
end
end

0 comments on commit 02a10ed

Please sign in to comment.