Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(media): share the mimetype extension lookup
Yudhi Armyndharis62d9d2e03824
diff --git a/src/common/media/mime-ext.spec.ts b/src/common/media/mime-ext.spec.tsnew file mode 100644index 000000000..5bfd8dc40--- /dev/null+++ b/src/common/media/mime-ext.spec.ts@@ -0,0 +1,26 @@+import { extFromMimetype } from './mime-ext';++describe('extFromMimetype', () => {+ it.each([+ ['image/png', 'png'],+ ['audio/ogg; codecs=opus', 'ogg'],+ ['IMAGE/WEBP', 'webp'],+ ['application/pdf', 'pdf'],+ ])('uses the subtype of %s as the extension', (mimetype, ext) => {+ expect(extFromMimetype(mimetype)).toBe(ext);+ });++ it.each([+ ['image/jpeg', 'jpg'],+ ['video/quicktime', 'mov'],+ ])('maps %s to the conventional %s', (mimetype, ext) => {+ expect(extFromMimetype(mimetype)).toBe(ext);+ });++ it.each(['', 'image', 'text/', 'image/svg+xml', 'application/vnd.ms-excel', 'image/../x'])(+ 'falls back to bin for %p',+ mimetype => {+ expect(extFromMimetype(mimetype)).toBe('bin');+ },+ );+});diff --git a/src/common/media/mime-ext.ts b/src/common/media/mime-ext.tsnew file mode 100644index 000000000..5bd40929e--- /dev/null+++ b/src/common/media/mime-ext.ts@@ -0,0 +1,9 @@+/** Subtypes whose registered mimetype name differs from the conventional file extension. */+const MIME_SUBTYPE_EXT_OVERRIDES: Record<string, string> = { jpeg: 'jpg', quicktime: 'mov' };++/** File extension to store a media blob under, derived from its mimetype; 'bin' when unrecognized. */+export function extFromMimetype(mimetype: string): string {+ const subtype = mimetype.split('/')[1]?.split(';')[0]?.trim().toLowerCase();+ if (!subtype || !/^[a-z0-9]+$/.test(subtype)) return 'bin';+ return MIME_SUBTYPE_EXT_OVERRIDES[subtype] ?? subtype;+}diff --git a/src/modules/chat-media/chat-media-archive.service.ts b/src/modules/chat-media/chat-media-archive.service.tsindex 47e9a6a5e..81a844dc4 100644--- a/src/modules/chat-media/chat-media-archive.service.ts+++ b/src/modules/chat-media/chat-media-archive.service.ts@@ -6,6 +6,7 @@ import { randomUUID } from 'crypto';import { Message } from '../message/entities/message.entity';import { StorageService } from '../../common/storage/storage.service';import { sweepOrphanedFiles } from '../../common/storage/orphan-sweep';+import { extFromMimetype } from '../../common/media/mime-ext';import { createLogger } from '../../common/services/logger.service';/** Storage key prefix owned by the chat-media archive; the media bucket is shared with statuses. */@@ -28,16 +29,6 @@ const PURGE_MAX_BATCHES_PER_RUN = 200;/** Archive keys reconciled per orphan-sweep query — bounds peak memory independently of store size. */const SWEEP_CHUNK_SIZE = 500;-/** Subtypes whose registered mimetype name differs from the conventional file extension. */-const MIME_SUBTYPE_EXT_OVERRIDES: Record<string, string> = { jpeg: 'jpg', quicktime: 'mov' };--/** File extension to store a blob under, derived from its mimetype; 'bin' when unrecognized. */-function extFromMimetype(mimetype: string): string {- const subtype = mimetype.split('/')[1]?.split(';')[0]?.trim().toLowerCase();- if (!subtype || !/^[a-z0-9]+$/.test(subtype)) return 'bin';- return MIME_SUBTYPE_EXT_OVERRIDES[subtype] ?? subtype;-}-/** `metadata.media.data` holding a remote URL (a URL-based send) rather than base64 bytes. */const MEDIA_URL_POINTER = /^https?:\/\//i;diff --git a/src/modules/status-store/status-store.service.ts b/src/modules/status-store/status-store.service.tsindex 004131f3e..ee2d4cca8 100644--- a/src/modules/status-store/status-store.service.ts+++ b/src/modules/status-store/status-store.service.ts@@ -8,6 +8,7 @@ import type { IncomingStatus } from './incoming-status';import type { Status } from '../../engine/interfaces/whatsapp-engine.interface';import { StorageService } from '../../common/storage/storage.service';import { sweepOrphanedFiles } from '../../common/storage/orphan-sweep';+import { extFromMimetype } from '../../common/media/mime-ext';import { isUniqueViolation } from '../../common/utils/db-errors';import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';import { parseWaId, userPart } from '../../engine/identity/wa-id';@@ -38,16 +39,6 @@ const DEFAULT_ORPHAN_GRACE_MS = 60 * 60 * 1000;/** Storage key prefix owned by the status store; the media bucket is shared with chat media. */const STATUS_MEDIA_PREFIX = 'statuses/';-/** Subtypes whose registered mimetype name differs from the conventional file extension. */-const MIME_SUBTYPE_EXT_OVERRIDES: Record<string, string> = { jpeg: 'jpg', quicktime: 'mov' };--/** File extension to store a status media blob under, derived from its mimetype; 'bin' when unrecognized. */-function extFromMimetype(mimetype: string): string {- const subtype = mimetype.split('/')[1]?.split(';')[0]?.trim().toLowerCase();- if (!subtype || !/^[a-z0-9]+$/.test(subtype)) return 'bin';- return MIME_SUBTYPE_EXT_OVERRIDES[subtype] ?? subtype;-}-/*** Persists inbound status/story broadcasts (`StatusUpdate` rows) with a 24h TTL, storing any attached* media through `StorageService`. Runs two recurring sweeps: the TTL purge (once at startup, then