From 41b54d6d8736e527b1f607ab9076019508dd1ea5 Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Sat, 14 Jan 2023 07:07:25 -0300 Subject: [PATCH] [FIX] Audio names not being handled properly (#4685) --- app/lib/methods/audioFile.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/lib/methods/audioFile.ts b/app/lib/methods/audioFile.ts index 31cd09f9e..2c0f7ab5d 100644 --- a/app/lib/methods/audioFile.ts +++ b/app/lib/methods/audioFile.ts @@ -4,12 +4,17 @@ import { sanitizeLikeString } from '../database/utils'; import { store } from '../store/auxStore'; import log from './helpers/log'; +const DEFAULT_EXTENSION = 'mp3'; + const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); -const parseFilename = (value: string) => { - const extension = value.substring(value.lastIndexOf('.') + 1); - const filename = sanitizeString(value.substring(value.lastIndexOf('/') + 1).split('.')[0]); - return `${filename}.${extension}`; +const getExtension = (value: string) => { + let extension = DEFAULT_EXTENSION; + const filename = value.split('/').pop(); + if (filename?.includes('.')) { + extension = value.substring(value.lastIndexOf('.') + 1); + } + return extension; }; const ensureDirAsync = async (dir: string, intermediates = true): Promise => { @@ -27,7 +32,7 @@ export const downloadAudioFile = async (url: string, fileUrl: string, messageId: const serverUrl = store.getState().server.server; const serverUrlParsed = sanitizeString(serverUrl); const folderPath = `${FileSystem.documentDirectory}audios/${serverUrlParsed}`; - const filename = `${messageId}_${parseFilename(fileUrl)}`; + const filename = `${messageId}.${getExtension(fileUrl)}`; const filePath = `${folderPath}/${filename}`; await ensureDirAsync(folderPath); const file = await FileSystem.getInfoAsync(filePath);