If I am uploading an image I want multer s3 to keep an original, in addition to create a thumbnail.
In other words. I only want to change the filename in the first transform.
The code below works ish, but the problem is that sharp() modifies the original. Let's say I upload a gif file, the animation will stop working.
I've tried to remove the transform arrow function from the original transform object, but that crashes the code with error dest.on is not a function.
I have tried remove the whole transform object, but since shouldTransform is truthy, it will not upload an original at all.
I have tried to replace sharp()
with null
, but that gives me this error Cannot read property 'on' of null.
According to docs I should be able to just input a string in the sharp() position, but that also crashes the code.
Example: cb(null, "something.jpeg");
Error: dest.on is not a function
The documentation on multer-s3-transform is lacking a bit, so I'm just bruteforcing at this point.
const awsStorage = multerS3({
s3,
bucket: process.env.AWS_BUCKET_NAME,
shouldTransform: (transformReq, file, cb) => {
cb(null, /^image/i.test(file.mimetype));
},
key: (transformReq, file, cb) => {
const splitFileName = file.originalname.split('.');
const fileEnding = splitFileName[splitFileName.length - 1];
cb(null, `files/${dateNow}.${fileEnding}`);
},
transforms: [
{
id: 'original',
key: (transformReq, file, cb) => {
const splitFileName = file.originalname.split('.');
const fileEnding = splitFileName[splitFileName.length - 1];
cb(null, `files/${dateNow}.${fileEnding}`);
},
transform: (transformReq, file, cb) => {
cb(null, sharp()); // This is where I'm struggling
},
},
{
id: 'thumbnail',
key: (transformReq, file, cb) => {
const splitFileName = file.originalname.split('.');
const fileEnding = splitFileName[splitFileName.length - 1];
cb(null, `thumbnails/${dateNow}.${fileEnding}`);
},
transform: (transformReq, file, cb) => {
cb(
null,
sharp()
.resize(200, 200)
.png()
);
},
},
],
});