Skip to content

Commit

Permalink
fix: multipart intercepter for artifats
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar committed May 10, 2024
1 parent 83733ea commit 8a0bbdc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
6 changes: 3 additions & 3 deletions api-gateway/src/api/service/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
Post,
Req,
Response,
UploadedFiles,
UseInterceptors,
} from '@nestjs/common';
import {
Expand All @@ -31,8 +30,9 @@ import { InternalServerErrorDTO } from '../../middlewares/validation/schemas/err
import { ApiImplicitQuery } from '@nestjs/swagger/dist/decorators/api-implicit-query.decorator.js';
import { ArtifactDTOItem } from '../../middlewares/validation/schemas/artifacts.js';
import { ApiImplicitParam } from '@nestjs/swagger/dist/decorators/api-implicit-param.decorator.js';
import { FilesInterceptor } from '@nestjs/platform-express';
import { Auth } from '../../auth/auth.decorator.js';
import { AnyFilesInterceptor } from '../../helpers/interceptors/multipart.js';
import { UploadedFiles } from '../../helpers/decorators/file.js';

@Controller('artifacts')
@ApiTags('artifacts')
Expand Down Expand Up @@ -181,7 +181,7 @@ export class ArtifactApi {
}
})
@ApiExtraModels(ArtifactDTOItem, InternalServerErrorDTO)
@UseInterceptors(FilesInterceptor('artifacts'))
@UseInterceptors(AnyFilesInterceptor())
@HttpCode(HttpStatus.CREATED)
@Auth(UserRole.STANDARD_REGISTRY)
async uploadArtifacts(@Req() req, @UploadedFiles() files): Promise<any> {
Expand Down
9 changes: 4 additions & 5 deletions api-gateway/src/helpers/interceptors/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export function AnyFilesInterceptor(options: MultipartOptions = {}): Type<NestIn
}

const file: MultipartFile = await getFileFromPart(part);
// const validationResult = validateFile(file, options);

// if (validationResult) throw new HttpException(validationResult, HttpStatus.UNPROCESSABLE_ENTITY);

files[part.fieldname] = files[part.fieldname] || [];
files.push(file);
}

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

req.body = body;

return next.handle();
Expand Down
2 changes: 2 additions & 0 deletions api-gateway/src/helpers/interceptors/types/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface MultipartFile {
size: number;
mimetype: string;
fieldname: string;
encoding: string;
originalname: string;
}

export class MultipartOptions {
Expand Down
27 changes: 3 additions & 24 deletions api-gateway/src/helpers/interceptors/utils/multipart.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { FileValidator } from '@nestjs/common/pipes/file/file-validator.interface';
import { FileTypeValidator, MaxFileSizeValidator } from '@nestjs/common';

import { MultipartFile as MultipartFileFastify} from '@fastify/multipart';

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

export const getFileFromPart = async (part: MultipartFileFastify): Promise<MultipartFile> => {
const buffer: Buffer = await part.toBuffer()
Expand All @@ -14,25 +11,7 @@ export const getFileFromPart = async (part: MultipartFileFastify): Promise<Multi
filename: part.filename,
mimetype: part.mimetype,
fieldname: part.fieldname,
encoding: part.encoding,
originalname: part.fieldname
};
};

export const validateFile = (file: MultipartFile, options: MultipartOptions): string | void => {
const validators: FileValidator[] = [];

if (options.maxFileSize) {
validators.push(new MaxFileSizeValidator({ maxSize: options.maxFileSize }));
}

if (options.fileType) {
validators.push(new FileTypeValidator({ fileType: options.fileType }));
}

for (const validator of validators) {
if (validator.isValid(file)) {
continue
}

return validator.buildErrorMessage(file);
}
};

0 comments on commit 8a0bbdc

Please sign in to comment.