app/controllers/spree/api/retailops/catalog_controller.rb
# reconcile spree's images with a passed-in image list.  a passed-in
# image can be satisfied by an existing image of the same name (ROP
# image filenames are hashed to force uniqueness), or a matched
# origin_url (this is an optimization for initial go-live when ROP
# images are copied from spree images, we don't need to copy them
# back).  All other images will be deleted...
reconcile spree's images with a passed-in image list.  a passed-in
image can be satisfied by an existing image of the same name (ROP
image filenames are hashed to force uniqueness), or a matched
origin_url (this is an optimization for initial go-live when ROP
images are copied from spree images, we don't need to copy them
back).  All other images will be deleted
reconcile spree ' s images with a passed - in image list . a passed - in image can be satisfied by an existing image of the same name ( ROP image filenames are hashed to force uniqueness ) or a matched origin_url ( this is an optimization for initial go - live when ROP images are copied from spree images we don ' t need to copy them back ) . All other images will be deleted
def update_images imgcoll, imglist
            stale = {}
            by_url = {}
            by_filename = {}
            imgcoll.each do |i|
              stale[i] = true
              by_url[i.attachment.url(:original)] = i
              by_filename[i.attachment.original_filename] = i
            end
            sequence = 1
            imglist.to_a.each do |i|
              is_new = false
              if imgobj = by_url[i["origin_url"]] || by_filename[i["filename"]]
                stale.delete(imgobj)
              else
                is_new = true
                new_file = Paperclip.io_adapters.for(URI(i["url"]))
                new_file.original_filename = i["filename"]
                imgobj = imgcoll.build(attachment: new_file)
              end
              if is_new || imgobj.alt != i["alt_text"] || imgobj.position != sequence || i["extend"].present?
                imgobj.alt = i["alt_text"]
                imgobj.position = sequence
                apply_extensions imgobj, i["extend"]
                imgobj.save!
              end
              sequence += 1
            end
            stale.each_key do |i|
              i.destroy!
            end
          end
def update_images imgcoll , imglist stale = { } by_url = { } by_filename = { } imgcoll . each do | i | stale [ i ] = true by_url [ i . attachment . url ( :original ) ] = i by_filename [ i . attachment . original_filename ] = i end sequence = 1 imglist . to_a . each do | i | is_new = false if imgobj = by_url [ i [ "origin_url" ] ] || by_filename [ i [ "filename" ] ] stale . delete ( imgobj ) else is_new = true new_file = Paperclip . io_adapters . for ( URI ( i [ "url" ] ) ) new_file . original_filename = i [ "filename" ] imgobj = imgcoll . build ( attachment : new_file ) end if is_new || imgobj . alt != i [ "alt_text" ] || imgobj . position != sequence || i [ "extend" ] . present? imgobj . alt = i [ "alt_text" ] imgobj . position = sequence apply_extensions imgobj , i [ "extend" ] imgobj . save! end sequence += 1 end stale . each_key do | i | i . destroy! end end
reconcile spree's images with a passed-in image list.
reconcile spree ' s images with a passed - in image list .
#p "Existing image: ",i.attachment.url(:original),i.attachment.original_filename,i #p "Reusing image: ",i,imgobj #p "New image: ",i #p "Deleting old image: ",i
imgcoll
<not_specific>
imglist
<not_specific>