Clear cache now clears temp directory

This commit is contained in:
Geomitron
2021-04-10 14:29:00 -05:00
parent 8f670c5adc
commit 0ddbd00f07
4 changed files with 53 additions and 3 deletions

View File

@@ -24,8 +24,6 @@ export class SettingsComponent implements OnInit, AfterViewInit {
constructor(public settingsService: SettingsService, private electronService: ElectronService, private ref: ChangeDetectorRef) { } constructor(public settingsService: SettingsService, private electronService: ElectronService, private ref: ChangeDetectorRef) { }
async ngOnInit() { async ngOnInit() {
const cacheSize = await this.settingsService.getCacheSize()
this.cacheSize = Math.round(cacheSize / 1000000) + ' MB'
this.electronService.receiveIPC('update-available', (result) => { this.electronService.receiveIPC('update-available', (result) => {
this.updateAvailable = result != null this.updateAvailable = result != null
this.updateRetrying = false this.updateRetrying = false
@@ -52,6 +50,9 @@ export class SettingsComponent implements OnInit, AfterViewInit {
this.loginAvailable = !isAuthenticated this.loginAvailable = !isAuthenticated
this.ref.detectChanges() this.ref.detectChanges()
}) })
const cacheSize = await this.settingsService.getCacheSize()
this.cacheSize = Math.round(cacheSize / 1000000) + ' MB'
} }
ngAfterViewInit() { ngAfterViewInit() {

View File

@@ -49,7 +49,8 @@ export class SettingsService {
async clearCache() { async clearCache() {
this.saveSettings() this.saveSettings()
return this.electronService.defaultSession.clearCache() await this.electronService.defaultSession.clearCache()
await this.electronService.invoke('clear-cache', undefined)
} }
// Individual getters/setters // Individual getters/setters

View File

@@ -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()

View File

@@ -8,6 +8,7 @@ import { downloadHandler } from '../ipc/download/DownloadHandler'
import { Settings } from './Settings' import { Settings } from './Settings'
import { batchSongDetailsHandler } from '../ipc/browse/BatchSongDetailsHandler.ipc' import { batchSongDetailsHandler } from '../ipc/browse/BatchSongDetailsHandler.ipc'
import { getSettingsHandler, setSettingsHandler } from '../ipc/SettingsHandler.ipc' import { getSettingsHandler, setSettingsHandler } from '../ipc/SettingsHandler.ipc'
import { clearCacheHandler } from '../ipc/CacheHandler.ipc'
import { googleLoginHandler, getAuthStatusHandler, googleLogoutHandler } from '../ipc/google/GoogleLoginHandler.ipc' import { googleLoginHandler, getAuthStatusHandler, googleLogoutHandler } from '../ipc/google/GoogleLoginHandler.ipc'
import { updateChecker, UpdateProgress, getCurrentVersionHandler, downloadUpdateHandler, quitAndInstallHandler, getUpdateAvailableHandler } from '../ipc/UpdateHandler.ipc' import { updateChecker, UpdateProgress, getCurrentVersionHandler, downloadUpdateHandler, quitAndInstallHandler, getUpdateAvailableHandler } from '../ipc/UpdateHandler.ipc'
import { UpdateInfo } from 'electron-updater' import { UpdateInfo } from 'electron-updater'
@@ -24,6 +25,7 @@ import { openURLHandler } from '../ipc/OpenURLHandler.ipc'
export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[] { export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[] {
return [ return [
getSettingsHandler, getSettingsHandler,
clearCacheHandler,
searchHandler, searchHandler,
songDetailsHandler, songDetailsHandler,
batchSongDetailsHandler, batchSongDetailsHandler,
@@ -44,6 +46,10 @@ export type IPCInvokeEvents = {
input: undefined input: undefined
output: Settings output: Settings
} }
'clear-cache': {
input: undefined
output: void
}
'song-search': { 'song-search': {
input: SongSearch input: SongSearch
output: SongResult[] output: SongResult[]