Skip to content

Commit

Permalink
✨ Feature(config): auto configuration backup & fallback to avoid main…
Browse files Browse the repository at this point in the history
… process crash

ISSUES CLOSED: #568
  • Loading branch information
Molunerfinn committed Sep 30, 2020
1 parent eab014d commit 32b8b97
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
app,
globalShortcut,
protocol
protocol,
dialog,
Notification
} from 'electron'
import {
createProtocol,
Expand Down Expand Up @@ -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 () {
Expand Down
55 changes: 55 additions & 0 deletions src/universal/datastore/dbChecker.ts
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions src/universal/datastore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -15,6 +16,8 @@ if (process.type !== 'renderer') {
}
}

dbChecker()

class DB {
private db: Datastore.LowdbSync<Datastore.AdapterSync>
constructor () {
Expand Down
1 change: 1 addition & 0 deletions src/universal/types/electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare global {
interface Global {
PICGO_GUI_VERSION: string
PICGO_CORE_VERSION: string
notificationList: IAppNotification[]
}
}
}
6 changes: 6 additions & 0 deletions src/universal/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,9 @@ interface IUpYunConfig {
}

type ILoggerType = string | Error | boolean | number | undefined

interface IAppNotification {
title: string
body: string
icon?: string
}

0 comments on commit 32b8b97

Please sign in to comment.