From a403b53c667fc7101b6ef26f0281aada13bbd8e0 Mon Sep 17 00:00:00 2001 From: GanghyeonSeo Date: Sun, 2 Jun 2024 21:08:13 +0900 Subject: [PATCH] modify: generateLookBook request body --- src/ai/ai.controller.ts | 9 ++------ src/ai/ai.service.ts | 28 +++++++++++++------------ src/ai/dto/req/AiReqBody.dto.ts | 30 +++++++++++++++++++++++++++ src/ai/dto/req/AiReqQuery.dto.ts | 35 -------------------------------- src/file/file.service.ts | 35 +------------------------------- 5 files changed, 48 insertions(+), 89 deletions(-) delete mode 100644 src/ai/dto/req/AiReqQuery.dto.ts diff --git a/src/ai/ai.controller.ts b/src/ai/ai.controller.ts index eaf96bc..abc40ad 100644 --- a/src/ai/ai.controller.ts +++ b/src/ai/ai.controller.ts @@ -1,8 +1,7 @@ -import { Body, Controller, Post, Query } from '@nestjs/common'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { AiReqBodyDto } from './dto/req/AiReqBody.dto'; import { AiService } from './ai.service'; -import { AiReqQueryDto } from './dto/req/AiReqQuery.dto'; @Controller('ai') @ApiTags('AI') @@ -10,11 +9,7 @@ export class AiController { constructor(private readonly aiService: AiService) {} @Post() - generateLookBook( - @Query() - { gender, ageRange }: AiReqQueryDto, - @Body() { area, TPO }: AiReqBodyDto, - ) { + generateLookBook(@Body() { gender, ageRange, area, TPO }: AiReqBodyDto) { return this.aiService.generateLookBook(gender, ageRange, area, TPO); } } diff --git a/src/ai/ai.service.ts b/src/ai/ai.service.ts index 6a89bf3..a7fd0a1 100644 --- a/src/ai/ai.service.ts +++ b/src/ai/ai.service.ts @@ -1,5 +1,7 @@ import { HttpService } from '@nestjs/axios'; import { + HttpException, + HttpStatus, Injectable, InternalServerErrorException, Logger, @@ -32,18 +34,16 @@ export class AiService { try { const response = await firstValueFrom( this.httpService - .post( - this.configService.get('AI_URL')!, - { - area: { - province, - city, - district, - }, - TPO, + .post(this.configService.get('AI_URL')!, { + gender, + ageRange, + area: { + province, + city, + district, }, - { params: { gender, ageRange } }, - ) + TPO, + }) .pipe( catchError((err) => { this.logger.error(err); @@ -54,11 +54,13 @@ export class AiService { ); if (response.data) { - return this.fileService.uploadFile(response.data.url); + const s3Url = await this.fileService.uploadFile(response.data.url); + + return { prompt: response.data.prompt, url: s3Url }; } - // return this.fileService.uploadFile('https://via.placeholder.com/150'); } catch (error) { this.logger.error(error); + throw new HttpException('Error occur', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/ai/dto/req/AiReqBody.dto.ts b/src/ai/dto/req/AiReqBody.dto.ts index 13aff94..504391d 100644 --- a/src/ai/dto/req/AiReqBody.dto.ts +++ b/src/ai/dto/req/AiReqBody.dto.ts @@ -1,7 +1,37 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty } from 'class-validator'; +export enum GenderEnum { + female = '여자', + male = '남자', + none = '논바이너리', +} + +enum AgeRangeEnum { + early_teens = '10대 초반', + late_teens = '10대 후반', + early_twenties = '20대 초반', + late_twenties = '20대 후반', + early_thirties = '30대 초반', + late_thirties = '30대 후반', + early_forties = '40대 초반', + late_forties = '40대 후반', + early_fifties = '50대 초반', + late_fifties = '50대 후반', +} export class AiReqBodyDto { + @ApiProperty({ + enum: GenderEnum, + }) + @IsNotEmpty() + @ApiProperty() + gender: string; + + @ApiProperty({ enum: AgeRangeEnum }) + @IsNotEmpty() + @ApiProperty() + ageRange: string; + @ApiProperty({ example: { province: '서울특별시', city: '강남구', district: '' }, }) diff --git a/src/ai/dto/req/AiReqQuery.dto.ts b/src/ai/dto/req/AiReqQuery.dto.ts deleted file mode 100644 index 5942c07..0000000 --- a/src/ai/dto/req/AiReqQuery.dto.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty } from 'class-validator'; - -export enum GenderEnum { - female = '여자', - male = '남자', - none = '논바이너리', -} - -enum AgeRangeEnum { - early_teens = '10대 초반', - late_teens = '10대 후반', - early_twenties = '20대 초반', - late_twenties = '20대 후반', - early_thirties = '30대 초반', - late_thirties = '30대 후반', - early_forties = '40대 초반', - late_forties = '40대 후반', - early_fifties = '50대 초반', - late_fifties = '50대 후반', -} - -export class AiReqQueryDto { - @ApiProperty({ - enum: GenderEnum, - }) - @IsNotEmpty() - @ApiProperty() - gender: string; - - @ApiProperty({ enum: AgeRangeEnum }) - @IsNotEmpty() - @ApiProperty() - ageRange: string; -} diff --git a/src/file/file.service.ts b/src/file/file.service.ts index 6723122..7173fc2 100644 --- a/src/file/file.service.ts +++ b/src/file/file.service.ts @@ -1,4 +1,3 @@ -import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; import { HttpService } from '@nestjs/axios'; import { Injectable, @@ -8,35 +7,17 @@ import { import { ConfigService } from '@nestjs/config'; import { firstValueFrom } from 'rxjs'; import { v4 as uuidv4 } from 'uuid'; -import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { MinioService } from 'nestjs-minio-client'; @Injectable() export class FileService { private readonly logger = new Logger(FileService.name); - // private readonly s3Client: S3Client; constructor( private readonly configService: ConfigService, private readonly httpService: HttpService, private readonly minioService: MinioService, - ) { - // this.s3Client = new S3Client({ - // endpoint: configService.get('S3_URL'), - // credentials: { - // accessKeyId: configService.get('AWS_ACCESS_KEY_ID')!, - // secretAccessKey: configService.get('AWS_SECRET_ACCESS_KEY')!, - // }, - // logger: { - // debug: (_) => this.logger.debug(''), - // info: (_) => this.logger.log(''), - // warn: (_) => this.logger.warn(''), - // error: (_) => this.logger.error(JSON.stringify(_)), - // // trace: (_) => this.logger.debug(_), - // }, - // region: configService.get('AWS_S3_REGION'), - // }); - } + ) {} async uploadFile(url: string) { this.logger.log('uploadFile called'); @@ -50,12 +31,6 @@ export class FileService { const imageUuid = uuidv4(); - const command = new PutObjectCommand({ - Bucket: this.configService.getOrThrow('AWS_S3_BUCKET_NAME'), - Key: imageUuid, - Body: response.data, - }); - await this.minioService.client .putObject( this.configService.getOrThrow('AWS_S3_BUCKET_NAME'), @@ -73,14 +48,6 @@ export class FileService { imageUuid, 24 * 60 * 60, ); - // const s3Url = await getSignedUrl( - // this.s3Client, - // new PutObjectCommand({ - // Bucket: this.configService.getOrThrow('AWS_S3_BUCKET_NAME'), - // Key: imageUuid, - // }), - // { expiresIn: 3600 }, - // ); return s3Url; }