Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Record the identities a DAM knows an image by

Damian Legawiec668d2caa8769
diff --git a/packages/admin-sdk/src/types/generated/Media.ts b/packages/admin-sdk/src/types/generated/Media.ts
index 71c53aadf6..5aa1adeb4b 100644
--- a/packages/admin-sdk/src/types/generated/Media.ts
+++ b/packages/admin-sdk/src/types/generated/Media.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 7107aa1342fc093c2fd2635e8c9fa5e6
+// Typelizer digest 2acbc395a80e34d49dcaa483bb8837c1
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -12,6 +12,7 @@ interface Media {
focal_point_x: number | null;
focal_point_y: number | null;
external_video_url: string | null;
+ external_media_url: string | null;
video_provider: string | null;
video_embed_url: string | null;
video_url: string | null;
@@ -23,6 +24,7 @@ interface Media {
large_url: string | null;
xlarge_url: string | null;
og_image_url: string | null;
+ external_references: Record<string, string>;
created_at: string;
updated_at: string;
attached: boolean;
diff --git a/packages/sdk/src/types/generated/Media.ts b/packages/sdk/src/types/generated/Media.ts
index 016cb0e36f..0c3e10aef7 100644
--- a/packages/sdk/src/types/generated/Media.ts
+++ b/packages/sdk/src/types/generated/Media.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 982fb1c77e99644481ed30fe4261bc1b
+// Typelizer digest 752fae21556c460a4fdb23e8e5a36f01
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -12,6 +12,7 @@ interface Media {
focal_point_x: number | null;
focal_point_y: number | null;
external_video_url: string | null;
+ external_media_url: string | null;
video_provider: string | null;
video_embed_url: string | null;
video_url: string | null;
diff --git a/packages/sdk/src/zod/generated/Media.ts b/packages/sdk/src/zod/generated/Media.ts
index e84487846e..0a924713a1 100644
--- a/packages/sdk/src/zod/generated/Media.ts
+++ b/packages/sdk/src/zod/generated/Media.ts
@@ -11,6 +11,7 @@ export const MediaSchema = z.object({
focal_point_x: z.number().nullable(),
focal_point_y: z.number().nullable(),
external_video_url: z.string().nullable(),
+ external_media_url: z.string().nullable(),
video_provider: z.string().nullable(),
video_embed_url: z.string().nullable(),
video_url: z.string().nullable(),
diff --git a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
index ba22bc1939..0201cd2266 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
@@ -3,6 +3,7 @@ module Spree
module V3
module Admin
class MediaController < ResourceController
+ include Spree::Api::V3::Admin::Concerns::ExternalReferences
scoped_resource :products
def create
@@ -103,11 +104,17 @@ module Spree
url = permitted_params[:url]
position = permitted_params[:position]
+ # The job runs later, so the identities the connector sent ride
+ # along and are written on the media once it exists. All of them:
+ # a DAM and a PIM may both name the same asset.
+ references = external_reference_params.map { |entry| entry.transform_keys(&:to_s) }
+ references = references.presence
+
Spree::Images::SaveFromUrlJob.perform_later(
@parent.id,
@parent.class.name,
url,
- nil,
+ references,
position
)
diff --git a/spree/api/app/serializers/spree/api/v3/admin/media_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/media_serializer.rb
index 7e2a9535b8..8d2d26378d 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/media_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/media_serializer.rb
@@ -3,6 +3,8 @@ module Spree
module V3
module Admin
class MediaSerializer < V3::MediaSerializer
+ include Concerns::ExternalReferencesAttribute
+
# Both are null for a library file that has not been placed yet.
typelize viewable_type: [:string, nullable: true],
viewable_id: [:string, nullable: true],
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
index 92ee09888f..9b8b231298 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
@@ -60,6 +60,31 @@ RSpec.describe Spree::Api::V3::Admin::MediaController, type: :controller do
expect(response).to have_http_status(:accepted)
end
+
+ it 'passes the identity a DAM sent along to the fetch job' do
+ expect {
+ post :create, params: {
+ product_id: product.prefixed_id,
+ url: 'https://example.com/image.jpg',
+ external_references: { dam: 'ASSET-9' }
+ }, as: :json
+ }.to have_enqueued_job(Spree::Images::SaveFromUrlJob).with(
+ product.id, 'Spree::Product', 'https://example.com/image.jpg',
+ [{ 'system' => 'dam', 'external_id' => 'ASSET-9' }], nil
+ )
+ end
+
+ it 'records the identity on media created directly' do
+ post :create, params: {
+ product_id: product.prefixed_id,
+ media_type: 'external_video',
+ external_video_url: 'https://vimeo.com/123456789',
+ external_references: { dam: 'ASSET-1' }
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['external_references']).to eq('dam' => 'ASSET-1')
+ end
end
describe 'PATCH #update' do
diff --git a/spree/core/app/jobs/spree/images/save_from_url_job.rb b/spree/core/app/jobs/spree/images/save_from_url_job.rb
index 3ea86b4797..ea81c4a05d 100644
--- a/spree/core/app/jobs/spree/images/save_from_url_job.rb
+++ b/spree/core/app/jobs/spree/images/save_from_url_job.rb
@@ -11,15 +11,29 @@ module Spree
discard_on URI::InvalidURIError
discard_on SsrfFilter::Error
+ # The system a bare external id is filed under when the caller names
+ # none. A DAM or PIM feeding media through this job should pass
+ # `{ system:, external_id: }` and name itself.
+ DEFAULT_EXTERNAL_SYSTEM = 'import'.freeze
+
+ # @param external_id [String, Hash, Array, nil] the identities the source
+ # systems know this media by — a bare id, a `{ system:, external_id: }`
+ # hash, or a list of them when several systems name the same asset.
+ # Recorded as Spree::ExternalReferences on the media, and used to find
+ # the row again on the next run so a feed never re-imports.
+ # @param position [Integer, nil] the media's position in the gallery
+ # @param link_variant_id [Integer, nil] links the product-level media to
+ # this variant once saved
+ # @return [void]
def perform(viewable_id, viewable_type, external_url, external_id = nil, position = nil, link_variant_id = nil)
viewable = viewable_type.safe_constantize.find(viewable_id)
Spree::Media.ensure_custom_field_definition_exists!(Spree::Media::EXTERNAL_URL_CUSTOM_FIELD_KEY)
external_url = external_url.strip
- external_id = external_id.to_s.downcase.strip if external_id.present?
+ references = external_references_from(external_id)
- image = find_or_initialize_image(viewable, external_url, external_id)
+ image = find_or_initialize_image(viewable, external_url, references)
image.set_default_values_for_import if image.new_record? && image.respond_to?(:set_default_values_for_import)
@@ -32,11 +46,13 @@ module Spree
# still trigger save! if position has changed
if image_already_saved?(image, external_url)
image.save!
+ record_external_references(image, references)
link_to_variant(image, link_variant_id)
return
end
- download_and_attach_image(external_url, image, external_id)
+ download_and_attach_image(external_url, image)
+ record_external_references(image, references)
link_to_variant(image, link_variant_id)
rescue ActiveStorage::IntegrityError => e
raise e unless Rails.env.test?
@@ -44,7 +60,7 @@ module Spree
private
- def download_and_attach_image(external_url, image, external_id)
+ def download_and_attach_image(external_url, image)
max_size = Spree::Config.max_image_download_size
response = SsrfFilter.get(
@@ -78,7 +94,6 @@ module Spree
image.attachment.attach(io: tempfile, filename: filename)
image.external_url = external_url
- image.external_id = external_id if external_id.present? && image.respond_to?(:external_id)
image.save!
ensure
tempfile.close
@@ -101,12 +116,40 @@ module Spree
scope.respond_to?(:with_deleted) ? scope.with_deleted : scope
end
- def find_or_initialize_image(viewable, external_url, external_id = nil)
- if external_id.present? && viewable.respond_to?(:external_id)
- image_scope(viewable).find_or_initialize_by(external_id: external_id)
- else
- image_scope(viewable).with_external_url(external_url).first || viewable_assets(viewable).new
+ # An identity from the source system wins over the URL: a DAM may move
+ # the file and keep the id, and matching on the URL would then import a
+ # second copy.
+ def find_or_initialize_image(viewable, external_url, references = [])
+ Array(references).each do |reference|
+ existing = image_scope(viewable).find_by_external_id(reference[:system], reference[:external_id])
+ return existing if existing.present?
end
+
+ image_scope(viewable).with_external_url(external_url).first || viewable_assets(viewable).new
+ end
+
+ # Normalises the caller's external ids — a bare string, a
+ # `{ system:, external_id: }` hash, or a list of either — into one shape.
+ def external_references_from(external_id)
+ return [] if external_id.blank?
+
+ entries = external_id.is_a?(Array) ? external_id : [external_id]
+ entries.filter_map do |entry|
+ if entry.is_a?(Hash)
+ hash = entry.to_h.symbolize_keys
+ next if hash[:external_id].blank?
+
+ { system: hash[:system].presence || DEFAULT_EXTERNAL_SYSTEM, external_id: hash[:external_id].to_s.strip }
+ elsif entry.present?
+ { system: DEFAULT_EXTERNAL_SYSTEM, external_id: entry.to_s.strip }
+ end
+ end
+ end
+
+ def record_external_references(image, references)
+ return unless image.persisted?
+
+ Array(references).each { |reference| image.set_external_id(reference[:system], reference[:external_id]) }
end
def link_to_variant(image, variant_id)
diff --git a/spree/core/app/models/spree/media.rb b/spree/core/app/models/spree/media.rb
index 1800bea3cf..ff4f490fa6 100644
--- a/spree/core/app/models/spree/media.rb
+++ b/spree/core/app/models/spree/media.rb
@@ -13,6 +13,7 @@ module Spree
# nothing to derive tenancy from. The column stays nullable for rows the
# upgrade could not reach; new rows always carry a store.
include Spree::SingleStoreResource
+ include Spree::HasExternalReferences
# SingleStoreResource refuses any store change on a persisted record. Media
# predates its store column, so a legacy row carries nil until the upgrade
diff --git a/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb b/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb
index 9007bde0ba..8bb4949119 100644
--- a/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb
+++ b/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb
@@ -62,6 +62,45 @@ RSpec.describe Spree::Images::SaveFromUrlJob, type: :job do
end
end
+ context 'when the source system names the media' do
+ let(:external_id) { { 'system' => 'dam', 'external_id' => 'ASSET-9' } }
+
+ it 'records that identity on the media' do
+ subject
+
+ expect(image.external_id_for('dam')).to eq('ASSET-9')
+ end
+
+ # A DAM may move the file and keep the id; matching on the URL would
+ # then import a second copy of the same asset.
+ it 'finds the media again by that identity even when the URL changed' do
+ subject
+ first = image
+
+ expect(SsrfFilter).to receive(:get).once.and_return(response)
+ described_class.new(viewable_id, viewable_type, 'https://cdn.example.com/moved.jpg', external_id, position).perform_now
+
+ expect(variant.images.reload.count).to eq(1)
+ expect(first.reload.external_url).to eq('https://cdn.example.com/moved.jpg')
+ end
+
+ it 'records every system that names the same asset' do
+ described_class.new(viewable_id, viewable_type, external_url,
+ [{ 'system' => 'dam', 'external_id' => 'ASSET-9' },
+ { 'system' => 'pim', 'external_id' => 'SKU-2' }], position).perform_now
+ media = variant.images.last
+
+ expect(media.external_id_for('dam')).to eq('ASSET-9')
+ expect(media.external_id_for('pim')).to eq('SKU-2')
+ end
+
+ it 'files a bare id under the default system' do
+ described_class.new(viewable_id, viewable_type, external_url, 'ASSET-1', position).perform_now
+
+ expect(variant.images.last.external_id_for(described_class::DEFAULT_EXTERNAL_SYSTEM)).to eq('ASSET-1')
+ end
+ end
+
context 'when skip_import? returns true' do
before do
allow(image).to receive(:skip_import?).and_return(true)