Skip to content

Commit

Permalink
[Documentation] file to object rename
Browse files Browse the repository at this point in the history
Fixes #93
  • Loading branch information
siwonpada committed Dec 23, 2024
1 parent a2e9679 commit fa1d599
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 51 deletions.
10 changes: 0 additions & 10 deletions libs/file/src/file.module.ts

This file was deleted.

2 changes: 0 additions & 2 deletions libs/file/src/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions libs/object/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './object.module';
export * from './object.service';
10 changes: 10 additions & 0 deletions libs/object/src/object.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -24,7 +27,15 @@ export class FileService {
});
}

async uploadFile(key: string, file: Express.Multer.File): Promise<string> {
/**
* 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<string> {
await this.s3Client
.send(
new PutObjectCommand({
Expand All @@ -43,7 +54,12 @@ export class FileService {
return key;
}

async deleteFile(key: string): Promise<void> {
/**
* 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<void> {
await this.s3Client
.send(
new DeleteObjectCommand({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions src/group/group.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ 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: [
ConfigModule,
PrismaModule,
UserModule,
RedisModule,
FileModule,
LoggerModule,
ObjectModule,
],
providers: [GroupService, GroupRepository],
controllers: [GroupController],
Expand Down
16 changes: 9 additions & 7 deletions src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
) {}

Expand Down Expand Up @@ -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<void> {
Expand All @@ -136,6 +132,12 @@ export class GroupService {
}

await this.groupRepository.deleteGroup(uuid, userUuid);

if (checkGroupExistence.profileImageKey) {
await this.objectService.deleteObject(
checkGroupExistence.profileImageKey,
);
}
}

/**
Expand Down
34 changes: 9 additions & 25 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]
}
}
}
}

0 comments on commit fa1d599

Please sign in to comment.