From 546f976f8a9c7d951351c042ce97513af05d56af Mon Sep 17 00:00:00 2001 From: NiallJoeMaher Date: Thu, 17 Oct 2024 21:36:09 +0200 Subject: [PATCH 1/2] Update to allow gif types Also some general lambda cleanup --- cdk/lambdas/avatarResize/index.js | 13 +++++-------- cdk/lambdas/uploadResize/index.js | 31 +++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cdk/lambdas/avatarResize/index.js b/cdk/lambdas/avatarResize/index.js index 48ba2466..f8a4e356 100644 --- a/cdk/lambdas/avatarResize/index.js +++ b/cdk/lambdas/avatarResize/index.js @@ -11,8 +11,6 @@ exports.handler = async (event) => { const bucket = event.Records[0].s3.bucket.name; const key = event.Records[0].s3.object.key; - console.log({ bucket, key }); - try { // Check if the image has already been resized const headParams = { @@ -21,10 +19,10 @@ exports.handler = async (event) => { }; const headResponse = await s3.send(new HeadObjectCommand(headParams)); - const metadata = headResponse.Metadata || {}; + const s3Metadata = headResponse.Metadata || {}; const contentType = headResponse.ContentType; - if (metadata.resized === "true") { + if (s3Metadata.resized === "true") { return { statusCode: 200, body: JSON.stringify({ message: "Image already resized. Skipping." }), @@ -39,14 +37,13 @@ exports.handler = async (event) => { const response = await s3.send(new GetObjectCommand(getParams)); const stream = response.Body; - console.log({ response, stream }); + if (!stream) throw new Error("BodyStream is empty"); const imageBuffer = Buffer.concat(await stream.toArray()); - const sharpImage = sharp(imageBuffer); // Resize the image - const resizedImageBuffer = await sharpImage + const resizedImageBuffer = await sharp(imageBuffer) .resize({ width: 220, height: 220, fit: "cover" }) .toBuffer(); @@ -56,7 +53,7 @@ exports.handler = async (event) => { Bucket: bucket, Key: key, Body: resizedImageBuffer, - Metadata: { ...metadata, resized: "true" }, + Metadata: { ...s3Metadata, resized: "true" }, ContentType: contentType, }), ); diff --git a/cdk/lambdas/uploadResize/index.js b/cdk/lambdas/uploadResize/index.js index ae2387ee..35bde4e7 100644 --- a/cdk/lambdas/uploadResize/index.js +++ b/cdk/lambdas/uploadResize/index.js @@ -61,17 +61,24 @@ exports.handler = async (event) => { const imageRaw = Buffer.concat(await stream.toArray()); + // Determine the image format + const imageMetadata = await sharp(imageRaw).metadata(); + const isGif = imageMetadata.format === "gif"; + // Function to resize an image const resizeImage = async (buffer, size) => { - return sharp(buffer) - .resize({ - width: size.maxWidth, - height: size.maxHeight, - fit: "inside", - withoutEnlargement: false, - }) - .webp({ quality: 80 }) - .toBuffer(); + const resizedImage = sharp(buffer).resize({ + width: size.maxWidth, + height: size.maxHeight, + fit: "inside", + withoutEnlargement: false, + }); + + if (isGif) { + return resizedImage.gif().toBuffer(); + } else { + return resizedImage.webp({ quality: 80 }).toBuffer(); + } }; // Loop through sizes and upload each resized image @@ -79,7 +86,10 @@ exports.handler = async (event) => { const resizedImage = await resizeImage(imageRaw, size); const resizedKey = size.suffix - ? key.replace(/(\.[\w\d_-]+)$/i, `_${size.suffix}$1`) + ? key.replace( + /(\.[\w\d_-]+)$/i, + `_${size.suffix}${isGif ? ".gif" : ".webp"}`, + ) : key; await s3.send( @@ -87,6 +97,7 @@ exports.handler = async (event) => { Bucket: bucket, Key: resizedKey, Body: resizedImage, + ContentType: isGif ? "image/gif" : "image/webp", }), ); } From ad9bb3da81c234cee423f765f9e86d5783c13165 Mon Sep 17 00:00:00 2001 From: Niall Maher Date: Thu, 17 Oct 2024 20:44:53 +0100 Subject: [PATCH 2/2] Update cdk/lambdas/uploadResize/index.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- cdk/lambdas/uploadResize/index.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/cdk/lambdas/uploadResize/index.js b/cdk/lambdas/uploadResize/index.js index 35bde4e7..07ee6362 100644 --- a/cdk/lambdas/uploadResize/index.js +++ b/cdk/lambdas/uploadResize/index.js @@ -67,17 +67,22 @@ exports.handler = async (event) => { // Function to resize an image const resizeImage = async (buffer, size) => { - const resizedImage = sharp(buffer).resize({ - width: size.maxWidth, - height: size.maxHeight, - fit: "inside", - withoutEnlargement: false, - }); - - if (isGif) { - return resizedImage.gif().toBuffer(); - } else { - return resizedImage.webp({ quality: 80 }).toBuffer(); + try { + const resizedImage = sharp(buffer).resize({ + width: size.maxWidth, + height: size.maxHeight, + fit: "inside", + withoutEnlargement: false, + }); + + if (isGif) { + return resizedImage.gif().toBuffer(); + } else { + return resizedImage.webp({ quality: 80 }).toBuffer(); + } + } catch (error) { + console.error(`Error resizing image to ${size.maxWidth}x${size.maxHeight}:`, error); + throw error; } };