This commit is contained in:
qier222 2023-09-04 12:51:56 +08:00
parent 32050e4553
commit 6aee8ae38e
No known key found for this signature in database
41 changed files with 523 additions and 415 deletions

View file

@ -13,6 +13,7 @@ import fastFolderSize from 'fast-folder-size'
import path from 'path'
import prettyBytes from 'pretty-bytes'
import { db, Tables } from './db'
import { promisify } from 'util'
log.info('[electron] ipcMain.ts')
@ -137,15 +138,15 @@ function initOtherIpcMain() {
/**
* API缓存
*/
on(IpcChannels.ClearAPICache, () => {
// db.truncate(Tables.Track)
// db.truncate(Tables.Album)
// db.truncate(Tables.Artist)
// db.truncate(Tables.Playlist)
// db.truncate(Tables.ArtistAlbum)
// db.truncate(Tables.AccountData)
// db.truncate(Tables.Audio)
// db.vacuum()
handle(IpcChannels.ClearAPICache, async () => {
db.truncate(Tables.Track)
db.truncate(Tables.Album)
db.truncate(Tables.Artist)
db.truncate(Tables.Playlist)
db.truncate(Tables.ArtistAlbum)
db.truncate(Tables.AccountData)
db.truncate(Tables.Audio)
db.vacuum()
})
/**
@ -170,6 +171,31 @@ function initOtherIpcMain() {
}
})
// 获取缓存位置
handle(IpcChannels.GetCachePath, async () => {
return path.join(app.getPath('userData'), './audio_cache')
})
/**
*
*/
handle(IpcChannels.GetAudioCacheSize, async () => {
const fastFolderSizeAsync = promisify(fastFolderSize)
const bytes = await fastFolderSizeAsync(path.join(app.getPath('userData'), './audio_cache'))
return prettyBytes(bytes ?? 0)
})
handle(IpcChannels.ClearAudioCache, async () => {
try {
const audioCachePath = path.join(app.getPath('userData'), './audio_cache')
fs.rmdirSync(audioCachePath, { recursive: true })
fs.mkdirSync(audioCachePath)
return true
} catch (e) {
return false
}
})
/**
*
*/
@ -178,17 +204,6 @@ function initOtherIpcMain() {
cache.set(CacheAPIs.CoverColor, { id, color })
})
/**
*
*/
on(IpcChannels.GetAudioCacheSize, event => {
fastFolderSize(path.join(app.getPath('userData'), './audio_cache'), (error, bytes) => {
if (error) throw error
event.returnValue = prettyBytes(bytes ?? 0)
})
})
/**
* Apple Music获取专辑信息
*/
@ -235,30 +250,26 @@ function initOtherIpcMain() {
* tables到json文件便table大小dev环境
*/
if (process.env.NODE_ENV === 'development') {
// on(IpcChannels.DevDbExportJson, () => {
// const tables = [
// Tables.ArtistAlbum,
// Tables.Playlist,
// Tables.Album,
// Tables.Track,
// Tables.Artist,
// Tables.Audio,
// Tables.AccountData,
// Tables.Lyric,
// ]
// tables.forEach(table => {
// const data = db.findAll(table)
// fs.writeFile(
// `./tmp/${table}.json`,
// JSON.stringify(data),
// function (err) {
// if (err) {
// return console.log(err)
// }
// console.log('The file was saved!')
// }
// )
// })
// })
on(IpcChannels.DevDbExportJson, () => {
const tables = [
Tables.ArtistAlbum,
Tables.Playlist,
Tables.Album,
Tables.Track,
Tables.Artist,
Tables.Audio,
Tables.AccountData,
Tables.Lyrics,
]
tables.forEach(table => {
const data = db.findAll(table)
fs.writeFile(`./tmp/${table}.json`, JSON.stringify(data), function (err) {
if (err) {
return console.log(err)
}
console.log('The file was saved!')
})
})
})
}
}