|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Post, |
| 5 | + Query, |
| 6 | + Body, |
| 7 | + Param, |
| 8 | + Res, |
| 9 | + Headers, |
| 10 | + BadRequestException, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { Response } from 'express'; |
| 13 | +import { DownloadRequest, DownloadResult, VideoInfo, YouTubeService } from './youtube.service'; |
| 14 | + |
| 15 | + |
| 16 | +@Controller('youtube') |
| 17 | +export class YouTubeController { |
| 18 | + constructor(private readonly youtubeService: YouTubeService) { } |
| 19 | + |
| 20 | + /** |
| 21 | + * Get video information from YouTube URL |
| 22 | + */ |
| 23 | + @Get('info') |
| 24 | + async getVideoInfo(@Query('url') url: string): Promise<VideoInfo> { |
| 25 | + if (!url) { |
| 26 | + throw new BadRequestException('URL is required'); |
| 27 | + } |
| 28 | + return this.youtubeService.getVideoInfo(url); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Start download with selected format |
| 33 | + */ |
| 34 | + @Post('download') |
| 35 | + async startDownload(@Body() request: DownloadRequest): Promise<DownloadResult> { |
| 36 | + if (!request.url) { |
| 37 | + throw new BadRequestException('URL is required'); |
| 38 | + } |
| 39 | + if (!request.formatType || !['video', 'audio'].includes(request.formatType)) { |
| 40 | + throw new BadRequestException('formatType must be "video" or "audio"'); |
| 41 | + } |
| 42 | + if (!request.quality) { |
| 43 | + throw new BadRequestException('quality is required'); |
| 44 | + } |
| 45 | + return this.youtubeService.startDownload(request); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get download status |
| 50 | + */ |
| 51 | + @Get(':id') |
| 52 | + async getDownloadStatus(@Param('id') id: string): Promise<DownloadResult> { |
| 53 | + return this.youtubeService.getDownloadStatus(id); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Stream downloaded file with Range support |
| 58 | + */ |
| 59 | + @Get(':id/file') |
| 60 | + async streamFile( |
| 61 | + @Param('id') id: string, |
| 62 | + @Headers('range') range: string | undefined, |
| 63 | + @Res() res: Response, |
| 64 | + ): Promise<void> { |
| 65 | + const { buffer, contentType, filename } = await this.youtubeService.getDownloadedFile(id); |
| 66 | + const fileSize = buffer.length; |
| 67 | + |
| 68 | + // Set filename for download |
| 69 | + const encodedFilename = encodeURIComponent(filename); |
| 70 | + |
| 71 | + if (range) { |
| 72 | + const parts = range.replace(/bytes=/, '').split('-'); |
| 73 | + const start = parseInt(parts[0], 10); |
| 74 | + const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1; |
| 75 | + const chunkSize = end - start + 1; |
| 76 | + |
| 77 | + res.status(206); |
| 78 | + res.set({ |
| 79 | + 'Content-Range': `bytes ${start}-${end}/${fileSize}`, |
| 80 | + 'Accept-Ranges': 'bytes', |
| 81 | + 'Content-Length': chunkSize, |
| 82 | + 'Content-Type': contentType, |
| 83 | + 'Content-Disposition': `attachment; filename*=UTF-8''${encodedFilename}`, |
| 84 | + }); |
| 85 | + res.end(buffer.subarray(start, end + 1)); |
| 86 | + } else { |
| 87 | + res.set({ |
| 88 | + 'Accept-Ranges': 'bytes', |
| 89 | + 'Content-Length': fileSize, |
| 90 | + 'Content-Type': contentType, |
| 91 | + 'Content-Disposition': `attachment; filename*=UTF-8''${encodedFilename}`, |
| 92 | + }); |
| 93 | + res.end(buffer); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments