From d578c919a5b444934c7b522ec29b947807f1813b Mon Sep 17 00:00:00 2001 From: GanghyeonSeo Date: Fri, 7 Jun 2024 05:46:05 +0900 Subject: [PATCH] fix: imageUrl to imageUuid --- .../migration.sql | 10 ++++++++ .../migration.sql | 10 ++++++++ prisma/schema.prisma | 4 ++-- src/ai/ai.repository.ts | 8 +++---- src/ai/ai.service.ts | 23 +++++++++++++++---- src/file/file.service.ts | 8 ++++--- src/post/dto/req/CreatePost.dto.ts | 6 ++--- src/post/dto/res/postRes.dto.ts | 16 ++++--------- src/post/post.mapper.ts | 2 +- src/post/post.repository.ts | 12 +++++----- src/post/types/PostFullContent.ts | 2 +- 11 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 prisma/migrations/20240606201220_change_image_url_image_uuid/migration.sql create mode 100644 prisma/migrations/20240606201634_change_file_url_image_uuid/migration.sql diff --git a/prisma/migrations/20240606201220_change_image_url_image_uuid/migration.sql b/prisma/migrations/20240606201220_change_image_url_image_uuid/migration.sql new file mode 100644 index 0000000..023c3d2 --- /dev/null +++ b/prisma/migrations/20240606201220_change_image_url_image_uuid/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `imageUrl` on the `lookbook` table. All the data in the column will be lost. + - Added the required column `image_uuid` to the `lookbook` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "lookbook" DROP COLUMN "imageUrl", +ADD COLUMN "image_uuid" TEXT NOT NULL; diff --git a/prisma/migrations/20240606201634_change_file_url_image_uuid/migration.sql b/prisma/migrations/20240606201634_change_file_url_image_uuid/migration.sql new file mode 100644 index 0000000..ef3d421 --- /dev/null +++ b/prisma/migrations/20240606201634_change_file_url_image_uuid/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `url` on the `file` table. All the data in the column will be lost. + - Added the required column `image_uuid` to the `file` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "file" DROP COLUMN "url", +ADD COLUMN "image_uuid" TEXT NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3995c33..29e72bb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -33,7 +33,7 @@ model File { order Int @default(0) name String createdAt DateTime @default(now()) @map("created_at") - url String + imageUuid String @map("image_uuid") postId Int @map("post_id") post Post @relation(fields: [postId], references: [id]) @@ -76,7 +76,7 @@ model LookBook { deletedAt DateTime? @map("deleted_at") authorId String @map("author_id") @db.Uuid prompt String - imageUrl String + imageUuid String @map("image_uuid") author User @relation(fields: [authorId], references: [uuid]) @@map("lookbook") diff --git a/src/ai/ai.repository.ts b/src/ai/ai.repository.ts index 473c028..2bd56ff 100644 --- a/src/ai/ai.repository.ts +++ b/src/ai/ai.repository.ts @@ -25,7 +25,7 @@ export class AiRepository { createdAt: true, authorId: true, prompt: true, - imageUrl: true, + imageUuid: true, }, }) .catch((error) => { @@ -57,7 +57,7 @@ export class AiRepository { createdAt: true, authorId: true, prompt: true, - imageUrl: true, + imageUuid: true, }, }) .catch((error) => { @@ -74,7 +74,7 @@ export class AiRepository { return { total: result.length, list: result }; } - async createLookBook(prompt: string, imageUrl: string, userUuid: string) { + async createLookBook(prompt: string, imageUuid: string, userUuid: string) { this.logger.log('createLookBook'); return this.prismaService.lookBook .create({ @@ -86,7 +86,7 @@ export class AiRepository { }, createdAt: new Date(), prompt, - imageUrl, + imageUuid, }, }) .catch((error) => { diff --git a/src/ai/ai.service.ts b/src/ai/ai.service.ts index 2635713..acd20ba 100644 --- a/src/ai/ai.service.ts +++ b/src/ai/ai.service.ts @@ -25,11 +25,24 @@ export class AiService { ) {} async getLookBookById(id: number) { - return await this.aiRepository.getLookBookById(id); + const { imageUuid, ...result } = + await this.aiRepository.getLookBookById(id); + + return { + ...result, + imageUrl: await this.fileService.getSignedUrl(imageUuid), + }; } async getLookBookByUserUuid(userUuid: string) { - return await this.aiRepository.getLookBookByUserUuid(userUuid); + const results = await this.aiRepository.getLookBookByUserUuid(userUuid); + + return await Promise.all( + results.list.map(async ({ imageUuid, ...result }) => ({ + ...result, + imageUrl: await this.fileService.getSignedUrl(imageUuid), + })), + ); } async createLookBook( @@ -73,14 +86,16 @@ export class AiService { ); if (response.data) { - const s3Url = await this.fileService.uploadFile(response.data.url); + const imageUuid = await this.fileService.uploadFile(response.data.url); await this.aiRepository.createLookBook( response.data.prompt, - s3Url, + imageUuid, userUuid, ); + const s3Url = await this.fileService.getSignedUrl(imageUuid); + return { prompt: response.data.prompt, imageUrl: s3Url }; } } catch (error) { diff --git a/src/file/file.service.ts b/src/file/file.service.ts index 7173fc2..0d89687 100644 --- a/src/file/file.service.ts +++ b/src/file/file.service.ts @@ -42,13 +42,15 @@ export class FileService { throw new InternalServerErrorException(); }); - const s3Url = this.minioService.client.presignedUrl( + return imageUuid; + } + + async getSignedUrl(imageUuid: string) { + return this.minioService.client.presignedUrl( 'GET', this.configService.getOrThrow('AWS_S3_BUCKET_NAME'), imageUuid, 24 * 60 * 60, ); - - return s3Url; } } diff --git a/src/post/dto/req/CreatePost.dto.ts b/src/post/dto/req/CreatePost.dto.ts index 8a4b843..7a1bd88 100644 --- a/src/post/dto/req/CreatePost.dto.ts +++ b/src/post/dto/req/CreatePost.dto.ts @@ -22,11 +22,11 @@ export class CreatePostDto { body: string; @ApiProperty({ - example: ['image_url1,', 'image_url2', 'image_url3'], - description: '이미지 url list', + example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], + description: '이미지 uuid list', required: true, }) @IsString({ each: true }) @IsNotEmpty() - imageUrls: string[] = []; + imageUuid: string[] = []; } diff --git a/src/post/dto/res/postRes.dto.ts b/src/post/dto/res/postRes.dto.ts index 87ce349..f6c0545 100644 --- a/src/post/dto/res/postRes.dto.ts +++ b/src/post/dto/res/postRes.dto.ts @@ -22,9 +22,9 @@ export class PostDto { contents: { title: string; body: string }; @ApiProperty({ - example: ['image_url1,', 'image_url2', 'image_url3'], + example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], }) - imageUrls: string[]; + imageUuid: string[]; } export class PostListDto { @@ -48,9 +48,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUrls: { - imageUrl: ['image_url1,', 'image_url2', 'image_url3'], - }, + imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], }, { id: 1, @@ -65,9 +63,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUrls: { - imageUrl: ['image_url1,', 'image_url2', 'image_url3'], - }, + imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], }, { id: 1, @@ -82,9 +78,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUrls: { - imageUrl: ['image_url1,', 'image_url2', 'image_url3'], - }, + imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], }, ], }) diff --git a/src/post/post.mapper.ts b/src/post/post.mapper.ts index efa7d50..e47ddeb 100644 --- a/src/post/post.mapper.ts +++ b/src/post/post.mapper.ts @@ -19,7 +19,7 @@ export class PostMapper { updatedAt, author, contents: contents[0], - imageUrls: files.map(({ url }: any) => `${url}`), + imageUuid: files.map(({ uuid }: any) => `${uuid}`), }; } } diff --git a/src/post/post.repository.ts b/src/post/post.repository.ts index e7dec73..47fc826 100644 --- a/src/post/post.repository.ts +++ b/src/post/post.repository.ts @@ -35,7 +35,7 @@ export class PostRepository { body: true, }, }, - files: { select: { url: true } }, + files: { select: { imageUuid: true } }, }, }) .catch((error) => { @@ -76,7 +76,7 @@ export class PostRepository { body: true, }, }, - files: { select: { url: true } }, + files: { select: { imageUuid: true } }, }, }) .catch((error) => { @@ -92,7 +92,7 @@ export class PostRepository { } async createPost( - { title, body, imageUrls }: CreatePostDto, + { title, body, imageUuid }: CreatePostDto, userUuid: string, ) { this.logger.log('createPost'); @@ -113,10 +113,10 @@ export class PostRepository { createdAt: new Date(), files: { create: [ - ...imageUrls.map((imageUrl, idx) => ({ + ...imageUuid.map((uuid, idx) => ({ order: idx, name: title, - url: imageUrl, + imageUuid: uuid, })), ], }, @@ -124,7 +124,7 @@ export class PostRepository { include: { author: { select: { name: true, uuid: true } }, contents: { select: { title: true, body: true } }, - files: { select: { url: true } }, + files: { select: { imageUuid: true } }, }, }) .catch((error) => { diff --git a/src/post/types/PostFullContent.ts b/src/post/types/PostFullContent.ts index ccb7287..610ef0f 100644 --- a/src/post/types/PostFullContent.ts +++ b/src/post/types/PostFullContent.ts @@ -20,7 +20,7 @@ export type PostFullContent = Prisma.PostGetPayload<{ }; files: { select: { - url: true; + imageUuid: true; }; }; };