2

I am using serverless + NestJS and has a image upload api.

If I uses UseInterceptors then got this error.

Error: Part terminated early due to unexpected end of multipart data
at /srv/storage-server/node_modules/dicer/lib/Dicer.js:65:36
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)

If I uses busboy, got this error

error: The "chunk" argument must be one of type string or Buffer. Received type object

I tried to intercept from handler and it's seem able to get file (see console log in below code)

export const handler: APIGatewayProxyHandler = (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false;
  if (!cachedServer) {
    bootstrapServer().then(server => {
      cachedServer = server;
      const contentType = event.headers['Content-Type'] || event.headers['content-type'];

      // Intercept here and seem working, but in controller can not get file
      var bb = new busboy({ headers: { 'content-type': contentType } });
      console.log(contentType)
      bb.on('file', function (fieldname, file, filename, encoding, mimetype) {
        const mimeType = mimetype;
        const ext = filename.split('.')[1];
        let chunks;
        console.log('file', file)
        file.on('data', function (data) {
          chunks = data;
        }).on('end', function () {
          let base64Image = new Buffer(chunks.toString(), 'binary').toString('base64');
          const fileData = new Buffer(base64Image, 'base64');
          console.log(fileData)
          // This seem working
          // <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 01 2c 01 2c 00 00 ff ed 16 e2 50 68 6f 74 6f 73 68 6f 70 20 33 2e 30 00 38 42 49 4d 04 04 00 00 00 00 00 07 ... 249548 more bytes>
        });
      })

      bb.end(event.body);

      return serverless.proxy(server, event, context);
    });
  } else {
    serverless.proxy(cachedServer, event, context);
  }
};

But in controller of NestJs, could not get file. And got error

error: The "chunk" argument must be one of type string or Buffer. Received type object

This is code in controller

  // @UseInterceptors(FileInterceptor('file', { fileFilter: imageFileFilter }))
  public async updateLogo(
      @Req() req,
      @Param("id", new ParseUUIDPipe()) id: string
      ): Promise<OrganizationResponse | ApiResponseError | any>
  {
    try {
      const contentType = req.headers['Content-Type'] || req.headers['content-type'];

      var bb = new busboy({ headers: { 'content-type': contentType } });
      bb.on('file', function (fieldname, file, filename, encoding, mimetype) {
        const mimeType = mimetype;
        const ext = filename.split('.')[1];
        let chunks;
        file.on('data', function (data) {
          chunks = data;
          console.log('file', chunks)
        }).on('end', function () {
          let base64Image = new Buffer(chunks.toString(), 'binary').toString('base64');
          const fileData = new Buffer(base64Image, 'base64');
          console.log(fileData)
        });
      })

      bb.end(req.body);
    } catch (error) {
      this.logger.error(error.message);
      throw new InternalServerErrorException(error.message);
    }
}

Any help!

Binh Ho
  • 3,690
  • 1
  • 31
  • 31

0 Answers0