-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lambda to allow gif types #1143
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -61,32 +61,48 @@ 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({ | ||||||||||||||
try { | ||||||||||||||
const resizedImage = sharp(buffer).resize({ | ||||||||||||||
width: size.maxWidth, | ||||||||||||||
height: size.maxHeight, | ||||||||||||||
fit: "inside", | ||||||||||||||
withoutEnlargement: false, | ||||||||||||||
}) | ||||||||||||||
.webp({ quality: 80 }) | ||||||||||||||
.toBuffer(); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
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; | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
// Loop through sizes and upload each resized image | ||||||||||||||
for (const size of sizes) { | ||||||||||||||
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( | ||||||||||||||
new PutObjectCommand({ | ||||||||||||||
Bucket: bucket, | ||||||||||||||
Key: resizedKey, | ||||||||||||||
Body: resizedImage, | ||||||||||||||
ContentType: isGif ? "image/gif" : "image/webp", | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Set appropriate Content-Encoding header for WebP images. When serving WebP images, it's beneficial to include the Modify the await s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: resizedKey,
Body: resizedImage,
ContentType: isGif ? "image/gif" : "image/webp",
+ ContentEncoding: isGif ? undefined : "webp",
}),
); This addition helps improve compatibility and performance with clients that support WebP. 📝 Committable suggestion
Suggested change
🛠️ Refactor suggestion Set the correct Setting appropriate cache headers can improve the performance of your application by enabling clients and intermediaries to cache the resized images. This reduces load on your server and decreases image load times for users. Add the await s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: resizedKey,
Body: resizedImage,
ContentType: isGif ? "image/gif" : "image/webp",
+ CacheControl: "max-age=31536000, public",
}),
); This sets the cache to expire after one year ( 📝 Committable suggestion
Suggested change
|
||||||||||||||
}), | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use the Node.js
path
module for reliable file extension handling.The regular expression used to replace the file extension may not handle all possible file names correctly, such as those without an extension or with multiple dots in their names. Using the built-in
path
module provides a more robust solution for handling file paths and extensions.Modify your code to use the
path
module:This change ensures that the file extension is accurately identified and replaced, regardless of the file name format.