mercurius-js / mercurius-upload

graphql-upload implementation plugin for Fastify & mercurius
https://mercurius.dev/
MIT License
29 stars 12 forks source link

On file size exceded, server stop ! #18

Closed qlaffont closed 1 year ago

qlaffont commented 1 year ago

Hello, I'm trying to use mercurius upload but I have an error who stop my server if the uploaded file exceed size.

[15:48:28.141] DEBUG (152219): File truncated as it exceeds the 131100 byte size limit.
    err: {
      "type": "PayloadTooLargeError",
      "message": "File truncated as it exceeds the 131100 byte size limit.",
      "stack":
          PayloadTooLargeError: File truncated as it exceeds the 131100 byte size limit.
              at FileStream.<anonymous> (/app/node_modules/graphql-upload/processRequest.js:250:23)
              at FileStream.emit (node:events:513:28)
              at SBMH.ssCb [as _cb] (/app/node_modules/busboy/lib/types/multipart.js:479:32)
              at feed (/app/node_modules/streamsearch/lib/sbmh.js:248:10)
              at SBMH.push (/app/node_modules/streamsearch/lib/sbmh.js:104:16)
              at Multipart._write (/app/node_modules/busboy/lib/types/multipart.js:567:19)
              at writeOrBuffer (node:internal/streams/writable:392:12)
              at _write (node:internal/streams/writable:333:10)
              at Multipart.Writable.write (node:internal/streams/writable:337:10)
              at IncomingMessage.ondata (node:internal/streams/readable:766:22)
      "status": 413,
      "statusCode": 413,
      "expose": true
    }
{
  filename: 'corey-willett-4wuVqpRvqbY-unsplash.jpg',
  mimetype: 'image/jpeg',
  encoding: '7bit',
  createReadStream: [Function: createReadStream]
}
[15:48:28.142] DEBUG (152219): Server is shutting down
@Mutation(() => String)
  async updateProfilePicture(
    @Arg('image', () => GraphQLUpload, { nullable: true }) payload,
  ): Promise<string> {
    await Controller.updateProfilePicture(payload);

    return 'OK';
  }

class Controller {
  static async updateProfilePicture(userId: string, payload) {
    //Retrict uploaded file
    if (['image/jpeg', 'image/png', 'image/jpg'].indexOf(payload.mimetype) === -1) {
      throw new BadRequest({ error: GenericErrors.invalid_file_type });
    }

    try {
      const rs = payload.createReadStream(); <------ C R A S H    H E R E
    } catch (e) {
      console.log(e);
      throw new BadRequest({ error: GenericErrors.file_too_large });
    }

    console.log(payload);
    // await savim.uploadFile(`nanny/${}`)
  }
}

  fastify.register(mercuriusUpload, {
    maxFileSize: 131100, // 1 MB
    maxFiles: 1,
  });