Wednesday, 22 May 2019

nestjs multer get list of new file names after multer storage

I have this middleware that I use to upload files.

@Injectable()
export class FilesMiddleware implements NestMiddleware {

  private storage = multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, path.join(__dirname, '../../uploads/'));
    },
    filename: (req, file, cb) => {
      let extArray = file.mimetype.split("/");
      let extension = extArray[extArray.length - 1];
      cb(null, file.fieldname + '-' + Date.now() + '.' + extension)
    }
});

  resolve(...args: any[]): MiddlewareFunction {
    return (req, res, next) => {
      console.log(req.files);
      const upload = multer({storage: this.storage});
      upload.any();
      return next();
    }
  }
}

The problem that in my request, when I use req.files it gives me the original file names instead of the new file names (with date, etc like I set in the multer storage options).

Is there a way I can get the new file names multer just uploaded with the middleware?

@Post('upload')
@UseInterceptors(FilesInterceptor('files[]', 20, {}))
public async onUpload(@Request() req, @Response() res, @UploadedFiles() files) {
    const mediaResponse = await this.media.saveMedias(0, files);
    res.json({status: true});
}



from nestjs multer get list of new file names after multer storage

No comments:

Post a Comment