From c09e7cbef8f5d505e3f5c2ae9f9cb58fc36e52a7 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sun, 26 Sep 2021 08:28:15 -0700 Subject: [PATCH] Add clear cache button --- .../settings/ConfigPanels/CacheConfig.tsx | 90 ++++++++++++++++++- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/src/components/settings/ConfigPanels/CacheConfig.tsx b/src/components/settings/ConfigPanels/CacheConfig.tsx index 75d83c1..a329e13 100644 --- a/src/components/settings/ConfigPanels/CacheConfig.tsx +++ b/src/components/settings/ConfigPanels/CacheConfig.tsx @@ -2,10 +2,11 @@ import React, { useState, useEffect } from 'react'; import settings from 'electron-settings'; import fs from 'fs'; import path from 'path'; -import { InputGroup, Button, Tag, Message, Icon } from 'rsuite'; +import { InputGroup, Button, Tag, Message, Icon, ButtonToolbar, Whisper, Popover } from 'rsuite'; import { ConfigPanel } from '../styled'; import { StyledInput, StyledCheckbox } from '../../shared/styled'; import { getSongCachePath, getImageCachePath } from '../../../shared/utils'; +import { notifyToast } from '../../shared/toast'; const fsUtils = require('nodejs-fs-utils'); @@ -32,6 +33,57 @@ const CacheConfig = () => { } }, []); + const handleClearSongCache = () => { + const songCachePath = getSongCachePath(); + fs.readdir(songCachePath, (err, files) => { + if (err) { + return notifyToast('error', `Unable to scan directory: ${err}`); + } + + return files.forEach((file) => { + const songPath = path.join(songCachePath, file); + + // Simple validation + if (path.extname(songPath) === '.mp3') { + fs.unlink(songPath, (error) => { + if (err) { + return notifyToast('error', `Unable to clear cache item: ${error}`); + } + return null; + }); + } + }); + }); + }; + + const handleClearImageCache = (type: 'playlist' | 'album') => { + const imageCachePath = getImageCachePath(); + fs.readdir(imageCachePath, (err, files) => { + if (err) { + return notifyToast('error', `Unable to scan directory: ${err}`); + } + + const selectedFiles = + type === 'playlist' + ? files.filter((file) => file.split('_')[0] === 'playlist') + : files.filter((file) => file.split('_')[0] === 'album'); + + return selectedFiles.forEach((file) => { + const imagePath = path.join(imageCachePath, file); + + // Simple validation + if (path.extname(imagePath) === '.jpg') { + fs.unlink(imagePath, (error) => { + if (err) { + return notifyToast('error', `Unable to clear cache item: ${error}`); + } + return null; + }); + } + }); + }); + }; + return ( {errorMessage !== '' && ( @@ -117,9 +169,41 @@ const CacheConfig = () => { {imgCacheSize} MB {imgCacheSize === 9999999 && '- Folder not found'} -
- +
+ + + + Which cache would you like to clear? + + + + + + + } + > + + +
); };