jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

Get size of file #224

Closed kamalkech closed 3 years ago

kamalkech commented 3 years ago
 @Mutation(() => Boolean)
  @UseGuards(JwtAuthGplGuard)
  async uploadAvatar(
    @Args({ name: 'file', type: () => GraphQLUpload })
    file: FileUpload,
    @CurrentUser()
    user: User,
  ) {
   const { createReadStream, filename, mimetype } = await file;
   const _buffer: ReadStream = createReadStream();

   // Get file Size
   for await (const uploadChunk of _buffer) {
     byteLength += (uploadChunk as Buffer).byteLength;
     console.log('forawait');
  }

  if (max_photo_size < byteLength) {
    return false;
  }

 // Add file
 const avatar = await this.userService.addFile(_buffer);
}

add file service not runing there another way to get file size ?? Thank you for help

jaydenseric commented 3 years ago

Closing because this is a usage question; bug reports and feature suggestions are appropriate for GitHub issues.

Also, there are existing similar issues:

https://github.com/jaydenseric/graphql-upload/issues/204#issuecomment-631153444

Anyone else who has done something similar before feel free to share an answer here though.

Note that there is a maxFileSize option, if you are happy for all file uploads to share the same limit:

https://github.com/jaydenseric/graphql-upload#type-processrequestoptions

kamalkech commented 3 years ago

Understood, but maxFileSizeit s for globaly param of application, however we need to check max file size for each type of madia : photos, videos that ofc has not the same max file size!

jaydenseric commented 3 years ago

I know, I read your issue and the others. Nothing about the graphql-upload API design is blocking you, go ahead! It's just Node.js streams. Please appreciate that I can't help everyone write their resolvers; there are infinite ways people might want to handle the file upload streams in the backend according to their particular requirements. If I had a code example handy of exactly what you're trying to do I would share it, but I don't.

If you would like to champion a particular approach once you figure it out to help others, feel free to PR the examples. Be conscious that because I don't want to be responsible for an incorrect example, I might take a long time to review it and might decide the effort is not worth it if the example is only useful for a small audience.

kamalkech commented 3 years ago

Take easy man!, we ask to make sure if this issue from or not graphql-upload, and for example in @nestjs/commun we can use a decorator @UploadedFile() file and we access easly to size like code bellow :

@Post('avatar')
  @UseGuards(JwtAuthenticationGuard)
  @UseInterceptors(FileInterceptor('file'))
  async addAvatar(@UploadedFile() file: any) {
    const { createReadStream, filename, mimetype } = await file;
    const _buffer = file.buffer;
    console.log('byteLength', _buffer.byteLength);
    console.log('mimetype', mimetype);
    return false;
  }

any way, be cool and take easy again, may be we can fixe this issue on the futur, thank you for your words + time ;) !