import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { AllConfigType } from 'src/config/config.type'; import { FileRepository } from '../../persistence/file.repository'; import { FileType } from 'src/files/domain/file'; @Injectable() export class FilesLocalService { constructor( private readonly configService: ConfigService, private readonly fileRepository: FileRepository, ) {} async create(file: Express.Multer.File): Promise<{ file: FileType }> { if (!file) { throw new HttpException( { status: HttpStatus.UNPROCESSABLE_ENTITY, errors: { file: 'selectFile', }, }, HttpStatus.UNPROCESSABLE_ENTITY, ); } return { file: await this.fileRepository.create({ path: `/${this.configService.get('app.apiPrefix', { infer: true, })}/v1/${file.path}`, }), }; } }