Skip to content

Commit

Permalink
fix: sypress 500 error instead 400 in artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar committed May 10, 2024
1 parent 8a0bbdc commit 0914904
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api-gateway/src/api/service/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class ArtifactApi {
async uploadArtifacts(@Req() req, @UploadedFiles() files): Promise<any> {
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;
Expand Down
11 changes: 9 additions & 2 deletions api-gateway/src/helpers/interceptors/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,28 @@ export function AnyFilesInterceptor(options: MultipartOptions = {}): Type<NestIn
const body = {};

for await (const part of req.parts()) {
if(!part.fieldname) {
continue
}

if (part.type !== 'file') {
body[part.fieldname] = (part as MultipartValue).value;
continue;
}

const file: MultipartFile = await getFileFromPart(part);
const file: MultipartFile | null = await getFileFromPart(part);

files.push(file);
if(file) {
files.push(file);
}
}

if(files.length) {
req.storedFiles = files;
}

req.body = body;
console.log("intercept3");

return next.handle();
}
Expand Down
26 changes: 17 additions & 9 deletions api-gateway/src/helpers/interceptors/utils/multipart.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { MultipartFile as MultipartFileFastify} from '@fastify/multipart';
import { MultipartFile as MultipartFileFastify } from '@fastify/multipart';

//types and interfaces
import { MultipartFile } from '../types/index.js';

export const getFileFromPart = async (part: MultipartFileFastify): Promise<MultipartFile> => {
const buffer: Buffer = await part.toBuffer()
export const getFileFromPart = async (part: MultipartFileFastify): Promise<MultipartFile | null> => {
const buffer: Buffer = await part.toBuffer();

const { byteLength: size } = buffer;
const { filename, mimetype, fieldname, encoding } = part;

if (!size) {
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,
};
};

0 comments on commit 0914904

Please sign in to comment.