@sunney/flareutils
Version:
Small Utilities and little goodies that make developing with Cloudflare easier and faster.
44 lines • 1.17 kB
JavaScript
import { nanoid } from "nanoid";
const inputFormats = [
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"image/svg+xml",
];
async function resizeImage(image, config) {
// Prep/validate config
if (!inputFormats.includes(config.contentType)) {
throw new Error("Invalid content type");
}
const bucket = config.storage.bucket;
const key = config.storage.prefix + nanoid();
// Store image in R2
await bucket.put(key, image, {
httpMetadata: {
contentType: config.contentType,
},
});
// Resize image
const resizedImage = await fetch(`${config.storage.url}/${key}`, {
cf: {
image: config.transformOpts,
},
});
if (!(resizedImage.ok && resizedImage.body)) {
throw {
status: resizedImage.status,
message: resizedImage.statusText,
};
}
await bucket.delete(key);
// Return resized image
if (config.returnType === "buffer") {
return resizedImage.arrayBuffer();
}
else {
return resizedImage.body;
}
}
export { resizeImage };
//# sourceMappingURL=Resizer.js.map