5

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()
                );
            },
        },
    ],
});
Jørgen Vik
  • 726
  • 1
  • 8
  • 27

1 Answers1

2

Can you remove the transform block from the first transforms object (original)?

transform: (transformReq, file, cb) => {
   cb(null, sharp()); // This is where I'm struggling
}

If you're just trying to rename the file then the key portion should be sufficient. You are properly using sharp on the thumbnail below.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
  • This could possibly solve the problem, but I cannot confirm as I ended up building my own file uploader to gain better control. – Jørgen Vik Aug 06 '20 at 13:21