Skip to content

Commit 0fa5ca4

Browse files
committed
cleanup
1 parent 71a5c06 commit 0fa5ca4

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/utils/media.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const extractExtension = (url) => {
3939
*/
4040
async function fetchImage(ctx, imageUrl) {
4141
const { log } = ctx;
42+
log.debug('fetching image: ', imageUrl);
4243
const resp = await fetch(imageUrl);
4344
if (!resp.ok) {
4445
throw errorWithResponse(502, `Failed to fetch image: ${imageUrl} (${resp.status})`);
@@ -50,8 +51,6 @@ async function fetchImage(ctx, imageUrl) {
5051
.map((byte) => byte.toString(16).padStart(2, '0'))
5152
.join('');
5253

53-
log.debug('got hash: ', imageUrl, hash);
54-
5554
return {
5655
data,
5756
sourceUrl: imageUrl,
@@ -134,19 +133,29 @@ export async function extractAndReplaceImages(ctx, product) {
134133
return newUrl;
135134
};
136135

137-
await Promise.all([
138-
processQueue([...product.images ?? []], async (image) => {
139-
const newUrl = await processImage(image.url);
140-
if (newUrl) {
136+
/**
137+
* @type {[img: string, setImage: (value: string) => void][]}
138+
*/
139+
const arr = [
140+
...(product.images ?? []).map((image) => [
141+
image.url,
142+
(newUrl) => {
141143
image.url = newUrl;
142-
}
143-
}),
144-
processQueue([...product.variants ?? []], async (variant) => {
145-
const newUrl = await processImage(variant.image);
146-
if (newUrl) {
144+
},
145+
]),
146+
...(product.variants ?? []).map((variant) => [
147+
variant.image,
148+
(newUrl) => {
147149
variant.image = newUrl;
148-
}
149-
}),
150-
]);
150+
},
151+
]),
152+
];
153+
154+
await processQueue(arr, async ([imageUrl, setImage]) => {
155+
const newUrl = await processImage(imageUrl);
156+
if (newUrl) {
157+
setImage(newUrl);
158+
}
159+
});
151160
return product;
152161
}

0 commit comments

Comments
 (0)