🚧 WIP: add gallery db

This commit is contained in:
PiEgg 2021-07-27 00:15:11 +08:00
parent 76964ff1a5
commit c70c3aff78
8 changed files with 118 additions and 50 deletions

View File

@ -221,6 +221,7 @@ export function createTray () {
setTimeout(() => { setTimeout(() => {
notification.show() notification.show()
}, i * 100) }, i * 100)
// FIXME: gallery db
db.insert('uploaded', imgs[i]) db.insert('uploaded', imgs[i])
} }
handleCopyUrl(pasteText.join('\n')) handleCopyUrl(pasteText.join('\n'))

View File

@ -77,13 +77,27 @@ class ConfigStore {
// @ts-ignore // @ts-ignore
return this.read().get(key).removeById(id).write() return this.read().get(key).removeById(id).write()
} }
getConfigPath () {
return CONFIG_PATH
}
} }
export default new ConfigStore() export default new ConfigStore()
// v2.3.0 add gallery db // v2.3.0 add gallery db
const dbStore = new DBStore(DB_PATH, 'gallery') class GalleryDB {
private static instance: DBStore
private constructor () {
console.log('init gallery db')
}
public static getInstance (): DBStore {
if (!GalleryDB.instance) {
GalleryDB.instance = new DBStore(DB_PATH, 'gallery')
}
return GalleryDB.instance
}
}
export { export {
dbStore GalleryDB
} }

View File

