From d3bb5caa83de1377fc23f5b9e8a818b15c75da88 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sun, 12 Jun 2022 20:20:08 +0800 Subject: [PATCH] :bug: Fix(db): fix some db bugs #873,#806 --- .vscode/launch.json | 48 +++++++------- .vscode/tasks.json | 27 ++++++++ package.json | 6 +- src/main/apis/app/shortKey/shortKeyHandler.ts | 2 +- src/main/apis/app/uploader/index.ts | 5 ++ src/main/apis/core/datastore/index.ts | 63 +++++++------------ src/main/apis/core/picgo/index.ts | 11 ++++ src/main/apis/gui/index.ts | 21 +++++++ src/main/events/picgoCoreIPC.ts | 31 ++++++--- src/main/lifeCycle/index.ts | 5 -- src/main/migrate/index.ts | 2 +- src/main/utils/getPicBeds.ts | 5 ++ src/main/utils/privacyManager.ts | 7 +-- src/renderer/pages/picbeds/AliYun.vue | 3 +- src/renderer/pages/picbeds/GitHub.vue | 3 +- src/renderer/pages/picbeds/Imgur.vue | 3 +- src/renderer/pages/picbeds/Others.vue | 3 +- src/renderer/pages/picbeds/Qiniu.vue | 3 +- src/renderer/pages/picbeds/SMMS.vue | 3 +- src/renderer/pages/picbeds/TcYun.vue | 3 +- src/renderer/pages/picbeds/UpYun.vue | 3 +- src/renderer/utils/common.ts | 8 +++ src/universal/i18n/en.ts | 5 +- src/universal/i18n/zh-CN.ts | 3 + src/universal/utils/common.ts | 7 +++ vue.config.js | 4 ++ yarn.lock | 61 ++++++++++++++---- 27 files changed, 238 insertions(+), 107 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 3f122b78..de810e08 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,38 +1,36 @@ { - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { - "name": "Launch", + "name": "Electron: Main", "type": "node", "request": "launch", - "program": "${workspaceRoot}/src/main/index.dev.js", - "env": { - "DEBUG_ENV": "debug" - }, - "stopOnEntry": false, - "args": [], - "cwd": "${workspaceRoot}", - // this points to the electron task runner + "protocol": "inspector", "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron", - "runtimeArgs": [ - "--nolazy" - ], - "sourceMaps": true + "windows": { + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd" + }, + "preLaunchTask": "electron-debug", + "args": ["--remote-debugging-port=9223", "./dist_electron"], + "outFiles": ["${workspaceFolder}/dist_electron/**/*.js"] }, { - "name": "Attach", - "type": "node", + "name": "Electron: Renderer", + "type": "chrome", "request": "attach", - "port": 5858, - "sourceMaps": true, - "restart": true, - "outFiles": [], - "localRoot": "${workspaceRoot}", - "protocol": "inspector", - "remoteRoot": null + "port": 9223, + "urlFilter": "http://localhost:*", + "timeout": 30000, + "webRoot": "${workspaceFolder}/src", + "sourceMapPathOverrides": { + "webpack:///./src/*": "${webRoot}/*" + } + } + ], + "compounds": [ + { + "name": "Electron: All", + "configurations": ["Electron: Main", "Electron: Renderer"] } ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..206ff823 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "electron-debug", + "type": "process", + "command": "./node_modules/.bin/vue-cli-service", + "windows": { + "command": "./node_modules/.bin/vue-cli-service.cmd" + }, + "isBackground": true, + "args": ["electron:serve", "--debug"], + "problemMatcher": { + "owner": "custom", + "pattern": { + "regexp": "" + }, + "background": { + "beginsPattern": "Starting development server\\.\\.\\.", + "endsPattern": "Not launching electron as debug argument was passed\\." + } + } + } + ] +} \ No newline at end of file diff --git a/package.json b/package.json index d8fa57c9..4e3bf6b0 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "bump": "bump-version", "release": "vue-cli-service electron:build --publish always" }, - "main": "background.js", + "main": "index.js", "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" @@ -36,7 +36,7 @@ }, "dependencies": { "@picgo/i18n": "^1.0.0", - "@picgo/store": "^1.0.3", + "@picgo/store": "2.0.1", "axios": "^0.19.0", "core-js": "^3.3.2", "element-ui": "^2.13.0", @@ -44,7 +44,7 @@ "keycode": "^2.2.0", "lodash-id": "^0.14.0", "lowdb": "^1.0.0", - "picgo": "^1.5.0-alpha.3", + "picgo": "^1.5.0-alpha.4", "qrcode.vue": "^1.7.0", "shell-path": "2.1.0", "uuidv4": "^6.2.11", diff --git a/src/main/apis/app/shortKey/shortKeyHandler.ts b/src/main/apis/app/shortKey/shortKeyHandler.ts index 4ba0ab7a..2dd7f64d 100644 --- a/src/main/apis/app/shortKey/shortKeyHandler.ts +++ b/src/main/apis/app/shortKey/shortKeyHandler.ts @@ -177,7 +177,7 @@ class ShortKeyHandler { keyList.forEach(item => { globalShortcut.unregister(item.key) shortKeyService.unregisterCommand(item.command) - picgo.unsetConfig('settings.shortKey', item.command) + db.unset('settings.shortKey', item.command) }) } } diff --git a/src/main/apis/app/uploader/index.ts b/src/main/apis/app/uploader/index.ts index fbf5bb2b..6f66e417 100644 --- a/src/main/apis/app/uploader/index.ts +++ b/src/main/apis/app/uploader/index.ts @@ -18,6 +18,7 @@ import logger from '@core/picgo/logger' import { T } from '~/universal/i18n' import fse from 'fs-extra' import path from 'path' +import { privacyManager } from '~/main/utils/privacyManager' const waitForShow = (webcontent: WebContents) => { return new Promise((resolve) => { @@ -140,6 +141,10 @@ class Uploader { async upload (img?: IUploadOption): Promise { try { + const privacyCheckRes = await privacyManager.check() + if (!privacyCheckRes) { + throw Error(T('PRIVACY_TIPS')) + } const startTime = Date.now() const output = await picgo.upload(img) if (Array.isArray(output) && output.some((item: ImgInfo) => item.imgUrl)) { diff --git a/src/main/apis/core/datastore/index.ts b/src/main/apis/core/datastore/index.ts index 3666d99d..003059ac 100644 --- a/src/main/apis/core/datastore/index.ts +++ b/src/main/apis/core/datastore/index.ts @@ -1,10 +1,6 @@ -import Datastore from 'lowdb' -// @ts-ignore -import LodashId from 'lodash-id' -import FileSync from 'lowdb/adapters/FileSync' import fs from 'fs-extra' import { dbPathChecker, dbPathDir, getGalleryDBPath } from './dbChecker' -import { DBStore } from '@picgo/store' +import { DBStore, JSONStore } from '@picgo/store' import { T } from '~/universal/i18n' const STORE_PATH = dbPathDir() @@ -15,68 +11,55 @@ if (!fs.pathExistsSync(STORE_PATH)) { const CONFIG_PATH: string = dbPathChecker() const DB_PATH: string = getGalleryDBPath().dbPath -// TODO: use JSONStore with @picgo/store class ConfigStore { - private db: Datastore.LowdbSync + private db: JSONStore constructor () { - const adapter = new FileSync(CONFIG_PATH) + this.db = new JSONStore(CONFIG_PATH) - this.db = Datastore(adapter) - this.db._.mixin(LodashId) - - if (!this.db.has('picBed').value()) { + if (!this.db.has('picBed')) { this.db.set('picBed', { current: 'smms', // deprecated uploader: 'smms', smms: { token: '' } - }).write() + }) } - if (!this.db.has('settings.shortKey').value()) { + if (!this.db.has('settings.shortKey')) { this.db.set('settings.shortKey[picgo:upload]', { enable: true, key: 'CommandOrControl+Shift+P', name: 'upload', label: T('QUICK_UPLOAD') - }).write() + }) } + this.read() } - read () { - return this.db.read() + read (flush = false) { + // if flush -> true, read origin file again + this.db.read(flush) + return this.db } - get (key = '') { - return this.read().get(key).value() + get (key = ''): any { + if (key === '') { + return this.db.read() + } + return this.db.get(key) } - set (key: string, value: any) { - return this.read().set(key, value).write() + set (key: string, value: any): void { + return this.db.set(key, value) } has (key: string) { - return this.read().has(key).value() - } - - insert (key: string, value: any): void { - // @ts-ignore - return this.read().get(key).insert(value).write() + return this.db.has(key) } unset (key: string, value: any): boolean { - return this.read().get(key).unset(value).value() - } - - getById (key: string, id: string) { - // @ts-ignore - return this.read().get(key).getById(id).value() - } - - removeById (key: string, id: string) { - // @ts-ignore - return this.read().get(key).removeById(id).write() + return this.db.unset(key, value) } getConfigPath () { @@ -84,7 +67,9 @@ class ConfigStore { } } -export default new ConfigStore() +const db = new ConfigStore() + +export default db // v2.3.0 add gallery db class GalleryDB { diff --git a/src/main/apis/core/picgo/index.ts b/src/main/apis/core/picgo/index.ts index 11cbe714..0112fc5b 100644 --- a/src/main/apis/core/picgo/index.ts +++ b/src/main/apis/core/picgo/index.ts @@ -1,6 +1,7 @@ import { dbChecker, dbPathChecker } from 'apis/core/datastore/dbChecker' import pkg from 'root/package.json' import { PicGo } from 'picgo' +import db from 'apis/core/datastore' const CONFIG_PATH = dbPathChecker() @@ -15,4 +16,14 @@ picgo.saveConfig({ global.PICGO_GUI_VERSION = pkg.version picgo.GUI_VERSION = global.PICGO_GUI_VERSION +const originPicGoSaveConfig = picgo.saveConfig.bind(picgo) + +picgo.saveConfig = (config: IStringKeyMap) => { + originPicGoSaveConfig(config) + // flush electron's db + setTimeout(() => { + db.read(true) + }, 0) +} + export default picgo diff --git a/src/main/apis/gui/index.ts b/src/main/apis/gui/index.ts index fb4227cc..65ec33e2 100644 --- a/src/main/apis/gui/index.ts +++ b/src/main/apis/gui/index.ts @@ -146,6 +146,27 @@ class GuiApi implements IGuiApi { get galleryDB (): DBStore { return new Proxy(GalleryDB.getInstance(), { get (target, prop: keyof DBStore) { + if (prop === 'overwrite') { + return new Proxy(GalleryDB.getInstance().overwrite, { + apply (target, ctx, args) { + return new Promise((resolve) => { + const guiApi = GuiApi.getInstance() + guiApi.showMessageBox({ + title: T('TIPS_WARNING'), + message: T('TIPS_PLUGIN_REMOVE_GALLERY_ITEM'), + type: 'info', + buttons: ['Yes', 'No'] + }).then(res => { + if (res.result === 0) { + resolve(Reflect.apply(target, ctx, args)) + } else { + resolve(undefined) + } + }) + }) + } + }) + } if (prop === 'removeById') { return new Proxy(GalleryDB.getInstance().removeById, { apply (target, ctx, args) { diff --git a/src/main/events/picgoCoreIPC.ts b/src/main/events/picgoCoreIPC.ts index 85888b90..2443948f 100644 --- a/src/main/events/picgoCoreIPC.ts +++ b/src/main/events/picgoCoreIPC.ts @@ -10,7 +10,7 @@ import { import { IPasteStyle, IPicGoHelperType, IWindowList } from '#/types/enum' import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler' import picgo from '@core/picgo' -import { handleStreamlinePluginName } from '~/universal/utils/common' +import { handleStreamlinePluginName, simpleClone } from '~/universal/utils/common' import { IGuiMenuItem, PicGo as PicGoCore } from 'picgo' import windowManager from 'apis/app/window/windowManager' import { showNotification } from '~/main/utils/common' @@ -128,10 +128,19 @@ const getPluginList = (): IPicGoPlugin[] => { const handleGetPluginList = () => { ipcMain.on('getPluginList', (event: IpcMainEvent) => { - const list = getPluginList() - // here can just send JS Object not function - // or will cause [Failed to serialize arguments] error - event.sender.send('pluginList', list) + try { + const list = simpleClone(getPluginList()) + // here can just send JS Object not function + // or will cause [Failed to serialize arguments] error + event.sender.send('pluginList', list) + } catch (e: any) { + event.sender.send('pluginList', []) + showNotification({ + title: T('TIPS_GET_PLUGIN_LIST_FAILED'), + body: e.message + }) + picgo.log.error(e) + } }) } @@ -267,8 +276,16 @@ const handleImportLocalPlugin = () => { if (filePaths.length > 0) { const res = await picgo.pluginHandler.install(filePaths) if (res.success) { - const list = getPluginList() - event.sender.send('pluginList', list) + try { + const list = simpleClone(getPluginList()) + event.sender.send('pluginList', list) + } catch (e: any) { + event.sender.send('pluginList', []) + showNotification({ + title: T('TIPS_GET_PLUGIN_LIST_FAILED'), + body: e.message + }) + } showNotification({ title: T('PLUGIN_IMPORT_SUCCEED'), body: '' diff --git a/src/main/lifeCycle/index.ts b/src/main/lifeCycle/index.ts index dcff7379..eff03e8b 100644 --- a/src/main/lifeCycle/index.ts +++ b/src/main/lifeCycle/index.ts @@ -31,7 +31,6 @@ import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler' import { getUploadFiles } from '~/main/utils/handleArgv' import db, { GalleryDB } from '~/main/apis/core/datastore' import bus from '@core/bus' -import { privacyManager } from '~/main/utils/privacyManager' import logger from 'apis/core/picgo/logger' import picgo from 'apis/core/picgo' import fixPath from './fixPath' @@ -81,10 +80,6 @@ class LifeCycle { console.error('Vue Devtools failed to install:', e.toString()) } } - const res = await privacyManager.init() - if (!res) { - return app.quit() - } windowManager.create(IWindowList.TRAY_WINDOW) windowManager.create(IWindowList.SETTING_WINDOW) createTray() diff --git a/src/main/migrate/index.ts b/src/main/migrate/index.ts index e2e32042..44e16c48 100644 --- a/src/main/migrate/index.ts +++ b/src/main/migrate/index.ts @@ -34,7 +34,7 @@ const updateShortKeyFromVersion212 = (db: typeof ConfigStore, shortKeyConfig: IS } const migrateGalleryFromVersion230 = async (configDB: typeof ConfigStore, galleryDB: DBStore, picgo: PicGoCore) => { - const originGallery: ImgInfo[] = configDB.get('uploaded') + const originGallery: ImgInfo[] = picgo.getConfig('uploaded') // if hasMigrate, we don't need to migrate const hasMigrate: boolean = configDB.get('__migrateUploaded') if (hasMigrate) { diff --git a/src/main/utils/getPicBeds.ts b/src/main/utils/getPicBeds.ts index f9a9113a..a5c744bd 100644 --- a/src/main/utils/getPicBeds.ts +++ b/src/main/utils/getPicBeds.ts @@ -10,6 +10,11 @@ const getPicBeds = () => { name: picgo.helper.uploader.get(item)!.name || item, visible: visible ? visible.visible : true } + }).sort((a) => { + if (a.type === 'tcyun') { + return -1 + } + return 0 }) as IPicBedType[] return picBeds } diff --git a/src/main/utils/privacyManager.ts b/src/main/utils/privacyManager.ts index 0d1c48f0..ccb57a11 100644 --- a/src/main/utils/privacyManager.ts +++ b/src/main/utils/privacyManager.ts @@ -1,14 +1,9 @@ import db from '~/main/apis/core/datastore' -import { ipcMain } from 'electron' import { showMessageBox } from '~/main/utils/common' -import { SHOW_PRIVACY_MESSAGE } from '~/universal/events/constants' import { T } from '~/universal/i18n' class PrivacyManager { - async init () { - ipcMain.on(SHOW_PRIVACY_MESSAGE, () => { - this.show(false) - }) + async check () { if (db.get('settings.privacyEnsure') !== true) { const res = await this.show(true) // cancel diff --git a/src/renderer/pages/picbeds/AliYun.vue b/src/renderer/pages/picbeds/AliYun.vue index 9af353ea..244ce418 100644 --- a/src/renderer/pages/picbeds/AliYun.vue +++ b/src/renderer/pages/picbeds/AliYun.vue @@ -72,6 +72,7 @@