Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Post, | |
| UnprocessableEntityException, | |
| UseInterceptors, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { SessionManager } from '@waha/core/abc/manager.abc'; | |
| import { WAMimeType } from '@waha/core/media/WAMimeType'; | |
| import { ApiFileAcceptHeader } from '@waha/nestjs/ApiFileAcceptHeader'; | |
| import { | |
| SessionApiParam, | |
| WorkingSessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { | |
| FileDTO, | |
| VideoFileDTO, | |
| VoiceFileDTO, | |
| } from '@waha/structures/media.dto'; | |
| import { WhatsappSession } from '../core/abc/session.abc'; | |
| import { BufferResponseInterceptor } from '../nestjs/BufferResponseInterceptor'; | |
| ('api_key') | |
| ('api/:session/media') | |
| ('🖼️ Media') | |
| class MediaController { | |
| constructor(private manager: SessionManager) {} | |
| ('convert/voice') | |
| ({ | |
| summary: 'Convert voice to WhatsApp format (opus)', | |
| }) | |
| ( | |
| new BufferResponseInterceptor(WAMimeType.VOICE, 'output.opus'), | |
| ) | |
| (WAMimeType.VOICE) | |
| async convertVoice( | |
| session: WhatsappSession, | |
| () file: VoiceFileDTO, | |
| ): Promise<Buffer> { | |
| const data = await this.buffer(session, file); | |
| const content = await session.mediaConverter.voice(data); | |
| return content; | |
| } | |
| ('convert/video') | |
| ({ | |
| summary: 'Convert video to WhatsApp format (mp4)', | |
| }) | |
| ( | |
| new BufferResponseInterceptor(WAMimeType.VIDEO, 'output.mp4'), | |
| ) | |
| (WAMimeType.VIDEO) | |
| async convertVideo( | |
| session: WhatsappSession, | |
| () file: VideoFileDTO, | |
| ): Promise<Buffer> { | |
| const data = await this.buffer(session, file); | |
| const content = await session.mediaConverter.video(data); | |
| return content; | |
| } | |
| private async buffer( | |
| session: WhatsappSession, | |
| file: FileDTO, | |
| ): Promise<Buffer> { | |
| if ('url' in file) { | |
| return session.fetch(file.url); | |
| } else if ('data' in file) { | |
| return Buffer.from(file.data, 'base64'); | |
| } else { | |
| throw new UnprocessableEntityException( | |
| 'Either "url" or "data" must be specified.', | |
| ); | |
| } | |
| } | |
| } | |
| export { MediaController }; | |