From f1eb7f4d70cc06e8714e25a72ea769f6354613ac Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sun, 1 Aug 2021 14:50:25 +0800 Subject: [PATCH] :bug: Fix: gallery db bug --- package.json | 4 +- public/wsl.sh | 18 +++++++++ src/main/apis/app/system/index.ts | 2 +- src/main/apis/app/uploader/apis.ts | 4 +- src/main/apis/core/datastore/dbChecker.ts | 2 + src/main/apis/gui/index.ts | 46 ++++++++++++++++++++++- src/main/events/ipcList.ts | 2 +- src/main/events/picgoCoreIPC.ts | 8 ++-- src/main/lifeCycle/index.ts | 7 ++-- src/main/migrate/index.ts | 5 ++- src/main/server/index.ts | 1 + src/main/utils/beforeOpen.ts | 24 +++++++----- src/renderer/pages/Gallery.vue | 23 +++++++----- src/renderer/pages/TrayPage.vue | 9 +++-- src/renderer/utils/db.ts | 6 +-- src/universal/types/extra-vue.d.ts | 4 +- src/universal/utils/pasteTemplate.ts | 7 ++-- yarn.lock | 29 ++++++++++---- 18 files changed, 145 insertions(+), 56 deletions(-) create mode 100644 public/wsl.sh diff --git a/package.json b/package.json index d1d5ef63..593cae01 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ ] }, "dependencies": { - "@picgo/store": "^1.0.0-alpha.3", + "@picgo/store": "^1.0.3", "axios": "^0.19.0", "core-js": "^3.3.2", "element-ui": "^2.13.0", @@ -43,7 +43,7 @@ "keycode": "^2.2.0", "lodash-id": "^0.14.0", "lowdb": "^1.0.0", - "picgo": "^1.4.21", + "picgo": "^1.4.23", "qrcode.vue": "^1.7.0", "uuidv4": "^6.2.11", "vue": "^2.6.10", diff --git a/public/wsl.sh b/public/wsl.sh new file mode 100644 index 00000000..7adb05de --- /dev/null +++ b/public/wsl.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# grab the paths +scriptPath=$(echo $0 | awk '{ print substr( $0, 1, length($0)-6 ) }')"windows10.ps1" +imagePath=$(echo $1 | awk '{ print substr( $0, 1, length($0)-18 ) }') +imageName=$(echo $1 | awk '{ print substr( $0, length($0)-17, length($0) ) }') + +# run the powershell script +res=$(powershell.exe -noprofile -noninteractive -nologo -sta -executionpolicy unrestricted -file $(wslpath -w $scriptPath) $(wslpath -w $imagePath)"\\"$imageName) + +# note that there is a return symbol in powershell result +noImage=$(echo "no image\r") + +# check whether image exists +if [ "$res" = "$noImage" ] ;then + echo "no image" +else + echo $(wslpath -u $res) +fi diff --git a/src/main/apis/app/system/index.ts b/src/main/apis/app/system/index.ts index 449732e1..099be6b0 100644 --- a/src/main/apis/app/system/index.ts +++ b/src/main/apis/app/system/index.ts @@ -212,7 +212,7 @@ export function createTray () { if (imgs !== false) { const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { - pasteText.push(pasteTemplate(pasteStyle, imgs[i])) + pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink'))) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl!, diff --git a/src/main/apis/app/uploader/apis.ts b/src/main/apis/app/uploader/apis.ts index 228d246d..83acdcf0 100644 --- a/src/main/apis/app/uploader/apis.ts +++ b/src/main/apis/app/uploader/apis.ts @@ -16,7 +16,7 @@ export const uploadClipboardFiles = async (): Promise => { if (img.length > 0) { const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW) const pasteStyle = db.get('settings.pasteStyle') || 'markdown' - handleCopyUrl(pasteTemplate(pasteStyle, img[0])) + handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink'))) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl!, @@ -52,7 +52,7 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW const pasteStyle = db.get('settings.pasteStyle') || 'markdown' const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { - pasteText.push(pasteTemplate(pasteStyle, imgs[i])) + pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink'))) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl!, diff --git a/src/main/apis/core/datastore/dbChecker.ts b/src/main/apis/core/datastore/dbChecker.ts index d94d633e..d6bd89c3 100644 --- a/src/main/apis/core/datastore/dbChecker.ts +++ b/src/main/apis/core/datastore/dbChecker.ts @@ -81,6 +81,8 @@ function dbPathChecker (): string { return defaultConfigPath } +export const defaultConfigPath = configFilePath + export { dbChecker, dbPathChecker diff --git a/src/main/apis/gui/index.ts b/src/main/apis/gui/index.ts index dd59ab58..c32e06bf 100644 --- a/src/main/apis/gui/index.ts +++ b/src/main/apis/gui/index.ts @@ -4,7 +4,9 @@ import { Notification, ipcMain } from 'electron' -import db, { GalleryDB } from '~/main/apis/core/datastore' +import path from 'path' +import db, { GalleryDB } from 'apis/core/datastore' +import { dbPathChecker, defaultConfigPath } from 'apis/core/datastore/dbChecker' import uploader from 'apis/app/uploader' import pasteTemplate from '#/utils/pasteTemplate' import { handleCopyUrl } from '~/main/utils/common' @@ -15,6 +17,8 @@ import { import { SHOW_INPUT_BOX } from '~/universal/events/constants' +import { DBStore } from '@picgo/store' +type PromiseResType = T extends Promise ? R : T // Cross-process support may be required in the future class GuiApi implements IGuiApi { @@ -79,7 +83,7 @@ class GuiApi implements IGuiApi { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { - pasteText.push(pasteTemplate(pasteStyle, imgs[i])) + pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink'))) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl as string, @@ -128,6 +132,44 @@ class GuiApi implements IGuiApi { }) }) } + + /** + * get picgo config/data path + */ + async getConfigPath () { + const currentConfigPath = dbPathChecker() + const galleryDBPath = path.join(path.dirname(currentConfigPath), 'picgo.db') + return { + defaultConfigPath, + currentConfigPath, + galleryDBPath + } + } + + get galleryDB (): DBStore { + return new Proxy(GalleryDB.getInstance(), { + get (target, prop: keyof DBStore) { + if (prop === 'removeById') { + return new Promise((resolve) => { + const guiApi = GuiApi.getInstance() + guiApi.showMessageBox({ + title: '警告', + message: '有插件正在试图删除一些相册文件,是否继续', + type: 'info', + buttons: ['Yes', 'No'] + }).then(res => { + if (res.result === 0) { + resolve(Reflect.get(target, prop)) + } else { + resolve(() => {}) + } + }) + }) + } + return Reflect.get(target, prop) + } + }) + } } export default GuiApi diff --git a/src/main/events/ipcList.ts b/src/main/events/ipcList.ts index ebee889d..8deea573 100644 --- a/src/main/events/ipcList.ts +++ b/src/main/events/ipcList.ts @@ -32,7 +32,7 @@ export default { const img = await uploader.setWebContents(trayWindow.webContents).upload() if (img !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' - handleCopyUrl(pasteTemplate(pasteStyle, img[0])) + handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink'))) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl!, diff --git a/src/main/events/picgoCoreIPC.ts b/src/main/events/picgoCoreIPC.ts index c1189062..43607c07 100644 --- a/src/main/events/picgoCoreIPC.ts +++ b/src/main/events/picgoCoreIPC.ts @@ -28,7 +28,7 @@ import { } from '#/events/constants' import { GalleryDB } from 'apis/core/datastore' -import { IObject } from '@picgo/store/dist/types' +import { IObject, IFilter } from '@picgo/store/dist/types' // eslint-disable-next-line const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require @@ -283,10 +283,10 @@ const handleImportLocalPlugin = () => { } const handlePicGoGalleryDB = () => { - ipcMain.on(PICGO_GET_DB, async (event: IpcMainEvent, callbackId: string) => { + ipcMain.on(PICGO_GET_DB, async (event: IpcMainEvent, filter: IFilter, callbackId: string) => { const dbStore = GalleryDB.getInstance() - const res = await dbStore.get() - event.sender.send(PICGO_GET_CONFIG, res, callbackId) + const res = await dbStore.get(filter) + event.sender.send(PICGO_GET_DB, res, callbackId) }) ipcMain.on(PICGO_INSERT_DB, async (event: IpcMainEvent, value: IObject, callbackId: string) => { diff --git a/src/main/lifeCycle/index.ts b/src/main/lifeCycle/index.ts index 4fa488ed..f7e36076 100644 --- a/src/main/lifeCycle/index.ts +++ b/src/main/lifeCycle/index.ts @@ -55,7 +55,7 @@ const handleStartUpFiles = (argv: string[], cwd: string) => { } class LifeCycle { - private async beforeReady () { + private beforeReady () { protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }]) // fix the $PATH in macOS fixPath() @@ -63,10 +63,11 @@ class LifeCycle { ipcList.listen() busEventList.listen() updateShortKeyFromVersion212(db, db.get('settings.shortKey')) - await migrateGalleryFromVersion230(db, GalleryDB.getInstance()) } private onReady () { app.on('ready', async () => { + console.log('on ready') + await migrateGalleryFromVersion230(db, GalleryDB.getInstance()) createProtocol('picgo') if (isDevelopment && !process.env.IS_TEST) { // Install Vue Devtools @@ -169,7 +170,7 @@ class LifeCycle { if (!gotTheLock) { app.quit() } else { - await this.beforeReady() + this.beforeReady() this.onReady() this.onRunning() this.onQuit() diff --git a/src/main/migrate/index.ts b/src/main/migrate/index.ts index dc8ee084..05bec2ab 100644 --- a/src/main/migrate/index.ts +++ b/src/main/migrate/index.ts @@ -38,10 +38,11 @@ const migrateGalleryFromVersion230 = async (configDB: typeof ConfigStore, galler if (fse.existsSync(configBakPath)) { return } - fse.copyFileSync(configPath, configBakPath) // migrate gallery from config to gallery db - if (originGallery && originGallery.length > 0) { + if (originGallery && originGallery?.length > 0) { + fse.copyFileSync(configPath, configBakPath) await galleryDB.insertMany(originGallery) + configDB.set('uploaded', []) } } diff --git a/src/main/server/index.ts b/src/main/server/index.ts index 11611504..daec1f24 100644 --- a/src/main/server/index.ts +++ b/src/main/server/index.ts @@ -103,6 +103,7 @@ class Server { }) } startup () { + console.log('startup', this.config.enable) if (this.config.enable) { this.listen(this.config.port) } diff --git a/src/main/utils/beforeOpen.ts b/src/main/utils/beforeOpen.ts index 6f30b347..db84d32e 100644 --- a/src/main/utils/beforeOpen.ts +++ b/src/main/utils/beforeOpen.ts @@ -1,11 +1,11 @@ import fs from 'fs-extra' import path from 'path' import os from 'os' -import { remote, app } from 'electron' import pkg from 'root/package.json' +import { dbPathChecker } from 'apis/core/datastore/dbChecker' -const APP = process.type === 'renderer' ? remote.app : app -const STORE_PATH = APP.getPath('userData') +const configPath = dbPathChecker() +const CONFIG_DIR = path.dirname(configPath) function injectPicGoVersion () { global.PICGO_GUI_VERSION = pkg.version @@ -41,7 +41,7 @@ function resolveMacWorkFlow () { */ function resolveClipboardImageGenerator () { let clipboardFiles = getClipboardFiles() - if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows10.ps1'))) { + if (!fs.pathExistsSync(path.join(CONFIG_DIR, 'windows10.ps1'))) { clipboardFiles.forEach(item => { fs.copyFileSync(item.origin, item.dest) }) @@ -52,10 +52,15 @@ function resolveClipboardImageGenerator () { } function diffFilesAndUpdate (filePath1: string, filePath2: string) { - let file1 = fs.readFileSync(filePath1) - let file2 = fs.readFileSync(filePath2) + try { + let file1 = fs.existsSync(filePath1) && fs.readFileSync(filePath1) + let file2 = fs.existsSync(filePath1) && fs.readFileSync(filePath2) - if (!file1.equals(file2)) { + if (!file1 || !file2 || !file1.equals(file2)) { + fs.copyFileSync(filePath1, filePath2) + } + } catch (e) { + console.error(e) fs.copyFileSync(filePath1, filePath2) } } @@ -65,13 +70,14 @@ function resolveClipboardImageGenerator () { '/linux.sh', '/mac.applescript', '/windows.ps1', - '/windows10.ps1' + '/windows10.ps1', + '/wsl.sh' ] return files.map(item => { return { origin: path.join(__static, item), - dest: path.join(STORE_PATH, item) + dest: path.join(CONFIG_DIR, item) } }) } diff --git a/src/renderer/pages/Gallery.vue b/src/renderer/pages/Gallery.vue index 378eb82e..95ecbe87 100644 --- a/src/renderer/pages/Gallery.vue +++ b/src/renderer/pages/Gallery.vue @@ -71,12 +71,12 @@ - +