Skip to content

Commit

Permalink
🐛 Fix: gallery db bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Aug 1, 2021
1 parent 6ddd660 commit f1eb7f4
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 56 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions public/wsl.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/main/apis/app/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
4 changes: 2 additions & 2 deletions src/main/apis/app/uploader/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const uploadClipboardFiles = async (): Promise<string> => {
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!,
Expand Down Expand Up @@ -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!,
Expand Down
2 changes: 2 additions & 0 deletions src/main/apis/core/datastore/dbChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function dbPathChecker (): string {
return defaultConfigPath
}

export const defaultConfigPath = configFilePath

export {
dbChecker,
dbPathChecker
Expand Down
46 changes: 44 additions & 2 deletions src/main/apis/gui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -15,6 +17,8 @@ import {
import {
SHOW_INPUT_BOX
} from '~/universal/events/constants'
import { DBStore } from '@picgo/store'
type PromiseResType<T> = T extends Promise<infer R> ? R : T

// Cross-process support may be required in the future
class GuiApi implements IGuiApi {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<DBStore>(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
2 changes: 1 addition & 1 deletion src/main/events/ipcList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
8 changes: 4 additions & 4 deletions src/main/events/picgoCoreIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ 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()
beforeOpen()
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
Expand Down Expand Up @@ -169,7 +170,7 @@ class LifeCycle {
if (!gotTheLock) {
app.quit()
} else {
await this.beforeReady()
this.beforeReady()
this.onReady()
this.onRunning()
this.onQuit()
Expand Down
5 changes: 3 additions & 2 deletions src/main/migrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Server {
})
}
startup () {
console.log('startup', this.config.enable)
if (this.config.enable) {
this.listen(this.config.port)
}
Expand Down
24 changes: 15 additions & 9 deletions src/main/utils/beforeOpen.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
}
}
Expand All @@ -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)
}
})
}
Expand Down
23 changes: 14 additions & 9 deletions src/renderer/pages/Gallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
<el-col :span="20" :offset="2">
<el-row :gutter="16">
<gallerys
:images="images"
:images="filterList"
:index="idx"
@close="handleClose"
:options="options"
></gallerys>
<el-col :span="6" v-for="(item, index) in images" :key="item.id" class="gallery-list__img">
<el-col :span="6" v-for="(item, index) in filterList" :key="item.id" class="gallery-list__img">
<div
class="gallery-list__item"
@click="zoomImage(index)"
Expand Down Expand Up @@ -162,12 +162,12 @@ export default class extends Vue {
async created () {
ipcRenderer.on('updateGallery', (event: IpcRendererEvent) => {
this.$nextTick(async () => {
this.images = await this.$$db.get()
this.updateGallery()
})
})
ipcRenderer.send('getPicBeds')
ipcRenderer.on('getPicBeds', this.getPicBeds)
this.images = await this.$$db.get()
this.updateGallery()
}
mounted () {
document.addEventListener('keydown', this.handleDetectShiftKey)
Expand Down Expand Up @@ -212,6 +212,9 @@ export default class extends Vue {
return this.images
}
}
async updateGallery () {
this.images = (await this.$$db.get({ orderBy: 'desc' })).data
}
@Watch('filterList')
handleFilterListChange () {
Expand Down Expand Up @@ -257,7 +260,8 @@ export default class extends Vue {
}
async copy (item: ImgInfo) {
const style = await this.getConfig<IPasteStyle>('settings.pasteStyle') || IPasteStyle.MARKDOWN
const copyLink = pasteStyle(style, item)
const customLink = await this.getConfig<string>('settings.customLink')
const copyLink = pasteStyle(style, item, customLink)
const obj = {
title: '复制链接成功',
body: copyLink,
Expand Down Expand Up @@ -286,7 +290,7 @@ export default class extends Vue {
myNotification.onclick = () => {
return true
}
this.getGallery()
this.updateGallery()
}).catch((e) => {
console.log(e)
return true
Expand All @@ -311,7 +315,7 @@ export default class extends Vue {
return true
}
this.dialogVisible = false
this.getGallery()
this.updateGallery()
}
choosePicBed (type: string) {
let idx = this.choosedPicBed.indexOf(type)
Expand Down Expand Up @@ -356,7 +360,6 @@ export default class extends Vue {
}
this.clearChoosedList()
this.choosedList = {} // 只有删除才能将这个置空
this.getGallery()
const obj = {
title: '操作结果',
body: '删除成功'
Expand All @@ -366,6 +369,7 @@ export default class extends Vue {
myNotification.onclick = () => {
return true
}
this.updateGallery()
}).catch(() => {
return true
})
Expand All @@ -375,14 +379,15 @@ export default class extends Vue {
if (Object.values(this.choosedList).some(item => item)) {
const copyString: string[] = []
const style = await this.getConfig<IPasteStyle>('settings.pasteStyle') || IPasteStyle.MARKDOWN
const customLink = await this.getConfig<string>('settings.customLink')
// choosedList -> { [id]: true or false }; true means choosed. false means not choosed.
const imageIDList = Object.keys(this.choosedList)
for (let i = 0; i < imageIDList.length; i++) {
const key = imageIDList[i]
if (this.choosedList[key]) {
const item = await this.$$db.getById<ImgInfo>(key)
if (item) {
copyString.push(pasteStyle(style, item))
copyString.push(pasteStyle(style, item, customLink))
this.choosedList[key] = false
}
}
Expand Down
Loading

0 comments on commit f1eb7f4

Please sign in to comment.