From 02a77e3ef15837ac85ce93c163ff83942a0103bd Mon Sep 17 00:00:00 2001 From: Ihar Date: Fri, 10 May 2024 19:33:27 +0500 Subject: [PATCH] fix: sypress 500 error instead 400 in artifact --- api-gateway/src/api/service/artifact.ts | 2 +- .../src/helpers/interceptors/multipart.ts | 26 ++++++++++++------- .../helpers/interceptors/utils/multipart.ts | 26 ++++++++++++------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/api-gateway/src/api/service/artifact.ts b/api-gateway/src/api/service/artifact.ts index cd936d83e..c5e5e8383 100644 --- a/api-gateway/src/api/service/artifact.ts +++ b/api-gateway/src/api/service/artifact.ts @@ -187,7 +187,7 @@ export class ArtifactApi { async uploadArtifacts(@Req() req, @UploadedFiles() files): Promise { try { if (!files) { - throw new HttpException('There are no files to upload', HttpStatus.UNPROCESSABLE_ENTITY) + throw new HttpException('There are no files to upload', HttpStatus.BAD_REQUEST) } const owner = req.user.did; const parentId = req.params.parentId; diff --git a/api-gateway/src/helpers/interceptors/multipart.ts b/api-gateway/src/helpers/interceptors/multipart.ts index 1a57aa19e..81acaff5a 100644 --- a/api-gateway/src/helpers/interceptors/multipart.ts +++ b/api-gateway/src/helpers/interceptors/multipart.ts @@ -15,24 +15,32 @@ export function AnyFilesInterceptor(options: MultipartOptions = {}): Type => { - const buffer: Buffer = await part.toBuffer() +export const getFileFromPart = async (part: MultipartFileFastify): Promise => { + const buffer: Buffer = await part.toBuffer(); + + const { byteLength: size } = buffer; + const { filename, mimetype, fieldname, encoding } = part; + + if (!size || !fieldname) { + return null; + } + return { buffer, - size: buffer.byteLength, - filename: part.filename, - mimetype: part.mimetype, - fieldname: part.fieldname, - encoding: part.encoding, - originalname: part.fieldname + size, + filename, + mimetype, + fieldname, + encoding, + originalname: fieldname, }; }; \ No newline at end of file