Skip to content

Commit

Permalink
feat : /upload/music 로직 수정
Browse files Browse the repository at this point in the history
* Object Storage에 음악이 업로드 되고, Cloud Functions의 action을 실행하는 POST 요청 전송
* 추후 응답이 잘 왔을 때 인코딩 된 m3u8 파일의 url 반환
* 응답이 제대로 오지 않았다면 에러 반환
  • Loading branch information
sk000801 committed Dec 7, 2023
1 parent a1ae456 commit 0183d34
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions server/src/upload/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import * as fs from 'fs';
import { Readable } from 'stream';
import { GreenEyeService } from '../config/greenEye.service';
import { DeleteObjectOutput } from 'aws-sdk/clients/s3';
import axios from 'axios';
import { CloudFunctionsResponseDto } from 'src/dto/cloudFunctions.response.dto';

@Injectable()
export class UploadService {
private readonly logger = new Logger('UploadService');
private objectStorage: S3;
private encodingActionUrl: string;
constructor(
private readonly nCloudConfigService: NcloudConfigService,
private readonly greenEyeService: GreenEyeService,
) {
this.objectStorage = nCloudConfigService.createObjectStorageOption();
this.encodingActionUrl = nCloudConfigService.getExecuteActionsUrl();
}

private isValidType(flag: string): boolean {
Expand Down Expand Up @@ -93,7 +97,7 @@ export class UploadService {
);
}

const uploadResult = await this.objectStorage
await this.objectStorage
.upload({
Bucket: 'catchy-tape-bucket2',
Key: `music/${musicId}/music.mp3`,
Expand All @@ -103,8 +107,45 @@ export class UploadService {
})
.promise();

return { url: uploadResult.Location };
} catch {
const params = {
container_name: 'catchy-tape-bucket2',
music_id: musicId,
};

const result: CloudFunctionsResponseDto = await axios
.post(
this.encodingActionUrl,
params,
this.nCloudConfigService.getRequestActionUrlHeaders(),
)
.then((response) => response.data)
.catch((err) => {
this.logger.error(
`upload.service - uploadMusic : MUSIC_ENCODE_REQUEST_ERROR`,
);
throw new CatchyException(
'MUSIC_ENCODE_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.MUSIC_ENCODE_ERROR,
);
});

if (!result.body.url) {
this.logger.error(`upload.service - uploadMusic : MUSIC_ENCODE_ERROR`);
throw new CatchyException(
'MUSIC_ENCODE_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.MUSIC_ENCODE_ERROR,
);
}

return { url: result.body.url };
} catch (err) {
console.log(err);
if (err instanceof CatchyException) {
throw err;
}

this.logger.error(`upload.service - uploadMusic : SERVICE_ERROR`);
throw new CatchyException(
'SERVER ERROR',
Expand Down

0 comments on commit 0183d34

Please sign in to comment.