@ -16,7 +16,19 @@ import { IGuiMenuItem } from 'picgo/dist/src/types'
import windowManager from 'apis/app/window/windowManager' import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants' import { IWindowList } from 'apis/app/window/constants'
import { showNotification } from '~/main/utils/common' import { showNotification } from '~/main/utils/common'
import { PICGO_SAVE_CONFIG, PICGO_GET_CONFIG } from '#/events/constants' import {
PICGO_SAVE_CONFIG,
PICGO_GET_CONFIG,
PICGO_GET_DB,
PICGO_INSERT_DB,
PICGO_INSERT_MANY_DB,
PICGO_UPDATE_BY_ID_DB,
PICGO_GET_BY_ID_DB,
PICGO_REMOVE_BY_ID_DB
} from '#/events/constants'
import { GalleryDB } from 'apis/core/datastore'
import { IObject } from '@picgo/store/dist/types'
// eslint-disable-next-line // eslint-disable-next-line
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
@ -270,6 +282,44 @@ const handleImportLocalPlugin = () => {
}) })
} }
const handlePicGoGalleryDB = () => {
ipcMain.on(PICGO_GET_DB, async (event: IpcMainEvent, callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.get()
event.sender.send(PICGO_GET_CONFIG, res, callbackId)
})
ipcMain.on(PICGO_INSERT_DB, async (event: IpcMainEvent, value: IObject, callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.insert(value)
event.sender.send(PICGO_INSERT_DB, res, callbackId)
})
ipcMain.on(PICGO_INSERT_MANY_DB, async (event: IpcMainEvent, value: IObject[], callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.insertMany(value)
event.sender.send(PICGO_INSERT_MANY_DB, res, callbackId)
})
ipcMain.on(PICGO_UPDATE_BY_ID_DB, async (event: IpcMainEvent, id: string, value: IObject[], callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.updateById(id, value)
event.sender.send(PICGO_UPDATE_BY_ID_DB, res, callbackId)
})
ipcMain.on(PICGO_GET_BY_ID_DB, async (event: IpcMainEvent, id: string, callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.getById(id)
event.sender.send(PICGO_GET_BY_ID_DB, res, callbackId)
})
ipcMain.on(PICGO_REMOVE_BY_ID_DB, async (event: IpcMainEvent, id: string, callbackId: string) => {
const dbStore = GalleryDB.getInstance()
const res = await dbStore.removeById(id)
event.sender.send(PICGO_REMOVE_BY_ID_DB, res, callbackId)
})
}
export default { export default {
listen () { listen () {
handleGetPluginList() handleGetPluginList()
@ -281,6 +331,7 @@ export default {
handleRemoveFiles() handleRemoveFiles()
handlePicGoSaveConfig() handlePicGoSaveConfig()
handlePicGoGetConfig() handlePicGoGetConfig()
handlePicGoGalleryDB()
handleImportLocalPlugin() handleImportLocalPlugin()
} }
} }

View File

@ -16,7 +16,8 @@ import busEventList from '~/main/events/busEventList'
import { IWindowList } from 'apis/app/window/constants' import { IWindowList } from 'apis/app/window/constants'
import windowManager from 'apis/app/window/windowManager' import windowManager from 'apis/app/window/windowManager'
import { import {
updateShortKeyFromVersion212 updateShortKeyFromVersion212,
migrateGalleryFromVersion230
} from '~/main/migrate' } from '~/main/migrate'
import { import {
uploadChoosedFiles, uploadChoosedFiles,
@ -29,7 +30,7 @@ import server from '~/main/server/index'
import updateChecker from '~/main/utils/updateChecker' import updateChecker from '~/main/utils/updateChecker'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler' import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
import { getUploadFiles } from '~/main/utils/handleArgv' import { getUploadFiles } from '~/main/utils/handleArgv'
import db from '~/main/apis/core/datastore' import db, { GalleryDB } from '~/main/apis/core/datastore'
import bus from '@core/bus' import bus from '@core/bus'
import { privacyManager } from '~/main/utils/privacyManager' import { privacyManager } from '~/main/utils/privacyManager'
import logger from 'apis/core/picgo/logger' import logger from 'apis/core/picgo/logger'
@ -54,7 +55,7 @@ const handleStartUpFiles = (argv: string[], cwd: string) => {
} }
class LifeCycle { class LifeCycle {
private beforeReady () { private async beforeReady () {
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }]) protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
// fix the $PATH in macOS // fix the $PATH in macOS
fixPath() fixPath()
@ -62,6 +63,7 @@ class LifeCycle {
ipcList.listen() ipcList.listen()
busEventList.listen() busEventList.listen()
updateShortKeyFromVersion212(db, db.get('settings.shortKey')) updateShortKeyFromVersion212(db, db.get('settings.shortKey'))
await migrateGalleryFromVersion230(db, GalleryDB.getInstance())
} }
private onReady () { private onReady () {
app.on('ready', async () => { app.on('ready', async () => {
@ -162,12 +164,12 @@ class LifeCycle {
} }
} }
} }
launchApp () { async launchApp () {
const gotTheLock = app.requestSingleInstanceLock() const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) { if (!gotTheLock) {
app.quit() app.quit()
} else { } else {
this.beforeReady() await this.beforeReady()
this.onReady() this.onReady()
this.onRunning() this.onRunning()
this.onQuit() this.onQuit()

View File

@ -1,6 +1,9 @@
import DB from '~/main/apis/core/datastore' import { DBStore } from '@picgo/store'
import ConfigStore from '~/main/apis/core/datastore'
import path from 'path'
import fse from 'fs-extra'
// from v2.1.2 // from v2.1.2
const updateShortKeyFromVersion212 = (db: typeof DB, shortKeyConfig: IShortKeyConfigs | IOldShortKeyConfigs) => { const updateShortKeyFromVersion212 = (db: typeof ConfigStore, shortKeyConfig: IShortKeyConfigs | IOldShortKeyConfigs) => {
// #557 极端情况可能会出现配置不存在,需要重新写入 // #557 极端情况可能会出现配置不存在,需要重新写入
if (shortKeyConfig === undefined) { if (shortKeyConfig === undefined) {
const defaultShortKeyConfig = { const defaultShortKeyConfig = {
@ -28,6 +31,21 @@ const updateShortKeyFromVersion212 = (db: typeof DB, shortKeyConfig: IShortKeyCo
return false return false
} }
export { const migrateGalleryFromVersion230 = async (configDB: typeof ConfigStore, galleryDB: DBStore) => {
updateShortKeyFromVersion212 const originGallery: ImgInfo[] = configDB.get('uploaded')
const configPath = configDB.getConfigPath()
const configBakPath = path.join(path.dirname(configPath), 'config-bak.json')
if (fse.existsSync(configBakPath)) {
return
}
fse.copyFileSync(configPath, configBakPath)
// migrate gallery from config to gallery db
if (originGallery && originGallery.length > 0) {
await galleryDB.insertMany(originGallery)
}
}
export {
updateShortKeyFromVersion212,
migrateGalleryFromVersion230
} }

View File

@ -159,14 +159,15 @@ export default class extends Vue {
this.clearChoosedList() this.clearChoosedList()
} }
} }
created () { async created () {
ipcRenderer.on('updateGallery', (event: IpcRendererEvent) => { ipcRenderer.on('updateGallery', (event: IpcRendererEvent) => {
this.$nextTick(() => { this.$nextTick(async () => {
this.filterList = this.getGallery() this.images = await this.$$db.get()
}) })
}) })
ipcRenderer.send('getPicBeds') ipcRenderer.send('getPicBeds')
ipcRenderer.on('getPicBeds', this.getPicBeds) ipcRenderer.on('getPicBeds', this.getPicBeds)
this.images = await this.$$db.get()
} }
mounted () { mounted () {
document.addEventListener('keydown', this.handleDetectShiftKey) document.addEventListener('keydown', this.handleDetectShiftKey)
@ -180,9 +181,6 @@ export default class extends Vue {
get filterList () { get filterList () {
return this.getGallery() return this.getGallery()
} }
set filterList (val) {
this.images = val
}
get isAllSelected () { get isAllSelected () {
const values = Object.values(this.choosedList) const values = Object.values(this.choosedList)
if (values.length === 0) { if (values.length === 0) {
@ -196,38 +194,21 @@ export default class extends Vue {
getPicBeds (event: IpcRendererEvent, picBeds: IPicBedType[]) { getPicBeds (event: IpcRendererEvent, picBeds: IPicBedType[]) {
this.picBed = picBeds this.picBed = picBeds
} }
// FIXME: gallery db && async computed - -||..... getGallery (): ImgInfo[] {
getGallery () { if (this.searchText || this.choosedPicBed.length > 0) {
if (this.choosedPicBed.length > 0) { return this.images
let arr: ImgInfo[] = [] .filter(item => {
this.choosedPicBed.forEach(item => { let isInChoosedPicBed = true
let obj: IObj = { if (this.choosedPicBed.length > 0) {
type: item isInChoosedPicBed = this.choosedPicBed.some(type => type === item.type)
} }
if (this.searchText) { return item.fileName?.includes(this.searchText) && isInChoosedPicBed
obj.fileName = this.searchText })
}
arr = arr.concat(this.$db.read().get('uploaded').filter(obj => {
return obj.fileName.indexOf(this.searchText) !== -1 && obj.type === item
}).reverse().value())
})
this.images = arr
} else { } else {
if (this.searchText) { return this.images
// FIXME: gallery db
let data = this.$db.read().get('uploaded')
// @ts-ignore
.filter(item => {
return item.fileName.indexOf(this.searchText) !== -1
}).reverse().value()
this.images = data
} else {
// FIXME: gallery db
this.images = this.$db.read().get('uploaded').slice().reverse().value()
}
} }
return this.images
} }
@Watch('filterList') @Watch('filterList')
handleFilterListChange () { handleFilterListChange () {
this.clearChoosedList() this.clearChoosedList()

View File

@ -27,8 +27,8 @@ export class GalleryDB implements IGalleryDB {
const res = await this.msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value) const res = await this.msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value)
return res return res
} }
async getById (id: string): Promise<IObject | undefined> { async getById<T> (id: string): Promise<IResult<T> | undefined> {
const res = await this.msgHandler<IObject | undefined>(PICGO_GET_BY_ID_DB, id) const res = await this.msgHandler<IResult<T> | undefined>(PICGO_GET_BY_ID_DB, id)
return res return res
} }
async removeById (id: string): Promise<void> { async removeById (id: string): Promise<void> {
@ -45,7 +45,7 @@ export class GalleryDB implements IGalleryDB {
} }
} }
ipcRenderer.on(method, callback) ipcRenderer.on(method, callback)
ipcRenderer.send(method, ...args) ipcRenderer.send(method, ...args, callbackId)
}) })
} }
} }

View File

@ -45,6 +45,7 @@ interface ImgInfo {
extname?: string extname?: string
imgUrl?: string imgUrl?: string
id?: string id?: string
type?: string
[propName: string]: any [propName: string]: any
} }