Skip to content

Commit

Permalink
feat : 음악 삭제 API 구현
Browse files Browse the repository at this point in the history
* 음악을 삭제하는 API 구현
* 버킷에 있는 폴더도 삭제
* 트랜잭션 추가 필요
  • Loading branch information
khw3754 committed Jan 9, 2024
1 parent 307661d commit 33a468d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
16 changes: 13 additions & 3 deletions server/src/music/music.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ValidationPipe,
Query,
Logger,
Delete,
} from '@nestjs/common';
import { MusicService } from './music.service';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
Expand All @@ -22,9 +23,7 @@ import { Music } from 'src/entity/music.entity';
export class MusicController {
private readonly logger = new Logger('Music');
private objectStorage: AWS.S3;
constructor(
private readonly musicService: MusicService,
) {}
constructor(private readonly musicService: MusicService) {}

@Post()
@UsePipes(ValidationPipe)
Expand Down Expand Up @@ -103,4 +102,15 @@ export class MusicController {
this.logger.log(`GET /musics/search - keyword=${keyword}`);
return this.musicService.getCertainKeywordNicknameUser(keyword);
}

@Delete()
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async deleteMusic(
@Req() req,
@Body('music_id') music_id: string,
): Promise<string> {
const userId = req.user.user_id;
return await this.musicService.deleteMusicById(music_id, userId);
}
}
86 changes: 86 additions & 0 deletions server/src/music/music.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ERROR_CODE } from 'src/config/errorCode.enum';
import { UploadService } from 'src/upload/upload.service';
import { NcloudConfigService } from 'src/config/ncloud.config';
import { AWSError } from 'aws-sdk';
import { DeleteObjectOutput } from 'aws-sdk/clients/s3';

@Injectable()
export class MusicService {
Expand Down Expand Up @@ -187,4 +188,89 @@ export class MusicService {
);
}
}

async deleteMusicById(musicId: string, userId: string): Promise<string> {
if (!(await Music.isMusicOwner(musicId, userId))) {
this.logger.error(`music.service - deleteMusicById : NOT_EXIST_MUSIC`);
throw new CatchyException(
'NOT_EXIST_MUSIC',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.NOT_EXIST_MUSIC,
);
}

try {
const music: Music = await Music.getMusicById(musicId);
this.musicRepository.delete(music.music_id);

const sliceCount: number =
'https://catchy-tape-bucket2.kr.object.ncloudstorage.com/'.length;
const musicFilePath: string = music.music_file.slice(sliceCount, sliceCount + 41);
const coverFilePath: string = music.cover.slice(sliceCount, sliceCount + 46);
console.log(coverFilePath, musicFilePath);

if (musicFilePath) this.deleteFolder(musicFilePath);
if (coverFilePath) this.deleteFolder(coverFilePath);

return music.music_id;
} catch {
this.logger.error(`music.service - deleteMusicById : SERVICE_ERROR`);
throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
}
}

async deleteFolder(folderPath) {
let params = {
Bucket: 'catchy-tape-bucket2',
Delete: {
Objects: [],
},
};

// 폴더 내의 파일들을 삭제할 객체 목록에 추가
const filesInFolder = await this.listFilesInFolder(folderPath);
filesInFolder.forEach((file) => {
params.Delete.Objects.push({ Key: file.Key });
});

try {
// 폴더 내의 파일들을 삭제
await this.objectStorage.deleteObjects(params).promise();

// 폴더를 삭제
await this.objectStorage
.deleteObject({ Bucket: 'catchy-tape-bucket2', Key: folderPath })
.promise();
} catch (error) {
this.logger.error(`music.service - deleteFolder : SERVICE_ERROR`);
throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
}
}

async listFilesInFolder(folderPath) {
let params = {
Bucket: 'catchy-tape-bucket2',
Prefix: folderPath,
};

try {
const data = await this.objectStorage.listObjectsV2(params).promise();
return data.Contents;
} catch (error) {
this.logger.error(`music.service - listFilesInFolder : SERVICE_ERROR`);
throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
}
}
}

0 comments on commit 33a468d

Please sign in to comment.