async function createAllVariationsForR2Image({
originalId,
originalUrl,
imgProxyKey,
imgProxySalt,
imgProxyUrl,
}: CreateAllVariationsForR2ImageOptions) {
const source = stringToBase64Url(originalUrl);
const limit = plimit(3);
const promises: Promise<{
response: Response;
width: number;
key: string;
}>[] = [];
const lastDotIndex = originalId.lastIndexOf(".");
const baseName = originalId.slice(0, lastDotIndex);
const extension = originalId.slice(lastDotIndex + 1);
for (const variation of variations) {
for (const dpr of [1, 2]) {
const path = `/rs:fit:${variation.width}:0/dpr:${dpr}/${source}`;
const signature = await sign(imgProxySalt, path, imgProxyKey);
const resizeUrl = `${imgProxyUrl}/${signature}${path}`;
console.log("resizeUrl", resizeUrl);
promises.push(
limit(async () => {
const response = await fetch(resizeUrl);
return {
response,
width: variation.width,
key: `${baseName}_${variation.id}_${dpr}x.${extension}`,
};
})
);
}
}
const resolvedPromises = await Promise.all(promises);
return resolvedPromises;
}
async function createAllVariationsForR2Image({
originalId,
originalUrl,
imgProxyKey,
imgProxySalt,
imgProxyUrl,
}: CreateAllVariationsForR2ImageOptions) {
const source = stringToBase64Url(originalUrl);
const limit = plimit(3);
const promises: Promise<{
response: Response;
width: number;
key: string;
}>[] = [];
const lastDotIndex = originalId.lastIndexOf(".");
const baseName = originalId.slice(0, lastDotIndex);
const extension = originalId.slice(lastDotIndex + 1);
for (const variation of variations) {
for (const dpr of [1, 2]) {
const path = `/rs:fit:${variation.width}:0/dpr:${dpr}/${source}`;
const signature = await sign(imgProxySalt, path, imgProxyKey);
const resizeUrl = `${imgProxyUrl}/${signature}${path}`;
console.log("resizeUrl", resizeUrl);
promises.push(
limit(async () => {
const response = await fetch(resizeUrl);
return {
response,
width: variation.width,
key: `${baseName}_${variation.id}_${dpr}x.${extension}`,
};
})
);
}
}
const resolvedPromises = await Promise.all(promises);
return resolvedPromises;
}