2022-04-09 00:19:58 +08:00
|
|
|
|
import { BrowserWindow, ipcMain, app } from 'electron'
|
2022-03-30 16:54:11 +08:00
|
|
|
|
import { db, Tables } from './db'
|
2022-04-09 00:28:37 +08:00
|
|
|
|
import { IpcChannels } from './IpcChannelsName'
|
2022-04-12 01:48:14 +08:00
|
|
|
|
import cache from './cache'
|
2022-04-09 00:28:37 +08:00
|
|
|
|
import logger from './logger'
|
|
|
|
|
import fs from 'fs'
|
2022-04-12 01:48:14 +08:00
|
|
|
|
import { APIs } from './CacheAPIsName'
|
2022-03-30 16:54:11 +08:00
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 处理需要win对象的事件
|
|
|
|
|
* @param {BrowserWindow} win
|
|
|
|
|
*/
|
|
|
|
|
export function initIpcMain(win: BrowserWindow | null) {
|
|
|
|
|
ipcMain.on(IpcChannels.Minimize, () => {
|
|
|
|
|
win?.minimize()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ipcMain.on(IpcChannels.MaximizeOrUnmaximize, () => {
|
|
|
|
|
if (!win) return
|
|
|
|
|
win.isMaximized() ? win.unmaximize() : win.maximize()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ipcMain.on(IpcChannels.Close, () => {
|
|
|
|
|
app.exit()
|
|
|
|
|
})
|
2022-03-30 16:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 清除API缓存
|
|
|
|
|
*/
|
|
|
|
|
ipcMain.on(IpcChannels.ClearAPICache, () => {
|
2022-04-12 01:48:14 +08:00
|
|
|
|
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)
|
2022-03-30 16:54:11 +08:00
|
|
|
|
db.vacuum()
|
|
|
|
|
})
|
2022-04-09 00:19:58 +08:00
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* Get API cache
|
|
|
|
|
*/
|
|
|
|
|
ipcMain.on(IpcChannels.GetApiCacheSync, (event, args) => {
|
|
|
|
|
const { api, query } = args
|
2022-04-12 01:48:14 +08:00
|
|
|
|
const data = cache.get(api, query)
|
2022-04-09 00:28:37 +08:00
|
|
|
|
event.returnValue = data
|
|
|
|
|
})
|
2022-04-09 00:19:58 +08:00
|
|
|
|
|
2022-04-12 01:48:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 缓存封面颜色
|
|
|
|
|
*/
|
|
|
|
|
ipcMain.on(IpcChannels.CacheCoverColor, (event, args) => {
|
|
|
|
|
const { id, color } = args.query
|
|
|
|
|
cache.set(APIs.CoverColor, { id, color })
|
|
|
|
|
})
|
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 导出tables到json文件,方便查看table大小(dev环境)
|
|
|
|
|
*/
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
ipcMain.on(IpcChannels.DevDbExportJson, () => {
|
|
|
|
|
const tables = [
|
2022-04-12 01:48:14 +08:00
|
|
|
|
Tables.ArtistAlbum,
|
|
|
|
|
Tables.Playlist,
|
|
|
|
|
Tables.Album,
|
|
|
|
|
Tables.Track,
|
|
|
|
|
Tables.Artist,
|
|
|
|
|
Tables.Audio,
|
|
|
|
|
Tables.AccountData,
|
|
|
|
|
Tables.Lyric,
|
2022-04-09 00:28:37 +08:00
|
|
|
|
]
|
|
|
|
|
tables.forEach(table => {
|
|
|
|
|
const data = db.findAll(table)
|
2022-04-09 00:19:58 +08:00
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
fs.writeFile(`./tmp/${table}.json`, JSON.stringify(data), function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return console.log(err)
|
|
|
|
|
}
|
|
|
|
|
console.log('The file was saved!')
|
|
|
|
|
})
|
|
|
|
|
})
|
2022-04-09 00:19:58 +08:00
|
|
|
|
})
|
|
|
|
|
}
|