From 6569547cdd56f879dc3c82aa0ba4a12f34f5b8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 16 Nov 2023 16:01:08 +0100 Subject: [PATCH] MOBILE-3947 dev: Remove non existing sites file storage --- src/core/features/settings/pages/dev/dev.html | 10 ++++++ src/core/features/settings/pages/dev/dev.ts | 12 +++++++ src/core/services/file.ts | 32 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/core/features/settings/pages/dev/dev.html b/src/core/features/settings/pages/dev/dev.html index b817c61ce..0daa68c2b 100644 --- a/src/core/features/settings/pages/dev/dev.html +++ b/src/core/features/settings/pages/dev/dev.html @@ -79,6 +79,16 @@ + + +

Clean file storage

+

Clear deleted sites from file storage and temp folder

+
+ + + +
+ diff --git a/src/core/features/settings/pages/dev/dev.ts b/src/core/features/settings/pages/dev/dev.ts index dd4a6f48e..3a7072d9a 100644 --- a/src/core/features/settings/pages/dev/dev.ts +++ b/src/core/features/settings/pages/dev/dev.ts @@ -20,6 +20,7 @@ import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { CoreUserTours } from '@features/usertours/services/user-tours'; import { CoreCacheManager } from '@services/cache-manager'; import { CoreConfig } from '@services/config'; +import { CoreFile } from '@services/file'; import { CoreNavigator } from '@services/navigator'; import { CorePlatform } from '@services/platform'; import { CoreSites } from '@services/sites'; @@ -196,6 +197,17 @@ export class CoreSettingsDevPage implements OnInit { await CoreDomUtils.showToast('Caches invalidated', true, ToastDuration.LONG); } + /** + * Delete all data from the app. + */ + async clearFileStorage(): Promise { + const sites = await CoreSites.getSitesIds(); + await CoreFile.clearDeletedSitesFolder(sites); + await CoreFile.clearTmpFolder(); + + CoreDomUtils.showToast('File storage cleared'); + } + async setEnabledStagingSites(enabled: boolean): Promise { if (this.enableStagingSites === this.previousEnableStagingSites) { return; diff --git a/src/core/services/file.ts b/src/core/services/file.ts index 0f3798ce0..939cea4ee 100644 --- a/src/core/services/file.ts +++ b/src/core/services/file.ts @@ -1134,6 +1134,38 @@ export class CoreFileProvider { await CoreUtils.ignoreErrors(this.removeDir(CoreFileProvider.TMPFOLDER)); } + /** + * Remove deleted sites folders. + * + * @returns Promise resolved when done. + */ + async clearDeletedSitesFolder(existingSiteNames: string[]): Promise { + // Ignore errors because the folder might not exist. + const dirPath = CoreFileProvider.SITESFOLDER; + + // Get the directory contents. + try { + const contents = await this.getDirectoryContents(dirPath); + + if (!contents.length) { + return; + } + + const promises: Promise[] = contents.map(async (file) => { + if (file.isDirectory) { + if (!existingSiteNames.includes(file.name)) { + // Site does not exist... delete it. + await CoreUtils.ignoreErrors(this.removeDir(this.getSiteFolder(file.name))); + } + } + }); + + await Promise.all(promises); + } catch { + // Ignore errors, maybe it doesn't exist. + } + } + /** * Given a folder path and a list of used files, remove all the files of the folder that aren't on the list of used files. *