dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
117 stars 23 forks source link

Can't validate array of files size wise nor mimetype wise #51

Open Yuniac opened 10 months ago

Yuniac commented 10 months ago

Hello. Thanks for creating this library.

A while ago, I had an issue where files sent as an array weren't appropriately received on the server and to fix this I used this hack from another issue that was opened about this: https://github.com/dmitriy-nz/nestjs-form-data/issues/47#issuecomment-1710264360

Now, when using version 1.9.1 I can't send an array of files, the validation always fails for the size and type of the files. It would only work if I send one file. Please how can I solve this? I have used the code from the readme even to see if it would work but it still doesn't work.

DTO:

export class ImagePostDto extends BasePostDto {
  @IsFiles({ each: true })
  @MaxFileSize(1e7, { each: true, message: 'The maximum size for a file is 10mb' })
  @HasMimeType(types, {
    each: true,
    message: (e) => {
      return `Error message`;
    },
  })
  files: FileSystemStoredFile[];
}

And I tried to send both via Postman and via my code, neither would work:

Code:


   const formData = new FormData();
   // files is just an array of files
    for (const file of files) {
      formData.append('files[]', file);
    }

   axios.post(url, formData);

PostMan:

Screenshot 2023-10-12 at 15 27 09

And this is the error I get:


{"message":["Error message","The maximum size for a file is 10mb","Field \"files\" does not contain file"],"error":"Bad Request","statusCode":400}

Here are the images that I'm sending from the request body log:


body: {
      files: [
        {
          originalName: '3e132..png',
          encoding: '7bit',
          busBoyMimeType: 'image/png',
          path: '/tmp/nestjs-tmp-storage/3e132.-79988c.png',
          size: 6368,
          fileType: { ext: 'webp', mime: 'image/webp' }
        },
        {
          originalName: '512818_IGDB-272x380.jpg',
          encoding: '7bit',
          busBoyMimeType: 'image/jpeg',
          path: '/tmp/nestjs-tmp-storage/512818_IGDB-272x380-9988c5.jpg',
          size: 33564,
          fileType: { ext: 'jpg', mime: 'image/jpeg' }
        }
      ]
    }

Thanks in advance. I appreciate any further guidance on those to properly use your library

Yuniac commented 10 months ago

This is still broken, I degraded the library to 1.9.0 and used this solution : https://github.com/dmitriy-nz/nestjs-form-data/issues/47#issuecomment-1710264360

This is the only thing that works

dmitriy-nz commented 10 months ago

@Yuniac Hi, I am trying to reproduce your issue but it is not reproducible. Automatic tests will not allow you to certify a version with obvious behavioral errors. Please provide more details about your environment, nest version, nodejs, controller. Or create a small repository where the issue is reproduced. Here is my environment where I tried:

package.json

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "nestjs-form-data": "^1.9.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },

app.controller.ts

import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Post,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { ImagePostDto } from './ImagePostDto';
import { FileSystemStoredFile, FormDataRequest } from 'nestjs-form-data';

@Controller()
export class AppController {
  @Post('files')
  @UsePipes(new ValidationPipe({ transform: true }))
  @FormDataRequest({ storage: FileSystemStoredFile })
  @HttpCode(HttpStatus.OK)
  getHello(@Body() body: ImagePostDto): any {
    console.log(body);
    return body;
  }
}

ImagePostDto.ts

import {
  FileSystemStoredFile,
  HasMimeType,
  IsFiles,
  MaxFileSize,
} from 'nestjs-form-data';

export class ImagePostDto {
  @IsFiles({ each: true })
  @MaxFileSize(1e7, {
    each: true,
    message: 'The maximum size for a file is 10mb',
  })
  @HasMimeType(['image/png', 'image/jpeg'], {
    each: true,
    message: (e) => {
      return `Error message`;
    },
  })
  files: FileSystemStoredFile[];
}

Request & response: image

dmitriy-nz commented 2 months ago

Hi everyone, thanks for your feedback This issue should be also fixed in the version 1.9.9, please try it out https://github.com/dmitriy-nz/nestjs-form-data/issues/60#issuecomment-2184924859 @Yuniac