refactor(media): centralize voice compatibility policy

This commit is contained in:
Peter Steinberger
2026-02-14 03:17:02 +01:00
parent 03fee3c605
commit 6ebf503fa8
6 changed files with 62 additions and 40 deletions

View File

@@ -1,13 +1,13 @@
import { getFileExtension } from "./mime.js";
import { getFileExtension, normalizeMimeType } from "./mime.js";
const VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
export const TELEGRAM_VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
/**
* MIME types compatible with voice messages.
* Telegram sendVoice supports OGG/Opus, MP3, and M4A.
* https://core.telegram.org/bots/api#sendvoice
*/
const VOICE_MIME_TYPES = new Set([
export const TELEGRAM_VOICE_MIME_TYPES = new Set([
"audio/ogg",
"audio/opus",
"audio/mpeg",
@@ -17,16 +17,13 @@ const VOICE_MIME_TYPES = new Set([
"audio/m4a",
]);
export function isVoiceCompatibleAudio(opts: {
export function isTelegramVoiceCompatibleAudio(opts: {
contentType?: string | null;
fileName?: string | null;
}): boolean {
const mime = opts.contentType?.toLowerCase().trim();
if (mime) {
const baseMime = mime.split(";")[0].trim();
if (VOICE_MIME_TYPES.has(baseMime)) {
return true;
}
const mime = normalizeMimeType(opts.contentType);
if (mime && TELEGRAM_VOICE_MIME_TYPES.has(mime)) {
return true;
}
const fileName = opts.fileName?.trim();
if (!fileName) {
@@ -36,5 +33,16 @@ export function isVoiceCompatibleAudio(opts: {
if (!ext) {
return false;
}
return VOICE_AUDIO_EXTENSIONS.has(ext);
return TELEGRAM_VOICE_AUDIO_EXTENSIONS.has(ext);
}
/**
* Backward-compatible alias used across plugin/runtime call sites.
* Keeps existing behavior while making Telegram-specific policy explicit.
*/
export function isVoiceCompatibleAudio(opts: {
contentType?: string | null;
fileName?: string | null;
}): boolean {
return isTelegramVoiceCompatibleAudio(opts);
}