From 989e6091657d6c5153f9158c1985847590307805 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 7 Jun 2023 17:15:44 -0300 Subject: [PATCH] refactor handleMediaDownload and deleteMedia --- app/lib/methods/handleMediaDownload.ts | 50 +++++++++++++------------- app/views/SettingsView/index.tsx | 6 ++-- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index 15ca324d8..623fe3915 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -6,26 +6,23 @@ import { sanitizeLikeString } from '../database/utils'; import { store } from '../store/auxStore'; import log from './helpers/log'; -export enum MediaTypes { - audio = 'audio', - image = 'image', - video = 'video' -} +export type MediaTypes = 'audio' | 'image' | 'video'; + const typeString = { - [MediaTypes.audio]: 'audios/', - [MediaTypes.image]: 'images/', - [MediaTypes.video]: 'videos/' + audio: 'audios/', + image: 'images/', + video: 'videos/' }; const defaultType = { - [MediaTypes.audio]: 'mp3', - [MediaTypes.image]: 'jpg', - [MediaTypes.video]: 'mp4' + audio: 'mp3', + image: 'jpg', + video: 'mp4' }; const downloadQueue: { [index: string]: FileSystem.DownloadResumable } = {}; -export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`; +export const mediaDownloadKey = (mediaType: MediaTypes, downloadUrl: string) => `${mediaType}-${sanitizeString(downloadUrl)}`; export function isDownloadActive(mediaType: MediaTypes, messageId: string): boolean { return !!downloadQueue[mediaDownloadKey(mediaType, messageId)]; @@ -45,18 +42,16 @@ export async function cancelDownload(mediaType: MediaTypes, messageId: string): export function downloadMediaFile({ mediaType, - messageId, downloadUrl, path }: { mediaType: MediaTypes; - messageId: string; downloadUrl: string; path: string; }): Promise { return new Promise(async (resolve, reject) => { try { - const downloadKey = mediaDownloadKey(mediaType, messageId); + const downloadKey = mediaDownloadKey(mediaType, downloadUrl); downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path); const result = await downloadQueue[downloadKey].downloadAsync(); if (result?.uri) { @@ -71,7 +66,11 @@ export function downloadMediaFile({ export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`; -const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); +const sanitizeString = (value: string) => { + const urlWithoutQueryString = value.split('?')[0]; + return sanitizeLikeString(urlWithoutQueryString.substring(urlWithoutQueryString.lastIndexOf('/') + 1)); +}; +const serverUrlParsedAsPath = (serverURL: string) => `${sanitizeString(serverURL)}/`; const getExtension = (type: MediaTypes, mimeType?: string) => { if (!mimeType) { @@ -105,21 +104,22 @@ const ensureDirAsync = async (dir: string, intermediates = true): Promise export const searchMediaFileAsync = async ({ type, mimeType, - messageId + urlToCache }: { type: MediaTypes; mimeType?: string; - messageId: string; + urlToCache: string; }) => { let file; let filePath = ''; try { const serverUrl = store.getState().server.server; - const serverUrlParsed = sanitizeString(serverUrl); - const folderPath = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`; - const filename = `${messageId}.${getExtension(type, mimeType)}`; - filePath = `${folderPath}/${filename}`; + const serverUrlParsed = serverUrlParsedAsPath(serverUrl); + const folderPath = `${LOCAL_DOCUMENT_PATH}${serverUrlParsed}${typeString[type]}`; + const fileUrlSanitized = sanitizeString(urlToCache); + const filename = `${fileUrlSanitized}.${getExtension(type, mimeType)}`; + filePath = `${folderPath}${filename}`; await ensureDirAsync(folderPath); file = await FileSystem.getInfoAsync(filePath); } catch (e) { @@ -128,10 +128,10 @@ export const searchMediaFileAsync = async ({ return { file, filePath }; }; -export const deleteMediaFiles = async (type: MediaTypes, serverUrl: string): Promise => { +export const deleteMediaFiles = async (serverUrl: string): Promise => { try { - const serverUrlParsed = sanitizeString(serverUrl); - const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`; + const serverUrlParsed = serverUrlParsedAsPath(serverUrl); + const path = `${LOCAL_DOCUMENT_PATH}${serverUrlParsed}`; await FileSystem.deleteAsync(path, { idempotent: true }); } catch (error) { log(error); diff --git a/app/views/SettingsView/index.tsx b/app/views/SettingsView/index.tsx index d783118fb..abe810c8e 100644 --- a/app/views/SettingsView/index.tsx +++ b/app/views/SettingsView/index.tsx @@ -21,7 +21,7 @@ import { APP_STORE_LINK, FDROID_MARKET_LINK, isFDroidBuild, LICENSE_LINK, PLAY_M import database from '../../lib/database'; import { useAppSelector } from '../../lib/hooks'; import { clearCache } from '../../lib/methods'; -import { deleteMediaFiles, MediaTypes } from '../../lib/methods/handleMediaDownload'; +import { deleteMediaFiles } from '../../lib/methods/handleMediaDownload'; import { getDeviceModel, getReadableVersion, isAndroid } from '../../lib/methods/helpers'; import EventEmitter from '../../lib/methods/helpers/events'; import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info'; @@ -99,9 +99,7 @@ const SettingsView = (): React.ReactElement => { confirmationText: I18n.t('Clear'), onPress: async () => { dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Clear_cache_loading') })); - await deleteMediaFiles(MediaTypes.image, server); - await deleteMediaFiles(MediaTypes.audio, server); - await deleteMediaFiles(MediaTypes.video, server); + await deleteMediaFiles(server); await clearCache({ server }); await FastImage.clearMemoryCache(); await FastImage.clearDiskCache();