Filter mimetypes
This commit is contained in:
1 parent
9145d565f9
commit
e514cf16a6
3 files changed
+12
-3
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user