Skip to content

Commit

Permalink
modify: generateLookBook request body
Browse files Browse the repository at this point in the history
  • Loading branch information
GanghyeonSeo committed Jun 2, 2024
1 parent ffef6b2 commit a403b53
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 89 deletions.
9 changes: 2 additions & 7 deletions src/ai/ai.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
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')
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);
}
}
28 changes: 15 additions & 13 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HttpService } from '@nestjs/axios';
import {
HttpException,
HttpStatus,
Injectable,
InternalServerErrorException,
Logger,
Expand Down Expand Up @@ -32,18 +34,16 @@ export class AiService {
try {
const response = await firstValueFrom(
this.httpService
.post(
this.configService.get<string>('AI_URL')!,
{
area: {
province,
city,
district,
},
TPO,
.post(this.configService.get<string>('AI_URL')!, {
gender,
ageRange,
area: {
province,
city,
district,
},
{ params: { gender, ageRange } },
)
TPO,
})
.pipe(
catchError((err) => {
this.logger.error(err);
Expand All @@ -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);
}
}
}
30 changes: 30 additions & 0 deletions src/ai/dto/req/AiReqBody.dto.ts
Original file line number Diff line number Diff line change
@@ -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: '' },
})
Expand Down
35 changes: 0 additions & 35 deletions src/ai/dto/req/AiReqQuery.dto.ts

This file was deleted.

35 changes: 1 addition & 34 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { HttpService } from '@nestjs/axios';
import {
Injectable,
Expand All @@ -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<string>('S3_URL'),
// credentials: {
// accessKeyId: configService.get<string>('AWS_ACCESS_KEY_ID')!,
// secretAccessKey: configService.get<string>('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<string>('AWS_S3_REGION'),
// });
}
) {}

async uploadFile(url: string) {
this.logger.log('uploadFile called');
Expand All @@ -50,12 +31,6 @@ export class FileService {

const imageUuid = uuidv4();

const command = new PutObjectCommand({
Bucket: this.configService.getOrThrow<string>('AWS_S3_BUCKET_NAME'),
Key: imageUuid,
Body: response.data,
});

await this.minioService.client
.putObject(
this.configService.getOrThrow<string>('AWS_S3_BUCKET_NAME'),
Expand All @@ -73,14 +48,6 @@ export class FileService {
imageUuid,
24 * 60 * 60,
);
// const s3Url = await getSignedUrl(
// this.s3Client,
// new PutObjectCommand({
// Bucket: this.configService.getOrThrow<string>('AWS_S3_BUCKET_NAME'),
// Key: imageUuid,
// }),
// { expiresIn: 3600 },
// );

return s3Url;
}
Expand Down

0 comments on commit a403b53

Please sign in to comment.