NestJs Multer upload file with fileFilter cause infinite pending

576 views Asked by At

i have an issue witch Multer and NestJS to upload file. I try to check if file already exist to return an error. it's working well but if i try to re-upload a file after that, i have infinite pending request. (if i remove the filter i have no problem but i overwrite the file)

here my controller code:

@UseGuards(JwtAuthGuard)
  @UseGuards(RolesGuard)
  @Role('SCENARISTE')
  @Post('upload/sound')
  @UseInterceptors(FileInterceptor('file', {
    storage: diskStorage({
      destination: 'files/sounds',
      filename: function (req, file, callback) {
        return callback(null, file.originalname);
      }
    }),
    fileFilter: (req, file, callback) => {
      if (existsSync(join('files/sounds', file.originalname))) {
        return callback(new NotAcceptableException(ErrorType.FILE_ALREADY_EXIST), false);
      } else {
        return callback(null, true);
      }
    },
  }))
  uploadSound(@UploadedFile() file: Express.Multer.File) {
    const fileReponse = {
      originalname: file.originalname,
      mimetype: file.mimetype,
      filename: file.filename,
      size: file.size,
      destination: file.destination,
      fieldname: file.fieldname,
      path: file.path
    };
    return fileReponse;
  }

thank in advance for your help

may be the first request not close/stop correctly ?

1

There are 1 answers

3
Mostafa Fakhraei On

According to Multer's documantion, whenever you want to throw an error, you must call the callback by passing the first argument with an error, and leave the second argument or pass the false value.

Hence, try to change your code like this:

return callback(new NotAcceptableException(ErrorType.FILE_ALREADY_EXIST));