From e514cf16a64a05d434b75fbe58e8dd726754110f Mon Sep 17 00:00:00 2001 From: allburov Date: Fri, 23 Oct 2020 20:42:27 +0700 Subject: [PATCH] Filter mimetypes --- README.md | 1 + src/config.service.ts | 5 +++++ src/whatsapp.service.ts | 9 ++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e8a71272..1de0353f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ All webhooks are disabled by default. - `WHATSAPP_HOOK_ONADDEDTOGROUP=http://localhost/uri` ## File storage - `WHATSAPP_FILES_FOLDER` - folder where will be stored files from chats (images, voice messages) +- `WHATSAPP_FILES_MIMETYPES` - download only these mimetypes from messages. Mimetypes must be separated by comma, without spaces: `audio,image/png,image/gif`. In order to choose type use prefix (like `audio,image`). Disabled by default, `message.clientUrl` will be equal emtpy string `""`. # Installation ```bash diff --git a/src/config.service.ts b/src/config.service.ts index bc533d75..8e915c5a 100644 --- a/src/config.service.ts +++ b/src/config.service.ts @@ -34,6 +34,11 @@ export class WhatsappConfigService { ) } + get mimetypes(): string[] | null { + const types = this.configService.get('WHATSAPP_FILES_MIMETYPES', "") + return types ? types.split(',') : null + } + get(name: string): any { return this.configService.get(name) } diff --git a/src/whatsapp.service.ts b/src/whatsapp.service.ts index ef25d30b..64e9cbc5 100644 --- a/src/whatsapp.service.ts +++ b/src/whatsapp.service.ts @@ -58,6 +58,7 @@ export class WhatsappService implements OnApplicationShutdown { private RETRY_DELAY = 15 private RETRY_ATTEMPTS = 3; readonly FILES_FOLDER: string + readonly mimetypes: string[] constructor( @Inject('WHATSAPP') private whatsapp: Whatsapp, @@ -66,8 +67,9 @@ export class WhatsappService implements OnApplicationShutdown { ) { this.log.setContext('WhatsappService') - this.clean_downloads() this.FILES_FOLDER = this.config.files_folder + this.clean_downloads() + this.mimetypes = this.config.mimetypes this.log.log('Configuring webhooks...') for (const hook of HOOKS) { @@ -135,9 +137,10 @@ export class WhatsappService implements OnApplicationShutdown { private async downloadAndDecryptMedia(message: Message) { return this.whatsapp.decryptFile(message).then(async (buffer) => { - // TODO: Add filter from env variables - if (!message.mimetype.startsWith("audio")) { + // Download only certain mimetypes + if (this.mimetypes === null || !this.mimetypes.some((type) => message.mimetype.startsWith(type))) { this.log.log(`The message ${message.id} has ${message.mimetype} media, skip it.`); + message.clientUrl = "" return message }