Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): persist the mimetype a bulk media item was sent with

Yudhi Armyndharis71425f25df86
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.ts
index 901cd118b..204b35fdd 100644
--- a/src/modules/message/bulk-message.service.spec.ts
+++ b/src/modules/message/bulk-message.service.spec.ts
@@ -609,6 +609,23 @@ describe('BulkMessageService.processBatch', () => {
);
});
+ it('persists the mimetype the engine was given when a base64 item declares none', async () => {
+ const batch = makeBatch(1);
+ batch.messages = [{ chatId: '[email protected]', type: 'image', content: { image: { base64: 'AAAA' } } }];
+ repo.findOne.mockResolvedValue(batch);
+
+ await runProcessBatch();
+
+ expect(engine.sendImageMessage).toHaveBeenCalledWith(
+ expect.objectContaining({ mimetype: 'image/jpeg', data: 'AAAA' }),
+ );
+ expect(messageService.saveOutgoingMessage).toHaveBeenCalledWith(
+ 's1',
+ expect.objectContaining({ metadata: { media: { mimetype: 'image/jpeg', data: 'AAAA', filename: undefined } } }),
+ );
+ });
+
it('runs the message:sending gate for each bulk message (bulk no longer bypasses moderation)', async () => {
repo.findOne.mockResolvedValue(makeBatch(1));
diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.ts
index 71f0b553d..1b535457e 100644
--- a/src/modules/message/bulk-message.service.ts
+++ b/src/modules/message/bulk-message.service.ts
@@ -784,7 +784,7 @@ export class BulkMessageService implements OnApplicationBootstrap {
metadata: media
? {
media: {
- mimetype: media.mimetype,
+ mimetype: this.mediaMimetype(type, content),
data: stripBase64DataUri(media.base64) || media.url,
filename: media.filename,
},
@@ -798,6 +798,19 @@ export class BulkMessageService implements OnApplicationBootstrap {
}
}
+ /**
+ * The mimetype a media item is sent with, and so the one its row must record: a stored row with no
+ * mimetype cannot be served back from the media endpoint.
+ */
+ private mediaMimetype(type: string, content: BulkMessageContent): string {
+ const declared = content[type as 'image' | 'video' | 'audio' | 'document']?.mimetype;
+ if (declared) return declared;
+ if (type === 'image') return 'image/jpeg';
+ if (type === 'video') return 'video/mp4';
+ if (type === 'audio') return content.audio?.ptt ? 'audio/ogg; codecs=opus' : 'audio/mpeg';
+ return 'application/octet-stream';
+ }
+
private sendMessage(
engine: IWhatsAppEngine,
chatId: string,
@@ -811,14 +824,14 @@ export class BulkMessageService implements OnApplicationBootstrap {
: engine.sendTextMessage(chatId, content.text || '');
case 'image':
return engine.sendImageMessage(chatId, {
- mimetype: content.image?.mimetype || 'image/jpeg',
+ mimetype: this.mediaMimetype(type, content),
data: stripBase64DataUri(content.image?.base64) || content.image?.url || '',
caption: content.caption,
mentions: content.mentions,
});
case 'video':
return engine.sendVideoMessage(chatId, {
- mimetype: content.video?.mimetype || 'video/mp4',
+ mimetype: this.mediaMimetype(type, content),
data: stripBase64DataUri(content.video?.base64) || content.video?.url || '',
caption: content.caption,
mentions: content.mentions,
@@ -829,14 +842,14 @@ export class BulkMessageService implements OnApplicationBootstrap {
// (see sendAudioMessage in baileys-messaging.ts). Dropping it here would accept the field and
// then deliver an untagged voice note with nothing to say so.
return engine.sendAudioMessage(chatId, {
- mimetype: content.audio?.mimetype || (content.audio?.ptt ? 'audio/ogg; codecs=opus' : 'audio/mpeg'),
+ mimetype: this.mediaMimetype(type, content),
data: stripBase64DataUri(content.audio?.base64) || content.audio?.url || '',
ptt: content.audio?.ptt,
mentions: content.mentions,
});
case 'document':
return engine.sendDocumentMessage(chatId, {
- mimetype: content.document?.mimetype || 'application/octet-stream',
+ mimetype: this.mediaMimetype(type, content),
data: stripBase64DataUri(content.document?.base64) || content.document?.url || '',
filename: content.document?.filename,
caption: content.caption,