-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
노래 삭제 API 구현 #370
노래 삭제 API 구현 #370
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위의 저 path 변수 두 개는 이후 경로의 아니면 slice 대신 그리구 console.log 빼주셔도 될 것 같아요!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수로 빼서 처리하겠습니다! |
||
|
||
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, | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엔티티에서 옵션을 걸어 삭제하는 방식은 제가 고려하지 않았던 것 같은데 훨씬 효율적일 것 같아요 제 코드도 리팩토링 해봐야겠네요 ! 👍