Skip to content

Commit

Permalink
feat: 첨부파일 관리하는 파일시스템 서비스 및 모듈 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
kmi0817 committed Aug 31, 2024
1 parent 4aca75c commit 51ab102
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions BE/src/file/file.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FileService } from './file.service';
import { Module } from '@nestjs/common';

@Module({
providers: [FileService],
exports: [FileService],
})
export class FileModule {}
35 changes: 35 additions & 0 deletions BE/src/file/file.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { unlink, writeFile } from 'fs/promises';
import { v4 as uuidv4 } from 'uuid';

@Injectable()
export class FileService {
private readonly basePath = 'static/';

constructor() {}

private generateFilename(originalname: string) {
const extension = originalname.split('.').pop();
const uniqueId = uuidv4();
return `${uniqueId}.${extension}`;
}

async upload(path: string, file: Express.Multer.File) {
const filePath =
this.basePath + path + this.generateFilename(file.originalname);
await writeFile(filePath, file.buffer);

return {
imageUrl: filePath,
path: filePath,
};
}

async getImageUrl(key: string): Promise<string> {
return this.basePath + key;
}

async delete(path: string) {
await unlink(this.basePath + path);
}
}

0 comments on commit 51ab102

Please sign in to comment.