|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Inject, |
| 5 | + Post, |
| 6 | + UploadedFiles, |
| 7 | + UseInterceptors, |
| 8 | +} from '@nestjs/common'; |
| 9 | +import { REDIS_CLIENT } from '@/common/redis/redis.module'; |
| 10 | +import { RedisClientType } from 'redis'; |
| 11 | +import { AlbumDto } from './dto/AlbumDto'; |
| 12 | +import { FileFieldsInterceptor } from '@nestjs/platform-express'; |
| 13 | +import path from 'path'; |
| 14 | +import * as fs from 'fs/promises'; |
| 15 | +import { MusicProcessingSevice } from '@/music/music.processor'; |
| 16 | +import { AdminService } from './admin.service'; |
| 17 | + |
| 18 | +export interface UploadedFiles { |
| 19 | + albumCover?: Express.Multer.File; |
| 20 | + bannerCover?: Express.Multer.File; |
| 21 | + songs: Express.Multer.File[]; |
| 22 | +} |
| 23 | + |
| 24 | +@Controller('admin') |
| 25 | +export class AdminController { |
| 26 | + constructor( |
| 27 | + @Inject(REDIS_CLIENT) private readonly redisClient: RedisClientType, |
| 28 | + @Inject() private readonly musicProcessingService: MusicProcessingSevice, |
| 29 | + private readonly adminService: AdminService, |
| 30 | + ) {} |
| 31 | + |
| 32 | + @Post('album') |
| 33 | + @UseInterceptors( |
| 34 | + FileFieldsInterceptor([ |
| 35 | + { name: 'albumCover' }, |
| 36 | + { name: 'bannerCover' }, |
| 37 | + { name: 'songs' }, |
| 38 | + ]), |
| 39 | + ) |
| 40 | + async createAlbum( |
| 41 | + @UploadedFiles() files: UploadedFiles, |
| 42 | + @Body('albumData') albumDataString: string, |
| 43 | + ): Promise<any> { |
| 44 | + const albumData = JSON.parse(albumDataString) as AlbumDto; |
| 45 | + // TODO 성준님 1. MySQL DB에 앨범 정보 저장 하고 앨범 Id를 반환 받음 |
| 46 | + //const albumId = this.adminRepository.createAlbum(); |
| 47 | + |
| 48 | + // 앨범 임시 ID |
| 49 | + const albumId = 'RANDOM_AHH_ALBUM_ID'; |
| 50 | + const imageUrls: { |
| 51 | + albumCoverURL?: string; |
| 52 | + bannerCoverURL?: string; |
| 53 | + } = {}; |
| 54 | + |
| 55 | + //2. 앨범 커버 이미지, 배너 이미지를 S3에 업로드 하고 URL을 반환 받음 |
| 56 | + if (files.albumCover?.[0] || files.bannerCover?.[0]) { |
| 57 | + const uploadResults = await this.adminService.uploadImageFiles( |
| 58 | + files.albumCover?.[0], |
| 59 | + files.bannerCover?.[0], |
| 60 | + `converted/${albumId}`, |
| 61 | + ); |
| 62 | + |
| 63 | + Object.assign(imageUrls, uploadResults); |
| 64 | + } |
| 65 | + |
| 66 | + // await this.adminRepository.updateAlbumUrls(albumId, { |
| 67 | + // albumCoverURL, |
| 68 | + // bannerCoverURL |
| 69 | + // }); |
| 70 | + |
| 71 | + //3. 노래 파일들 처리: 기존 processSongFiles 사용 |
| 72 | + const message = await this.processSongFiles( |
| 73 | + files.songs, |
| 74 | + albumData, |
| 75 | + albumId, |
| 76 | + ); |
| 77 | + return message; |
| 78 | + |
| 79 | + // 4. MySQL DB에 노래 정보 저장 |
| 80 | + //return { albumId }; |
| 81 | + } |
| 82 | + |
| 83 | + private async processSongFiles( |
| 84 | + songFiles: Express.Multer.File[], |
| 85 | + albumData: AlbumDto, |
| 86 | + albumId: string, |
| 87 | + ): Promise<any> { |
| 88 | + const tempDir = await this.createTempDirectory(albumId); |
| 89 | + |
| 90 | + await Promise.all( |
| 91 | + songFiles.map(async (file, index) => { |
| 92 | + const songInfo = albumData.songs[index]; |
| 93 | + await this.musicProcessingService.processUpload(file, tempDir, { |
| 94 | + albumId, |
| 95 | + ...songInfo, |
| 96 | + }); |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 101 | + |
| 102 | + return { |
| 103 | + albumId, |
| 104 | + message: 'Album songs updated to object storage successfully', |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private async createTempDirectory(albumId: string): Promise<string> { |
| 109 | + const tempDir = path.join(__dirname, `album/${albumId}`); |
| 110 | + await fs.mkdir(tempDir, { recursive: true }); |
| 111 | + return tempDir; |
| 112 | + } |
| 113 | +} |
0 commit comments