Files
Picsur/backend/src/models/dto/multipart.dto.ts

65 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-02-24 11:22:28 +01:00
import { MultipartFile } from 'fastify-multipart';
import { BusboyFileStream } from '@fastify/busboy';
2022-02-24 22:56:27 +01:00
import { IsDefined, IsNotEmpty, IsString } from 'class-validator';
2022-02-24 21:32:31 +01:00
import { HttpException } from '@nestjs/common';
2022-02-24 11:22:28 +01:00
export class MultiPartFileDto {
@IsString()
@IsNotEmpty()
fieldname: string;
@IsString()
@IsNotEmpty()
encoding: string;
@IsString()
@IsNotEmpty()
filename: string;
@IsString()
@IsNotEmpty()
mimetype: string;
@IsDefined()
toBuffer: () => Promise<Buffer>;
@IsDefined()
file: BusboyFileStream;
2022-02-24 21:32:31 +01:00
constructor(file: MultipartFile, exceptionOnFail: HttpException) {
2022-02-24 11:22:28 +01:00
this.fieldname = file.fieldname;
this.encoding = file.encoding;
this.filename = file.filename;
this.mimetype = file.mimetype;
2022-02-24 21:32:31 +01:00
this.toBuffer = async () => {
try {
return await file.toBuffer();
} catch (e) {
throw exceptionOnFail;
}
};
2022-02-24 11:22:28 +01:00
this.file = file.file;
}
}
export class MultiPartFieldDto {
@IsString()
@IsNotEmpty()
fieldname: string;
@IsString()
@IsNotEmpty()
encoding: string;
@IsString()
@IsNotEmpty()
value: string;
constructor(file: MultipartFile) {
this.fieldname = file.fieldname;
this.encoding = file.encoding;
this.value = (file as any).value;
}
}