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

FileInterceptor : The file size check of FileInterceptor is not effective, and the file will be compressed, and the file in fileFilter does not have a file size key #14357

Closed
2 of 15 tasks
linhaidi opened this issue Dec 31, 2024 · 2 comments
Labels
needs triage This issue has not been looked into

Comments

@linhaidi
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

import {
  Controller,
  Post,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { FileService } from './file.service';
import { FileInterceptor } from '@nestjs/platform-express';
import * as dayjs from 'dayjs';
import * as fs from 'fs';
import { diskStorage } from 'multer';
import { extname } from 'path';

@Controller('file')
export class FileController {
  constructor(private readonly fileService: FileService) {}

  @Post()
  @UseInterceptors(
    FileInterceptor(
      'file',

      {
        storage: diskStorage({
          destination: (req, file, callback) => {
            const uploadPath = './uploads/images';
            if (!fs.existsSync(uploadPath)) {
              fs.mkdirSync(uploadPath, { recursive: true });
              callback(null, uploadPath);
            } else {
              callback(null, uploadPath);
            }
          },
          filename: (req, file, callback) => {
            const uniqueSuffix = dayjs().format('YYYYMMDDHHmmssSSS');
            const ext = extname(file.originalname);
            callback(null, `${uniqueSuffix}${ext}`);
          },
        }),
        fileFilter: (req, file, callback) => {
          console.log(file, 'file>>');

          callback(null, true);
        },
        limits: {
          fileSize: 0.0005 * 1024 * 1024, // 文件大小限制
        },
      },
    ),
  )
  create(
    @UploadedFile()
    file: Express.Multer.File,
  ) {
    console.log(file, '>>>>>>>>>>>');
  }
}

Minimum reproduction code

https://github.com/uqianr/fileInterceptor-error-example

Steps to reproduce

I uploaded a file that didn't meet the size limit, and it didn't pop up an error but changed the file size

Expected behavior

I hope it can pop up an error and stop writing to disk, or in the filter file can get the file size and manually implement verification. Instead of verifying through Express.Multer.File, because at this time it has already been written to disk

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

10.0.0

Packages versions

{
  "name": "project-name",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.3.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^10.4.15",
    "@nestjs/serve-static": "^4.0.2",
    "@nestjs/typeorm": "^10.0.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "dayjs": "^1.11.13",
    "multer": "1.4.5-lts.1",
    "mysql2": "^3.12.0",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1",
    "typeorm": "^0.3.20"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^5.0.0",
    "@types/jest": "^29.5.2",
    "@types/multer": "^1.4.12",
    "@types/node": "^20.3.1",
    "@types/supertest": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "^8.0.0",
    "@typescript-eslint/parser": "^8.0.0",
    "eslint": "^8.0.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

20.18.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@linhaidi linhaidi added the needs triage This issue has not been looked into label Dec 31, 2024
@linhaidi
Copy link
Author

linhaidi commented Jan 2, 2025

Come help me

@kamilmysliwiec
Copy link
Member

Please, report this issue in the multer repository as that's what we use for file uploading

@nestjs nestjs locked and limited conversation to collaborators Jan 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants