From fa1d5999e7ea0b0ba00561baf57a3d9291e44ff7 Mon Sep 17 00:00:00 2001 From: siwonpada Date: Mon, 23 Dec 2024 15:46:05 +0900 Subject: [PATCH] [Documentation] file to object rename Fixes #93 --- libs/file/src/file.module.ts | 10 ------ libs/file/src/index.ts | 2 -- libs/object/src/index.ts | 2 ++ libs/object/src/object.module.ts | 10 ++++++ .../src/object.service.ts} | 24 ++++++++++--- libs/{file => object}/tsconfig.lib.json | 2 +- src/group/group.module.ts | 4 +-- src/group/group.service.ts | 16 +++++---- tsconfig.json | 34 +++++-------------- 9 files changed, 53 insertions(+), 51 deletions(-) delete mode 100644 libs/file/src/file.module.ts delete mode 100644 libs/file/src/index.ts create mode 100644 libs/object/src/index.ts create mode 100644 libs/object/src/object.module.ts rename libs/{file/src/file.service.ts => object/src/object.service.ts} (64%) rename libs/{file => object}/tsconfig.lib.json (82%) diff --git a/libs/file/src/file.module.ts b/libs/file/src/file.module.ts deleted file mode 100644 index 5187178..0000000 --- a/libs/file/src/file.module.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Module } from '@nestjs/common'; -import { FileService } from './file.service'; -import { ConfigModule } from '@nestjs/config'; - -@Module({ - imports: [ConfigModule], - providers: [FileService], - exports: [FileService], -}) -export class FileModule {} diff --git a/libs/file/src/index.ts b/libs/file/src/index.ts deleted file mode 100644 index a6aaaab..0000000 --- a/libs/file/src/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './file.module'; -export * from './file.service'; diff --git a/libs/object/src/index.ts b/libs/object/src/index.ts new file mode 100644 index 0000000..a2d0f2f --- /dev/null +++ b/libs/object/src/index.ts @@ -0,0 +1,2 @@ +export * from './object.module'; +export * from './object.service'; diff --git a/libs/object/src/object.module.ts b/libs/object/src/object.module.ts new file mode 100644 index 0000000..365c26c --- /dev/null +++ b/libs/object/src/object.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { ObjectService } from './object.service'; + +@Module({ + imports: [ConfigModule], + providers: [ObjectService], + exports: [ObjectService], +}) +export class ObjectModule {} diff --git a/libs/file/src/file.service.ts b/libs/object/src/object.service.ts similarity index 64% rename from libs/file/src/file.service.ts rename to libs/object/src/object.service.ts index a45db5c..af88ddc 100644 --- a/libs/file/src/file.service.ts +++ b/libs/object/src/object.service.ts @@ -10,9 +10,12 @@ import { } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +/** + * Service for using AWS S3. + */ @Injectable() -export class FileService { - private readonly logger = new Logger(FileService.name); +export class ObjectService { + private readonly logger = new Logger(ObjectService.name); private readonly s3Client: S3Client; constructor(private readonly configService: ConfigService) { this.s3Client = new S3Client({ @@ -24,7 +27,15 @@ export class FileService { }); } - async uploadFile(key: string, file: Express.Multer.File): Promise { + /** + * Uploads an object to S3. The object will be stored in the bucket defined in the configuration. + * Note that the type of the file is provided by multer. + * @param key the key of the object + * @param file the file to upload + * @returns the uploaded object key + * @throws InternalServerErrorException if the upload fails + */ + async uploadObject(key: string, file: Express.Multer.File): Promise { await this.s3Client .send( new PutObjectCommand({ @@ -43,7 +54,12 @@ export class FileService { return key; } - async deleteFile(key: string): Promise { + /** + * Deletes an object from S3. + * @param key the key of the object to delete + * @throws InternalServerErrorException if the deletion fails + */ + async deleteObject(key: string): Promise { await this.s3Client .send( new DeleteObjectCommand({ diff --git a/libs/file/tsconfig.lib.json b/libs/object/tsconfig.lib.json similarity index 82% rename from libs/file/tsconfig.lib.json rename to libs/object/tsconfig.lib.json index 5e75c9b..82e116d 100644 --- a/libs/file/tsconfig.lib.json +++ b/libs/object/tsconfig.lib.json @@ -2,7 +2,7 @@ "extends": "../../tsconfig.json", "compilerOptions": { "declaration": true, - "outDir": "../../dist/libs/file" + "outDir": "../../dist/libs/object" }, "include": ["src/**/*"], "exclude": ["node_modules", "dist", "test", "**/*spec.ts"] diff --git a/src/group/group.module.ts b/src/group/group.module.ts index 755c444..2906a03 100644 --- a/src/group/group.module.ts +++ b/src/group/group.module.ts @@ -7,7 +7,7 @@ import { RedisModule } from '@nestjs-modules/ioredis'; import { ConfigModule } from '@nestjs/config'; import { LoggerModule } from '@lib/logger'; import { PrismaModule } from '@lib/prisma'; -import { FileModule } from '@lib/file'; +import { ObjectModule } from '@lib/object'; @Module({ imports: [ @@ -15,8 +15,8 @@ import { FileModule } from '@lib/file'; PrismaModule, UserModule, RedisModule, - FileModule, LoggerModule, + ObjectModule, ], providers: [GroupService, GroupRepository], controllers: [GroupController], diff --git a/src/group/group.service.ts b/src/group/group.service.ts index a5fa7d0..21973a1 100644 --- a/src/group/group.service.ts +++ b/src/group/group.service.ts @@ -20,7 +20,7 @@ import { ConfigService } from '@nestjs/config'; import { ExpandedUser } from './types/ExpandedUser'; import { GetGroupByNameQueryDto } from './dto/req/getGroup.dto'; import { Loggable } from '@lib/logger/decorator/loggable'; -import { FileService } from '@lib/file'; +import { ObjectService } from '@lib/object'; @Injectable() @Loggable() @@ -29,7 +29,7 @@ export class GroupService { constructor( private readonly groupRepository: GroupRepository, @InjectRedis() private readonly redis: Redis, - private readonly fileService: FileService, + private readonly objectService: ObjectService, private readonly configService: ConfigService, ) {} @@ -118,13 +118,9 @@ export class GroupService { const key = `group/${groupUuid}/image/${Date.now().toString()}-${file.originalname}`; - await this.fileService.uploadFile(key, file); + await this.objectService.uploadObject(key, file); await this.groupRepository.updateGroupImage(key, groupUuid); - - if (checkGroupExistence.profileImageKey) { - await this.fileService.deleteFile(checkGroupExistence.profileImageKey); - } } async deleteGroup(uuid: string, userUuid: string): Promise { @@ -136,6 +132,12 @@ export class GroupService { } await this.groupRepository.deleteGroup(uuid, userUuid); + + if (checkGroupExistence.profileImageKey) { + await this.objectService.deleteObject( + checkGroupExistence.profileImageKey, + ); + } } /** diff --git a/tsconfig.json b/tsconfig.json index f1980c1..3fe4da4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,30 +18,14 @@ "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, "paths": { - "@lib/logger": [ - "libs/logger/src" - ], - "@lib/logger/*": [ - "libs/logger/src/*" - ], - "@lib/prisma": [ - "libs/prisma/src" - ], - "@lib/prisma/*": [ - "libs/prisma/src/*" - ], - "@lib/infoteam-idp": [ - "libs/infoteam-idp/src" - ], - "@lib/infoteam-idp/*": [ - "libs/infoteam-idp/src/*" - ], - "@lib/file": [ - "libs/file/src" - ], - "@lib/file/*": [ - "libs/file/src/*" - ] + "@lib/logger": ["libs/logger/src"], + "@lib/logger/*": ["libs/logger/src/*"], + "@lib/prisma": ["libs/prisma/src"], + "@lib/prisma/*": ["libs/prisma/src/*"], + "@lib/infoteam-idp": ["libs/infoteam-idp/src"], + "@lib/infoteam-idp/*": ["libs/infoteam-idp/src/*"], + "@lib/object": ["libs/object/src"], + "@lib/object/*": ["libs/object/src/*"] } } -} \ No newline at end of file +}