expressjs / multer

Node.js middleware for handling `multipart/form-data`.
MIT License
11.54k stars 1.05k forks source link

S3 limiting file size based on fieldName on upload.fields #833

Open ghost opened 4 years ago

ghost commented 4 years ago

I want to be able to filter files based on different file extensions. One idea would be to pass a middleware after the upload which handles it. Like this:

app.post("/upload", S3.upload.fields([{
    name: 'video',
    maxCount: 1,
}, {
    name: 'subtitles',
    maxCount: 1,
}]), validateUploadForms, validateUploadSizes, async function(req, res, next) {

    console.log("Success");

});

function validateUploadSizes(req, res, next) {

    if (req.files.video[0].size > 2000) {
        return next();
    } else {

        res.json({
            response: Strings.ERROR
        });

        return next(new Error);

    }

}

But the problem here is that the file is already finished uploading so I will have to waste extra space and time on removing it.

Another idea would be to do something like this:

app.post("/upload", S3.upload.single("video"), S3.upload.single("audio"), validateUploadForms, async function(req, res, next) {

    console.log("Success");

});

And then use the fieldSize/fileSize property. But I am unsure on how it would work like. The problem here is that I can't use multiple upload.single middlewares.

What can I do? I am using S3/MulterS3

jonchurch commented 4 years ago

Does https://github.com/expressjs/multer/issues/569 sound like what you're trying to do? This is possible in the beta version of multer 2, but that doesn't use storage engines so you'd have to implement the upload to S3 yourself.

I think that filtering by size per field isn't currently supported. Related https://github.com/expressjs/multer/issues/314

ghost commented 4 years ago

Does #569 sound like what you're trying to do? This is possible in the beta version of multer 2, but that doesn't use storage engines so you'd have to implement the upload to S3 yourself.

I think that filtering by size per field isn't currently supported. Related #314

No, it doesn't. I specified the issue right below the first code.