Skip to content
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

Using FileInterceptor with multerOptions.fileFilter not working properly #437

Closed
chanlito opened this issue Feb 19, 2018 · 4 comments
Closed

Comments

@chanlito
Copy link

chanlito commented Feb 19, 2018

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

{
  ...
  @Post('video')
  @UseInterceptors(
    FileInterceptor('video', {
      limits: {
        files: 1,
        fileSize: 5 * 10 * 10 * 10 * 10 * 10 * 10 * 10 // 50 mb in bytes
      },
      storage: diskStorage({
        destination: (req, file, cb) => cb(null, resolve('.', 'public', 'uploads')),
        filename: (req, file, cb) => cb(null, `${v4().replace(/-/g, '')}.${extension(file.mimetype)}`)
      }),
      fileFilter: (req: Request, file, cb) => {
        const ext = extension(file.mimetype);
        if (ext !== 'mp4') {
          return cb(new Error('Extension not allowed'), false); // FileIntercepter is completely ignoring this.
        }
        return cb(null, true);
      }
    })
  )
  async uploadVideo(@UploadedFile() video: Express.Multer.File) {
    console.log('video', video);
    return { message: 'OK' };
  }
  ...
}

Expected behavior

Expect only .mp4 files to uploaded, and other types to throw Error.

Right now, Error is not thrown.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: 4.6.x

 
For Tooling issues:
- Node version: 8.9.4  
- Platform: Mac  

Others:

@chanlito chanlito changed the title Using FileInterceptor with multerOptions.fileFilter not working. Using FileInterceptor with multerOptions.fileFilter not working properly Feb 19, 2018
@lo78cn
Copy link

lo78cn commented Feb 19, 2018

You could solve the issue in the fileFilter by setting a req.fileValidationErrror and check for any fileValidationError. But errors from limits remain unvisable. So, I think this is a valid issue.

const s = multer.diskStorage({
    destination: /some/path,
    filename(req, file, cb) {
        cb(null, uuidv4());
    },
});

const ff = function fileFilter(req, file, cb) {
    const validator = new Validator();
    if (!validator.isEnum(file.mimetype, globals.FiletypeEnum)) {
        req.fileValidationError = 'unsupported mime type';
        cb(null, false);
    } else {
        cb(null, true);
    }
};

export const multerOptions = {
    storage: s,
    fileFilter: ff,
    limits: {fileSize: 1024*1024},
};
    @ApiOperation({title: 'upload a file (role: write)'})
    @Post('upload')
    @RequiredRoles('write')
    @UseInterceptors(FileInterceptor('file', multerOptions))
    async upload(@Req() req, @UploadedFile() file) {
        if (req.fileValidationError) {
            throw new BadRequestException(req.fileValidationError);
        }
        if (!file) {
            throw new BadRequestException('invalid file');
        }
        return {message: 'file has been uploaded', filename: file.filename};
    }

How do I access the limits fileSize error in my controller? serializeError(error).code would result in LIMIT_FILE_SIZE. Multer returns an error like:

{ Error: File too large
    at makeError (/some/path/node_modules/multer/lib/make-error.js:12:13)
    at abortWithCode (/some/path/node_modules/multer/lib/make-middleware.js:79:22)
    at FileStream.<anonymous> (/some/path/node_modules/multer/lib/make-middleware.js:141:11)
    at emitNone (events.js:106:13)
    at FileStream.emit (events.js:208:7)
    at PartStream.onData (/some/path/node_modules/busboy/lib/types/multipart.js:220:18)
    at emitOne (events.js:116:13)
    at PartStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11) code: 'LIMIT_FILE_SIZE', field: 'file', storageErrors: [] }

@kamilmysliwiec
Copy link
Member

Fixed 🔥

@lo78cn
Copy link

lo78cn commented Mar 12, 2018

I confirm that it is now working the way it should, great job, thanks!

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants