|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Post, |
| 4 | + Get, |
| 5 | + Param, |
| 6 | + Query, |
| 7 | + UseInterceptors, |
| 8 | + UploadedFile, |
| 9 | + BadRequestException, |
| 10 | + Res, |
| 11 | + Headers, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { FileInterceptor } from '@nestjs/platform-express'; |
| 14 | +import { Response } from 'express'; |
| 15 | +import { SpeechService, SpeechTranscription } from './speech.service'; |
| 16 | + |
| 17 | +@Controller('speech') |
| 18 | +export class SpeechController { |
| 19 | + constructor(private readonly speechService: SpeechService) { } |
| 20 | + |
| 21 | + /** |
| 22 | + * Upload audio file |
| 23 | + */ |
| 24 | + @Post('upload') |
| 25 | + @UseInterceptors(FileInterceptor('file', { |
| 26 | + limits: { fileSize: 50 * 1024 * 1024 }, // 50MB limit |
| 27 | + fileFilter: (req, file, cb) => { |
| 28 | + const allowedMimes = [ |
| 29 | + 'audio/mpeg', |
| 30 | + 'audio/mp3', |
| 31 | + 'audio/wav', |
| 32 | + 'audio/wave', |
| 33 | + 'audio/ogg', |
| 34 | + 'audio/webm', |
| 35 | + 'audio/mp4', |
| 36 | + 'audio/m4a', |
| 37 | + 'audio/flac', |
| 38 | + 'audio/x-m4a', |
| 39 | + ]; |
| 40 | + if (allowedMimes.includes(file.mimetype) || file.originalname.match(/\.(mp3|wav|ogg|webm|m4a|flac)$/i)) { |
| 41 | + cb(null, true); |
| 42 | + } else { |
| 43 | + cb(new BadRequestException('Only audio files are allowed'), false); |
| 44 | + } |
| 45 | + }, |
| 46 | + })) |
| 47 | + async uploadAudio(@UploadedFile() file: { originalname: string; buffer: Buffer; mimetype: string; size: number }): Promise<SpeechTranscription> { |
| 48 | + if (!file) { |
| 49 | + throw new BadRequestException('No file uploaded'); |
| 50 | + } |
| 51 | + return this.speechService.uploadAudio(file); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Start transcription |
| 56 | + */ |
| 57 | + @Post(':id/transcribe') |
| 58 | + async transcribe(@Param('id') id: string, @Query('language') language?: string): Promise<SpeechTranscription> { |
| 59 | + return this.speechService.transcribe(id, language || 'vi'); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get transcription result |
| 64 | + */ |
| 65 | + @Get(':id') |
| 66 | + async getTranscription(@Param('id') id: string): Promise<SpeechTranscription> { |
| 67 | + return this.speechService.getTranscription(id); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Stream audio file with Range request support for seeking |
| 72 | + */ |
| 73 | + @Get(':id/audio') |
| 74 | + async streamAudio( |
| 75 | + @Param('id') id: string, |
| 76 | + @Headers('range') range: string | undefined, |
| 77 | + @Res() res: Response, |
| 78 | + ): Promise<void> { |
| 79 | + const { buffer, contentType } = await this.speechService.getAudioFile(id); |
| 80 | + const fileSize = buffer.length; |
| 81 | + |
| 82 | + // Handle Range requests for seeking |
| 83 | + if (range) { |
| 84 | + const parts = range.replace(/bytes=/, '').split('-'); |
| 85 | + const start = parseInt(parts[0], 10); |
| 86 | + const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1; |
| 87 | + const chunkSize = end - start + 1; |
| 88 | + |
| 89 | + res.status(206); |
| 90 | + res.set({ |
| 91 | + 'Content-Range': `bytes ${start}-${end}/${fileSize}`, |
| 92 | + 'Accept-Ranges': 'bytes', |
| 93 | + 'Content-Length': chunkSize, |
| 94 | + 'Content-Type': contentType, |
| 95 | + }); |
| 96 | + res.end(buffer.subarray(start, end + 1)); |
| 97 | + } else { |
| 98 | + // No range - return full file |
| 99 | + res.set({ |
| 100 | + 'Accept-Ranges': 'bytes', |
| 101 | + 'Content-Length': fileSize, |
| 102 | + 'Content-Type': contentType, |
| 103 | + 'Content-Disposition': 'inline', |
| 104 | + }); |
| 105 | + res.end(buffer); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments