Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/fix-recording-octet-stream.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 9 additions & 2 deletions src/bitrix/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions src/calls/calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down
23 changes: 20 additions & 3 deletions src/calls/recordings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ const EXTENSIONS: Record<string, string> = {
}

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<string, string> = { 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. */
Expand Down Expand Up @@ -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<string[]> | undefined
const files = () => (listed ??= readdir(folder).catch(() => []))
Expand All @@ -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",
Expand Down
Loading