From 12684c57d5e074191604e6cb84677216d5439338 Mon Sep 17 00:00:00 2001 From: leemour Date: Tue, 17 Nov 2026 22:58:35 +0100 Subject: [PATCH] fix(calls): read a recording sent as octet-stream by its file name's extension A Beeline cloud PBX serves a call recording as application/octet-stream and names the file (.mp3) in Content-Disposition. b24 took the format from the type alone and refused it as recording.not-audio, so after #148 the download worked but no call of that portal was transcribed. When the type names no format (empty or octet-stream), the format comes from the Content-Disposition file name's extension, only one b24 already keeps (mp3, wav, ogg, m4a). Checked on the live portal before the push: one call, 89 s of audio, transcribed. Co-Authored-By: Claude Opus 5.5 --- changes/fix-recording-octet-stream.md | 6 ++++++ src/bitrix/transport.ts | 11 +++++++++-- src/calls/calls.test.ts | 19 +++++++++++++++++++ src/calls/recordings.ts | 23 ++++++++++++++++++++--- 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 changes/fix-recording-octet-stream.md diff --git a/changes/fix-recording-octet-stream.md b/changes/fix-recording-octet-stream.md new file mode 100644 index 0000000..db4c9a0 --- /dev/null +++ b/changes/fix-recording-octet-stream.md @@ -0,0 +1,6 @@ +### Fixed + +- **A recording sent with no audio type.** A telephony provider's server, such as a Beeline cloud PBX, sends a + recording as `application/octet-stream` and names the file, `call.mp3`, in its `Content-Disposition`. b24 refused + it as not audio (`recording.not-audio`), so `meetings transcribe` turned none of these calls into text. It now takes + the format from the file name when the type names none. diff --git a/src/bitrix/transport.ts b/src/bitrix/transport.ts index 663f8d9..9044f7f 100644 --- a/src/bitrix/transport.ts +++ b/src/bitrix/transport.ts @@ -210,7 +210,10 @@ export class BitrixTransport { * provider gives the provider's own link to a recording; that one is followed only over https, only to a public * address, and with nothing of the portal's sign-in. */ - async download(link: string, maxBytes: number): Promise<{ bytes: Uint8Array; type: string | null }> { + async download( + link: string, + maxBytes: number, + ): Promise<{ bytes: Uint8Array; type: string | null; disposition: string | null }> { const portal = new URL(this.#auth.origin) const url = URL.canParse(link, portal.href) ? new URL(link, portal) : null if (!url) throw refusedLink("the portal gave a download link b24 cannot read") @@ -248,7 +251,11 @@ export class BitrixTransport { throw new CliError("invalid_response", `the file is larger than ${Math.round(maxBytes / 1e6)} MB`, { reason: "bitrix.download-too-large", }) - return { bytes, type: response.headers.get("content-type") } + return { + bytes, + type: response.headers.get("content-type"), + disposition: response.headers.get("content-disposition"), + } } finally { clearTimeout(timer) } diff --git a/src/calls/calls.test.ts b/src/calls/calls.test.ts index 9e6c58b..c2354a7 100644 --- a/src/calls/calls.test.ts +++ b/src/calls/calls.test.ts @@ -356,6 +356,25 @@ describe("meetings transcribe", () => { ]) }) + it("reads a recording sent with no audio type by the extension in its file name, and refuses one without", async () => { + const generic = { "content-type": "application/octet-stream;charset=utf-8" } + const portal = portalWith( + [ + row(1, { CALL_RECORD_URL: LINK }), + row(2, { CALL_RECORD_URL: LINK, CALL_START_DATE: "2026-11-11T10:00:00+03:00" }), + ], + { + "download.json": [ + { text: AUDIO, headers: generic }, + { text: AUDIO, headers: { ...generic, "content-disposition": 'attachment; filename="call-1.mp3"' } }, + ], + }, + ) + await b24(["work", "meetings", "sync", "--json"], { portal }) + const report = JSON.parse((await transcribe(portal)).out).item as { transcribed: number; failed: unknown[] } + expect(report).toMatchObject({ transcribed: 1, failed: [{ meeting: 2, reason: "recording.not-audio" }] }) + }) + it("lists a call it could not transcribe with the reason, and goes on", async () => { const portal = portalWith( [ diff --git a/src/calls/recordings.ts b/src/calls/recordings.ts index 53b2010..e1306da 100644 --- a/src/calls/recordings.ts +++ b/src/calls/recordings.ts @@ -19,6 +19,20 @@ const EXTENSIONS: Record = { } const mimeOf = (type: string | null) => (type ?? "").split(";")[0]?.trim().toLowerCase() ?? "" + +/** A type that names no format, as a telephony provider's server sends a recording (Beeline cloud PBX, live). */ +const UNNAMED = new Set(["", "application/octet-stream", "binary/octet-stream"]) + +/** + * The audio format the answer names: its type, else, when the type names none, the extension of the file name in + * `Content-Disposition`. The decoder reads the format from the bytes again, so a wrong name fails there. + */ +const extensionOf = (type: string | null, disposition: string | null | undefined) => { + const named = EXTENSIONS[mimeOf(type)] + if (named || !UNNAMED.has(mimeOf(type))) return named + const extension = /filename\*?=[^;]*\.([a-z0-9]{2,4})["']?\s*(?:;|$)/i.exec(disposition ?? "")?.[1]?.toLowerCase() + return extension && extension in MIMES ? extension : undefined +} const MIMES: Record = { mp3: "audio/mpeg", wav: "audio/wav", ogg: "audio/ogg", m4a: "audio/mp4" } /** b24's own state folder, one folder per profile, so Desktop plays a call from the file `meetings show` names. */ @@ -67,7 +81,10 @@ export interface Recordings { export const recordingsAt = ( folder: string, - download: (link: string, maxBytes: number) => Promise<{ bytes: Uint8Array; type: string | null }>, + download: ( + link: string, + maxBytes: number, + ) => Promise<{ bytes: Uint8Array; type: string | null; disposition?: string | null }>, ): Recordings => { let listed: Promise | undefined const files = () => (listed ??= readdir(folder).catch(() => [])) @@ -80,8 +97,8 @@ export const recordingsAt = ( throw new CliError("not_found", `call ${record.CALL_ID} has no recording on the portal`, { reason: "recording.missing", }) - const { bytes, type } = await download(record.CALL_RECORD_URL, LARGEST_RECORDING) - const extension = EXTENSIONS[mimeOf(type)] + const { bytes, type, disposition } = await download(record.CALL_RECORD_URL, LARGEST_RECORDING) + const extension = extensionOf(type, disposition) if (!extension) throw new CliError( "invalid_response",