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 11, 2024
1 parent 8a0bbdc commit 02a77e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 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
26 changes: 17 additions & 9 deletions api-gateway/src/helpers/interceptors/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ export function AnyFilesInterceptor(options: MultipartOptions = {}): Type<NestIn
const req = context.switchToHttp().getRequest() as FastifyRequest;

if (!req.isMultipart()) {
throw new HttpException('The request should be a form-data', HttpStatus.BAD_REQUEST)
throw new HttpException('The request should be a form-data', HttpStatus.BAD_REQUEST);
}

const files: MultipartFile[] = [];
const body = {};

for await (const part of req.parts()) {
if (part.type !== 'file') {
body[part.fieldname] = (part as MultipartValue).value;
continue;
}
try {
for await (const part of req.parts()) {
const { type, fieldname } = part;

if (type !== 'file') {
body[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);
}
}
} catch (error) {
throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
}

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

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 || !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,
};
};

0 comments on commit 02a77e3

Please sign in to comment.