Feature(config): auto configuration backup & fallback to avoid main process crash

ISSUES CLOSED: #568
This commit is contained in:
PiEgg 2020-09-30 17:32:35 +08:00
parent eab014dc88
commit 32b8b976be
5 changed files with 76 additions and 1 deletions

View File

@ -1,7 +1,9 @@
import {
app,
globalShortcut,
protocol
protocol,
dialog,
Notification
} from 'electron'
import {
createProtocol,
@ -75,6 +77,14 @@ class LifeCycle {
}
}
}
if (global.notificationList?.length > 0) {
while (global.notificationList.length) {
const option = global.notificationList.pop()
const notice = new Notification(option!)
notice.show()
}
}
})
}
private onRunning () {

View File

@ -0,0 +1,55 @@
import fs from 'fs-extra'
import path from 'path'
import { app } from 'electron'
import dayjs from 'dayjs'
const errorMsg = {
broken: 'PicGo 配置文件损坏,已经恢复为默认配置',
brokenButBackup: 'PicGo 配置文件损坏,已经恢复为备份配置'
}
function dbChecker () {
if (process.type !== 'renderer') {
if (!global.notificationList) global.notificationList = []
const STORE_PATH = app.getPath('userData')
const configFilePath = path.join(STORE_PATH, 'data.json')
const configFileBackupPath = path.join(STORE_PATH, 'data.bak.json')
if (!fs.existsSync(configFilePath)) {
return
}
let configFile: string = ''
let optionsTpl = {
title: '注意',
body: ''
}
try {
configFile = fs.readFileSync(configFilePath, { encoding: 'utf-8' })
JSON.parse(configFile)
} catch (e) {
fs.unlinkSync(configFilePath)
if (fs.existsSync(configFileBackupPath)) {
try {
configFile = fs.readFileSync(configFileBackupPath, { encoding: 'utf-8' })
JSON.parse(configFile)
fs.writeFileSync(configFilePath, configFile, { encoding: 'utf-8' })
const stats = fs.statSync(configFileBackupPath)
optionsTpl.body = `${errorMsg.brokenButBackup}\n备份文件版本${dayjs(stats.mtime).format('YYYY-MM-DD HH:mm:ss')}`
global.notificationList.push(optionsTpl)
return
} catch (e) {
optionsTpl.body = errorMsg.broken
global.notificationList.push(optionsTpl)
return
}
}
optionsTpl.body = errorMsg.broken
global.notificationList.push(optionsTpl)
return
}
fs.writeFileSync(configFileBackupPath, configFile, { encoding: 'utf-8' })
}
}
export {
dbChecker
}

View File

@ -5,6 +5,7 @@ import FileSync from 'lowdb/adapters/FileSync'
import path from 'path'
import fs from 'fs-extra'
import { remote, app } from 'electron'
import { dbChecker } from './dbChecker'
const APP = process.type === 'renderer' ? remote.app : app
const STORE_PATH = APP.getPath('userData')
@ -15,6 +16,8 @@ if (process.type !== 'renderer') {
}
}
dbChecker()
class DB {
private db: Datastore.LowdbSync<Datastore.AdapterSync>
constructor () {

View File

@ -27,6 +27,7 @@ declare global {
interface Global {
PICGO_GUI_VERSION: string
PICGO_CORE_VERSION: string
notificationList: IAppNotification[]
}
}
}

View File

@ -279,3 +279,9 @@ interface IUpYunConfig {
}
type ILoggerType = string | Error | boolean | number | undefined
interface IAppNotification {
title: string
body: string
icon?: string
}