Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

1.5 KB
Raw Blame
import { IsString, IsNotEmpty, ValidateIf } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsMediaUrl } from '../../../common/media/media-url';
import { stripBase64DataUri } from '../../message/media-cap.util';
import { ToStrictString } from '../../../common/utils/strict-boolean';

/**
 * Media to convert, in the same url-or-base64 shape every other media endpoint accepts.
 *
 * No `mimetype` field: ffmpeg identifies the input from its contents, so a declared type would be
 * ignored at best and misleading at worst. The OUTPUT type is fixed by the endpoint, and is returned
 * in the response.
 */
export class ConvertMediaDto {
  @ApiPropertyOptional({
    description: 'Public http(s) URL of the media to convert (server-fetched, SSRF-guarded).',
    example: 'https://example.com/note.m4a',
  })
  @ValidateIf((dto: ConvertMediaDto) => dto.base64 === undefined || dto.url !== undefined)
  @ToStrictString()
  @IsString()
  // Same rule as every send DTO: base64 wins when it holds data, so a url next to it is not fetched.
  @IsMediaUrl<ConvertMediaDto>({ ignoreWhen: o => !!stripBase64DataUri(o.base64) })
  url?: string;

  @ApiPropertyOptional({
    description: 'Base64-encoded media to convert. Takes precedence when both are supplied.',
    example: 'SUQzBAAAAAAA...',
  })
  @ValidateIf((dto: ConvertMediaDto) => dto.url === undefined || dto.base64 !== undefined)
  @ToStrictString()
  @IsString()
  @IsNotEmpty()
  base64?: string;
}