MOBILE-3947 dev: Remove non existing sites file storage

main
Pau Ferrer Ocaña 2023-11-16 16:01:08 +01:00
parent 258d12f4d0
commit 6569547cdd
3 changed files with 54 additions and 0 deletions

View File

@ -79,6 +79,16 @@
</ion-label>
</ion-item>
<ion-item class="ion-text-wrap">
<ion-label>
<p class="item-heading">Clean file storage</p>
<p>Clear deleted sites from file storage and temp folder</p>
</ion-label>
<ion-button (click)="clearFileStorage()" aria-label="Clear File Storage" fill="clear" slot="end">
<ion-icon slot="icon-only" aria-hidden="true" name="fas-broom"></ion-icon>
</ion-button>
</ion-item>
<ng-container *ngIf="siteId">
<ion-item-divider>
<ion-label>

View File

@ -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<void> {
const sites = await CoreSites.getSitesIds();
await CoreFile.clearDeletedSitesFolder(sites);
await CoreFile.clearTmpFolder();
CoreDomUtils.showToast('File storage cleared');
}
async setEnabledStagingSites(enabled: boolean): Promise<void> {
if (this.enableStagingSites === this.previousEnableStagingSites) {
return;

View File

@ -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<void> {
// 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<void>[] = 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.
*