From 0ddbd00f07e46e1507f3a2db008b745aeeaa909d Mon Sep 17 00:00:00 2001 From: Geomitron <22552797+Geomitron@users.noreply.github.com> Date: Sat, 10 Apr 2021 14:29:00 -0500 Subject: [PATCH] Clear cache now clears temp directory --- .../components/settings/settings.component.ts | 5 ++- src/app/core/services/settings.service.ts | 3 +- src/electron/ipc/CacheHandler.ipc.ts | 42 +++++++++++++++++++ src/electron/shared/IPCHandler.ts | 6 +++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/electron/ipc/CacheHandler.ipc.ts diff --git a/src/app/components/settings/settings.component.ts b/src/app/components/settings/settings.component.ts index 0572512..d67771f 100644 --- a/src/app/components/settings/settings.component.ts +++ b/src/app/components/settings/settings.component.ts @@ -24,8 +24,6 @@ export class SettingsComponent implements OnInit, AfterViewInit { constructor(public settingsService: SettingsService, private electronService: ElectronService, private ref: ChangeDetectorRef) { } async ngOnInit() { - const cacheSize = await this.settingsService.getCacheSize() - this.cacheSize = Math.round(cacheSize / 1000000) + ' MB' this.electronService.receiveIPC('update-available', (result) => { this.updateAvailable = result != null this.updateRetrying = false @@ -52,6 +50,9 @@ export class SettingsComponent implements OnInit, AfterViewInit { this.loginAvailable = !isAuthenticated this.ref.detectChanges() }) + + const cacheSize = await this.settingsService.getCacheSize() + this.cacheSize = Math.round(cacheSize / 1000000) + ' MB' } ngAfterViewInit() { diff --git a/src/app/core/services/settings.service.ts b/src/app/core/services/settings.service.ts index 2e6c2df..da4bd68 100644 --- a/src/app/core/services/settings.service.ts +++ b/src/app/core/services/settings.service.ts @@ -49,7 +49,8 @@ export class SettingsService { async clearCache() { this.saveSettings() - return this.electronService.defaultSession.clearCache() + await this.electronService.defaultSession.clearCache() + await this.electronService.invoke('clear-cache', undefined) } // Individual getters/setters diff --git a/src/electron/ipc/CacheHandler.ipc.ts b/src/electron/ipc/CacheHandler.ipc.ts new file mode 100644 index 0000000..6cad7f6 --- /dev/null +++ b/src/electron/ipc/CacheHandler.ipc.ts @@ -0,0 +1,42 @@ +import { IPCInvokeHandler } from '../shared/IPCHandler' +import { tempPath } from '../shared/Paths' +import * as _rimraf from 'rimraf' +import { Dirent, readdir as _readdir } from 'fs' +import { promisify } from 'util' +import { join } from 'path' +import { devLog } from '../shared/ElectronUtilFunctions' + +const readdir = promisify(_readdir) +const rimraf = promisify(_rimraf) + +/** + * Handles the 'clear-cache' event. + */ +class ClearCacheHandler implements IPCInvokeHandler<'clear-cache'> { + event: 'clear-cache' = 'clear-cache' + + /** + * Deletes all the files under `tempPath` + */ + async handler() { + let files: Dirent[] + try { + files = await readdir(tempPath, { withFileTypes: true }) + } catch (err) { + devLog('Failed to read cache: ', err) + return + } + + for (const file of files) { + try { + devLog(`Deleting ${file.isFile() ? 'file' : 'folder'}: ${join(tempPath, file.name)}`) + await rimraf(join(tempPath, file.name)) + } catch (err) { + devLog(`Failed to delete ${file.isFile() ? 'file' : 'folder'}: `, err) + return + } + } + } +} + +export const clearCacheHandler = new ClearCacheHandler() \ No newline at end of file diff --git a/src/electron/shared/IPCHandler.ts b/src/electron/shared/IPCHandler.ts index 6f63881..913cc3f 100644 --- a/src/electron/shared/IPCHandler.ts +++ b/src/electron/shared/IPCHandler.ts @@ -8,6 +8,7 @@ import { downloadHandler } from '../ipc/download/DownloadHandler' import { Settings } from './Settings' import { batchSongDetailsHandler } from '../ipc/browse/BatchSongDetailsHandler.ipc' import { getSettingsHandler, setSettingsHandler } from '../ipc/SettingsHandler.ipc' +import { clearCacheHandler } from '../ipc/CacheHandler.ipc' import { googleLoginHandler, getAuthStatusHandler, googleLogoutHandler } from '../ipc/google/GoogleLoginHandler.ipc' import { updateChecker, UpdateProgress, getCurrentVersionHandler, downloadUpdateHandler, quitAndInstallHandler, getUpdateAvailableHandler } from '../ipc/UpdateHandler.ipc' import { UpdateInfo } from 'electron-updater' @@ -24,6 +25,7 @@ import { openURLHandler } from '../ipc/OpenURLHandler.ipc' export function getIPCInvokeHandlers(): IPCInvokeHandler[] { return [ getSettingsHandler, + clearCacheHandler, searchHandler, songDetailsHandler, batchSongDetailsHandler, @@ -44,6 +46,10 @@ export type IPCInvokeEvents = { input: undefined output: Settings } + 'clear-cache': { + input: undefined + output: void + } 'song-search': { input: SongSearch output: SongResult[]