|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import processQueue from '@adobe/helix-shared-process-queue'; |
| 14 | + |
| 15 | +/** |
| 16 | + * @typedef {Object} ImageData |
| 17 | + * @property {string} sourceUrl |
| 18 | + * @property {ArrayBuffer} data |
| 19 | + * @property {string} hash |
| 20 | + * @property {string} mimeType |
| 21 | + * @property {number} length |
| 22 | + * @property {string} [extension] |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {string} url |
| 27 | + * @returns {string|undefined} |
| 28 | + */ |
| 29 | +const extractExtension = (url) => { |
| 30 | + const match = url.match(/\.([^.]+)$/); |
| 31 | + return match ? match[1] : undefined; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * @param {Context} ctx |
| 36 | + * @param {string} imageUrl |
| 37 | + * @returns {Promise<ImageData | null>} |
| 38 | + */ |
| 39 | +async function fetchImage(ctx, imageUrl) { |
| 40 | + const resp = await fetch(imageUrl); |
| 41 | + if (!resp.ok) { |
| 42 | + throw new Error(`Failed to fetch image: ${resp.statusText}`); |
| 43 | + } |
| 44 | + |
| 45 | + const data = await resp.arrayBuffer(); |
| 46 | + const arr = await crypto.subtle.digest('SHA-1', data); |
| 47 | + const hash = Array.from(new Uint8Array(arr)) |
| 48 | + .map((byte) => byte.toString(16).padStart(2, '0')) |
| 49 | + .join(''); |
| 50 | + |
| 51 | + return { |
| 52 | + data, |
| 53 | + sourceUrl: imageUrl, |
| 54 | + hash, |
| 55 | + mimeType: resp.headers.get('content-type'), |
| 56 | + length: data.byteLength, |
| 57 | + extension: extractExtension(imageUrl), |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * |
| 63 | + * @param {Context} ctx |
| 64 | + * @param {ImageData} image |
| 65 | + * @returns {Promise<string>} new url |
| 66 | + */ |
| 67 | +async function uploadImage(ctx, image) { |
| 68 | + const { |
| 69 | + env, |
| 70 | + config: { org, site }, |
| 71 | + } = ctx; |
| 72 | + const { |
| 73 | + data, |
| 74 | + hash, |
| 75 | + mimeType, |
| 76 | + extension, |
| 77 | + sourceUrl, |
| 78 | + } = image; |
| 79 | + |
| 80 | + const filename = `media_${hash}${extension ? `.${extension}` : ''}`; |
| 81 | + const key = `${org}/${site}/media/${filename}`; |
| 82 | + const resp = await env.CATALOG_BUCKET.head(key); |
| 83 | + if (resp) { |
| 84 | + return `./${filename}`; |
| 85 | + } |
| 86 | + |
| 87 | + await env.CATALOG_BUCKET.put(key, data, { |
| 88 | + httpMetadata: { |
| 89 | + contentType: mimeType, |
| 90 | + }, |
| 91 | + customMetadata: { |
| 92 | + sourceLocation: sourceUrl, |
| 93 | + }, |
| 94 | + }); |
| 95 | + return `./${filename}`; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @param {Context} ctx |
| 100 | + * @param {ProductBusEntry} product |
| 101 | + * @returns {Promise<ProductBusEntry>} |
| 102 | + */ |
| 103 | +export async function extractAndReplaceImages(ctx, product) { |
| 104 | + /** @type {Map<string, Promise<string>>} */ |
| 105 | + const processed = new Map(); |
| 106 | + |
| 107 | + /** |
| 108 | + * @param {string} url |
| 109 | + * @returns {Promise<string|undefined>} new url |
| 110 | + */ |
| 111 | + const processImage = async (url) => { |
| 112 | + if (processed.has(url)) { |
| 113 | + return processed.get(url); |
| 114 | + } |
| 115 | + |
| 116 | + /** @type {(value: string) => void} */ |
| 117 | + let resolve; |
| 118 | + const promise = new Promise((r) => { |
| 119 | + resolve = r; |
| 120 | + }); |
| 121 | + processed.set(url, promise); |
| 122 | + |
| 123 | + const img = await fetchImage(ctx, url); |
| 124 | + const newUrl = await uploadImage(ctx, img); |
| 125 | + resolve(newUrl); |
| 126 | + return newUrl; |
| 127 | + }; |
| 128 | + |
| 129 | + await Promise.all([ |
| 130 | + processQueue([...product.images], async (image) => { |
| 131 | + const newUrl = await processImage(image.url); |
| 132 | + if (newUrl) { |
| 133 | + image.url = newUrl; |
| 134 | + } |
| 135 | + }), |
| 136 | + processQueue([...product.variants], async (variant) => { |
| 137 | + const newUrl = await processImage(variant.image); |
| 138 | + if (newUrl) { |
| 139 | + variant.image = newUrl; |
| 140 | + } |
| 141 | + }), |
| 142 | + ]); |
| 143 | + return product; |
| 144 | +} |
0 commit comments