Skip to content

Commit

Permalink
🐛 Fix: some bugs
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #722
  • Loading branch information
Molunerfinn committed Aug 21, 2021
1 parent 2d3e779 commit a676c08
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/main/apis/core/datastore/dbChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs-extra'
import path from 'path'
import { remote, app } from 'electron'
import dayjs from 'dayjs'
import { getLogger } from '@core/utils/localLogger'
const APP = process.type === 'renderer' ? remote.app : app
const STORE_PATH = APP.getPath('userData')
const configFilePath = path.join(STORE_PATH, 'data.json')
Expand All @@ -15,9 +16,11 @@ const errorMsg = {
brokenButBackup: 'PicGo 配置文件损坏,已经恢复为备份配置'
}

/** ensure notification list */
if (!global.notificationList) global.notificationList = []

function dbChecker () {
if (process.type !== 'renderer') {
if (!global.notificationList) global.notificationList = []
// db save bak
try {
const { dbPath, dbBackupPath } = getGalleryDBPath()
Expand Down Expand Up @@ -50,16 +53,16 @@ function dbChecker () {
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)
global.notificationList?.push(optionsTpl)
return
} catch (e) {
optionsTpl.body = errorMsg.broken
global.notificationList.push(optionsTpl)
global.notificationList?.push(optionsTpl)
return
}
}
optionsTpl.body = errorMsg.broken
global.notificationList.push(optionsTpl)
global.notificationList?.push(optionsTpl)
return
}
fs.writeFileSync(configFileBackupPath, configFile, { encoding: 'utf-8' })
Expand Down Expand Up @@ -92,15 +95,17 @@ function dbPathChecker (): string {
}
return _configFilePath
} catch (e) {
// TODO: local logger is needed
const picgoLogPath = path.join(defaultConfigPath, 'picgo.log')
const logger = getLogger(picgoLogPath)
if (!hasCheckPath) {
let optionsTpl = {
title: '注意',
body: '自定义文件解析出错,请检查路径内容是否正确'
}
global.notificationList.push(optionsTpl)
global.notificationList?.push(optionsTpl)
hasCheckPath = true
}
logger('error', e)
console.error(e)
_configFilePath = defaultConfigPath
return _configFilePath
Expand Down
32 changes: 32 additions & 0 deletions src/main/apis/core/utils/localLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'fs-extra'
import dayjs from 'dayjs'
import util from 'util'

/**
* for local log before picgo inited
*/
const getLogger = (logPath: string) => {
if (!fs.existsSync(logPath)) {
fs.ensureFileSync(logPath)
}
return (type: string, ...msg: any[]) => {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
msg.forEach((item: ILogArgvTypeWithError) => {
if (typeof item === 'object' && type === 'error') {
log += `\n------Error Stack Begin------\n${util.format(item.stack)}\n-------Error Stack End------- `
} else {
if (typeof item === 'object') {
item = JSON.stringify(item)
}
log += `${item} `
}
})
log += '\n'
// A synchronized approach to avoid log msg sequence errors
fs.appendFileSync(logPath, log)
}
}

export {
getLogger
}
4 changes: 2 additions & 2 deletions src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class LifeCycle {
handleStartUpFiles(process.argv, process.cwd())
}

if (global.notificationList?.length > 0) {
while (global.notificationList.length) {
if (global.notificationList && global.notificationList?.length > 0) {
while (global.notificationList?.length) {
const option = global.notificationList.pop()
const notice = new Notification(option!)
notice.show()
Expand Down
2 changes: 1 addition & 1 deletion src/main/migrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const migrateGalleryFromVersion230 = async (configDB: typeof ConfigStore, galler
const configPath = configDB.getConfigPath()
const configBakPath = path.join(path.dirname(configPath), 'config.bak.json')
// migrate gallery from config to gallery db
if (originGallery && originGallery?.length > 0) {
if (originGallery && Array.isArray(originGallery) && originGallery?.length > 0) {
if (fse.existsSync(configBakPath)) {
fse.copyFileSync(configPath, configBakPath)
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/ConfigForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class extends Vue {
async handleConfig (val: any) {
this.ruleForm = Object.assign({}, {})
const config = await this.getConfig<IPicGoPluginConfig>(this.getConfigType())
if (val.length > 0 && config) {
if (val.length > 0) {
this.configList = cloneDeep(val).map((item: any) => {
let defaultValue = item.default !== undefined
? item.default : item.type === 'checkbox'
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/layouts/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:default-active="defaultActive"
@select="handleSelect"
:unique-opened="true"
@open="handleGetPicPeds"
>
<el-menu-item index="upload">
<i class="el-icon-upload"></i>
Expand Down Expand Up @@ -192,8 +193,8 @@ export default class extends Vue {
created () {
this.os = process.platform
this.buildMenu()
ipcRenderer.send('getPicBeds')
ipcRenderer.on('getPicBeds', this.getPicBeds)
this.handleGetPicPeds()
}
@Watch('choosedPicBedForQRCode')
Expand All @@ -207,6 +208,10 @@ export default class extends Vue {
}
}
handleGetPicPeds = () => {
ipcRenderer.send('getPicBeds')
}
handleSelect (index: string) {
const type = index.match(/picbeds-/)
if (type === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/universal/types/electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare global {
interface Global {
PICGO_GUI_VERSION: string
PICGO_CORE_VERSION: string
notificationList: IAppNotification[]
notificationList?: IAppNotification[]
}
}
}
4 changes: 4 additions & 0 deletions src/universal/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ interface IAnalyticsData {
interface IStringKeyMap {
[propName: string]: any
}

type ILogArgvType = string | number

type ILogArgvTypeWithError = ILogArgvType | Error

0 comments on commit a676c08

Please sign in to comment.