import type { AudioFile, HistoryEntry } from '@d3ro/api-client'; import { persistExport } from '../data-portability/portable-file'; import type { PreparedPortableFile } from '../data-portability/portability-types'; export const MAX_SHARED_AUDIO_BYTES = 50 * 1024 * 1024; export type TranscriptShareFormat = 'txt' | 'md'; type TranscriptShareEntry = Pick< HistoryEntry, 'title' | 'original_text' | 'polished_text' | 'summary_text' >; export function buildTranscriptShareText( entry: TranscriptShareEntry, format: TranscriptShareFormat, summaryLabel: string, ): string { const title = entry.title?.replace(/\s*\r?\n\s*/g, ' ').trim() ?? ''; const transcript = entry.polished_text?.trim() || entry.original_text.trim(); const summary = entry.summary_text?.trim() ?? ''; const sections = [ format === 'md' && title !== '' ? `# ${title}` : title, transcript, ]; if (summary !== '') { sections.push(`${format === 'md' ? '## ' : ''}${summaryLabel}`, summary); } return sections.filter(section => section !== '').join('\n\n'); } export function prepareTranscriptShareFile( entry: TranscriptShareEntry, format: TranscriptShareFormat, summaryLabel: string, ): Promise { return persistExport({ baseName: entry.title?.trim() || 'd3ro-transcript', extension: format, mimeType: format === 'md' ? 'text/markdown' : 'text/plain', format, text: buildTranscriptShareText(entry, format, summaryLabel), bytes: null, }); } const AUDIO_EXTENSIONS: Readonly> = { 'audio/aac': 'aac', 'audio/flac': 'flac', 'audio/mp4': 'm4a', 'audio/mpeg': 'mp3', 'audio/ogg': 'ogg', 'audio/wav': 'wav', 'audio/wave': 'wav', 'audio/webm': 'webm', 'audio/x-m4a': 'm4a', 'audio/x-wav': 'wav', 'video/mp4': 'mp4', }; export async function prepareAudioShareFile( audioFile: Pick, signedUrl: string, fetchAudio: typeof fetch = fetch, ): Promise { const mimeType = audioFile.mime_type.toLowerCase().split(';')[0].trim(); const extension = AUDIO_EXTENSIONS[mimeType]; if (extension === undefined) throw new Error('Unsupported audio share format'); if (audioFile.size_bytes > MAX_SHARED_AUDIO_BYTES) throw new Error('Audio share exceeds 50 MiB'); const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 30_000); try { const response = await fetchAudio(signedUrl, { signal: controller.signal }); if (!response.ok) throw new Error(`Audio download failed with HTTP ${response.status}`); const declaredLength = Number(response.headers.get('content-length')); if (Number.isFinite(declaredLength) && declaredLength > MAX_SHARED_AUDIO_BYTES) { throw new Error('Audio share exceeds 50 MiB'); } const bytes = new Uint8Array(await response.arrayBuffer()); if (bytes.byteLength === 0) throw new Error('Audio share is empty'); if (bytes.byteLength > MAX_SHARED_AUDIO_BYTES) throw new Error('Audio share exceeds 50 MiB'); return await persistExport({ baseName: audioFile.original_name?.replace(/\.[^.]+$/, '') || 'd3ro-audio', extension, mimeType, format: 'binary', text: null, bytes, }); } finally { clearTimeout(timeout); } }