-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feature(config): auto configuration backup & fallback to avoid main…
… process crash ISSUES CLOSED: #568
- Loading branch information
1 parent
eab014d
commit 32b8b97
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters