From 1ea0a6db2dc663f5dc4afc8841d5677cb3ea31bf Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Tue, 2 Nov 2021 14:35:27 +0500 Subject: [PATCH] add clearFileStorage function --- apps/mobile/src/utils/filesystem.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/mobile/src/utils/filesystem.js b/apps/mobile/src/utils/filesystem.js index 4f24f5dad..eb922091a 100644 --- a/apps/mobile/src/utils/filesystem.js +++ b/apps/mobile/src/utils/filesystem.js @@ -116,10 +116,10 @@ async function downloadFile(filename, {url, headers}, cancelToken) { let path = `${cacheDir}/${filename}`; let exists = await RNFetchBlob.fs.exists(path); if (exists) { - console.log("file is downloaded"); + console.log('file is downloaded'); return true; } - console.log('downloading again') + console.log('downloading again'); let res = await fetch(url, { method: 'GET', headers @@ -182,7 +182,7 @@ function cancelable(operation) { execute: () => operation(filename, {url, headers}, cancelToken), cancel: async () => { await cancelToken.cancel(); - RNFetchBlob.fs.unlink(`${cacheDir}/${filename}`).catch(console.log) + RNFetchBlob.fs.unlink(`${cacheDir}/${filename}`).catch(console.log); } }; }; @@ -232,9 +232,9 @@ async function downloadAttachment(hash, global = true) { message: attachment.metadata.filename + ' downloaded', type: 'success' }); - RNFetchBlob.fs.unlink( - RNFetchBlob.fs.dirs.CacheDir + `/${attachment.metadata.hash}` - ).catch(console.log); + RNFetchBlob.fs + .unlink(RNFetchBlob.fs.dirs.CacheDir + `/${attachment.metadata.hash}`) + .catch(console.log); if (Platform.OS === 'ios') { fileUri = folder.uri + `/${attachment.metadata.filename}`; @@ -266,6 +266,18 @@ async function downloadAttachment(hash, global = true) { } } +async function clearFileStorage() { + try { + let files = await RNFetchBlob.fs.ls(cacheDir); + for (let file of files) { + await RNFetchBlob.fs.unlink(file).catch(console.log); + } + console.log(await RNFetchBlob.fs.ls(cacheDir)); + } catch (e) { + console.log(e); + } +} + export default { readEncrypted, writeEncrypted, @@ -273,5 +285,6 @@ export default { downloadFile: cancelable(downloadFile), deleteFile, exists, - downloadAttachment + downloadAttachment, + clearFileStorage };