Repository navigation
fix(meta): detect audio container in sendWhatsAppAudio base64 uploads #2810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KainanP
wants to merge
2
commits into
evolution-foundation:develop
Choose a base branch
from
KainanP:fix/meta-audio-mimetype
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
β1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export type AudioType = { mimetype: string; extension: string }; | ||
|
|
||
| const MPEG: AudioType = { mimetype: 'audio/mpeg', extension: 'mp3' }; | ||
|
|
||
| // Ogg page header: 'OggS', version, header type, granule (8), serial (4), sequence (4), CRC (4), segment count. | ||
| const OGG_PAGE_HEADER_SIZE = 27; | ||
|
|
||
| /** | ||
| * Whether an Ogg stream carries Opus: the first page of an Ogg/Opus stream holds only the | ||
| * identification header, which starts with the 'OpusHead' magic right after the segment table. | ||
| */ | ||
| function isOggOpus(buffer: Buffer): boolean { | ||
| if (buffer.length <= OGG_PAGE_HEADER_SIZE) return false; | ||
| const start = OGG_PAGE_HEADER_SIZE + buffer[OGG_PAGE_HEADER_SIZE - 1]; | ||
| return buffer.length >= start + 8 && buffer.toString('latin1', start, start + 8) === 'OpusHead'; | ||
| } | ||
|
|
||
| /** | ||
| * Detects the container of an audio buffer from its magic bytes, limited to the | ||
| * formats the WhatsApp Cloud API accepts (ogg/opus, mp3, aac, m4a, amr). | ||
| * Meta only accepts Ogg with the Opus codec ("base audio/ogg not supported"), so Opus is | ||
| * labelled with its codec, while other Ogg codecs (e.g. Vorbis) and unknown content fall | ||
| * back to audio/mpeg, which was the previous behaviour. | ||
| */ | ||
| export function detectAudioType(buffer: Buffer): AudioType { | ||
| if (!buffer || buffer.length < 4) return MPEG; | ||
|
|
||
| if (buffer.toString('latin1', 0, 4) === 'OggS') { | ||
| return isOggOpus(buffer) ? { mimetype: 'audio/ogg; codecs=opus', extension: 'ogg' } : MPEG; | ||
| } | ||
|
|
||
| if (buffer.toString('latin1', 0, 5) === '#!AMR') { | ||
| return { mimetype: 'audio/amr', extension: 'amr' }; | ||
| } | ||
|
|
||
| if (buffer.length >= 8 && buffer.toString('latin1', 4, 8) === 'ftyp') { | ||
| return { mimetype: 'audio/mp4', extension: 'm4a' }; | ||
| } | ||
|
|
||
| if (buffer.toString('latin1', 0, 3) === 'ID3') return MPEG; | ||
|
|
||
| // ADTS (raw AAC): 12-bit sync word plus layer bits 00; MPEG audio frames use layer bits != 00. | ||
| if (buffer[0] === 0xff && (buffer[1] & 0xf6) === 0xf0) { | ||
| return { mimetype: 'audio/aac', extension: 'aac' }; | ||
| } | ||
|
|
||
| return MPEG; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Run: npx tsx --test test/whatsapp-business-audio.test.ts | ||
| // Importing the service needs a generated Prisma client (npm run db:generate). | ||
| import '../src/api/server.module'; | ||
|
|
||
| import { BusinessStartupService } from '../src/api/integrations/channel/meta/whatsapp.business.service'; | ||
| import { detectAudioType } from '../src/utils/detectAudioType'; | ||
| import assert from 'node:assert/strict'; | ||
| import { after, describe, it } from 'node:test'; | ||
|
|
||
| after(() => setImmediate(() => process.exit(0))); | ||
|
|
||
| // Synthetic headers only: enough bytes for the magic numbers, no real audio. | ||
| // First Ogg page: 27-byte header ending in the segment count, a one-entry segment table, then the codec header. | ||
| const oggPage = (packet: Buffer) => | ||
| Buffer.concat([ | ||
| Buffer.from('OggS', 'latin1'), | ||
| Buffer.from([0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, packet.length]), | ||
| packet, | ||
| Buffer.alloc(32), | ||
| ]); | ||
| const oggOpus = oggPage(Buffer.concat([Buffer.from('OpusHead', 'latin1'), Buffer.alloc(11)])); | ||
| const oggVorbis = oggPage(Buffer.concat([Buffer.from([1]), Buffer.from('vorbis', 'latin1'), Buffer.alloc(23)])); | ||
| const mp3Id3 = Buffer.concat([Buffer.from('ID3', 'latin1'), Buffer.from([4, 0, 0, 0, 0, 0, 0]), Buffer.alloc(32)]); | ||
| const mp3Frame = Buffer.concat([Buffer.from([0xff, 0xfb, 0x90, 0x64]), Buffer.alloc(32)]); | ||
| const aacAdts = Buffer.concat([Buffer.from([0xff, 0xf1, 0x50, 0x80]), Buffer.alloc(32)]); | ||
| const m4a = Buffer.concat([Buffer.from([0, 0, 0, 0x20]), Buffer.from('ftypM4A ', 'latin1'), Buffer.alloc(32)]); | ||
| const amr = Buffer.concat([Buffer.from('#!AMR\n', 'latin1'), Buffer.alloc(32)]); | ||
|
|
||
| function fakeService() { | ||
| const uploads: any[] = []; | ||
| const self = { | ||
| configService: { get: () => ({}) }, | ||
| logger: { verbose: () => undefined }, | ||
| getIdMedia: async (media: any) => { | ||
| uploads.push({ ...media }); | ||
| return 'media-id'; | ||
| }, | ||
| }; | ||
| return { self, uploads }; | ||
| } | ||
|
|
||
| describe('BusinessStartupService.processAudio (base64, no audio converter)', () => { | ||
| it('uploads Ogg/Opus voice notes as audio/ogg with the opus codec, not audio/mpeg', async () => { | ||
| const { self, uploads } = fakeService(); | ||
| const result = await BusinessStartupService.prototype.processAudio.call( | ||
| self, | ||
| oggOpus.toString('base64'), | ||
| '+55 11 90000-0000', | ||
| undefined, | ||
| ); | ||
|
|
||
| assert.equal(uploads.length, 1); | ||
| assert.equal(uploads[0].mimetype, 'audio/ogg; codecs=opus'); | ||
| assert.match(uploads[0].fileName, /\.ogg$/); | ||
| assert.equal(result.mimetype, 'audio/ogg; codecs=opus'); | ||
| assert.equal(result.id, 'media-id'); | ||
| }); | ||
|
|
||
| it('does not label Ogg/Vorbis as audio/ogg, which Meta only accepts with Opus', async () => { | ||
| const { self, uploads } = fakeService(); | ||
| const result = await BusinessStartupService.prototype.processAudio.call( | ||
| self, | ||
| oggVorbis.toString('base64'), | ||
| '5511900000000', | ||
| undefined, | ||
| ); | ||
|
|
||
| assert.equal(uploads[0].mimetype, 'audio/mpeg'); | ||
| assert.match(uploads[0].fileName, /\.mp3$/); | ||
| assert.equal(result.mimetype, 'audio/mpeg'); | ||
| }); | ||
|
|
||
| it('keeps MP3 uploads as audio/mpeg', async () => { | ||
| const { self, uploads } = fakeService(); | ||
| const result = await BusinessStartupService.prototype.processAudio.call( | ||
| self, | ||
| mp3Id3.toString('base64'), | ||
| '5511900000000', | ||
| undefined, | ||
| ); | ||
|
|
||
| assert.equal(uploads[0].mimetype, 'audio/mpeg'); | ||
| assert.match(uploads[0].fileName, /\.mp3$/); | ||
| assert.equal(result.mimetype, 'audio/mpeg'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('detectAudioType', () => { | ||
| it('recognises the containers accepted by the Cloud API', () => { | ||
| assert.deepEqual(detectAudioType(oggOpus), { mimetype: 'audio/ogg; codecs=opus', extension: 'ogg' }); | ||
| assert.deepEqual(detectAudioType(mp3Id3), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| assert.deepEqual(detectAudioType(mp3Frame), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| assert.deepEqual(detectAudioType(aacAdts), { mimetype: 'audio/aac', extension: 'aac' }); | ||
| assert.deepEqual(detectAudioType(m4a), { mimetype: 'audio/mp4', extension: 'm4a' }); | ||
| assert.deepEqual(detectAudioType(amr), { mimetype: 'audio/amr', extension: 'amr' }); | ||
| }); | ||
|
|
||
| it('only treats Ogg as supported when the first page carries OpusHead', () => { | ||
| assert.deepEqual(detectAudioType(oggVorbis), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| assert.deepEqual(detectAudioType(oggOpus.subarray(0, 30)), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| }); | ||
|
|
||
| it('falls back to audio/mpeg for unknown or empty input', () => { | ||
| assert.deepEqual(detectAudioType(Buffer.from('hello world')), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| assert.deepEqual(detectAudioType(Buffer.alloc(0)), { mimetype: 'audio/mpeg', extension: 'mp3' }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.