mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-07 00:30:14 +08:00
✨ Feature(custom): refactor the logic of config data
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import path from 'node:path'
|
||||
|
||||
import { defaultDir } from '@core/datastore/dirs'
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
import axios from 'axios'
|
||||
import { app, clipboard, dialog, shell } from 'electron'
|
||||
@@ -14,9 +15,7 @@ const REMOTE_NOTICE_URL = 'https://release.piclist.cn/remote-notice.json'
|
||||
|
||||
const REMOTE_NOTICE_LOCAL_STORAGE_FILE = 'piclist-remote-notice.json'
|
||||
|
||||
const STORE_PATH = app.getPath('userData')
|
||||
|
||||
const REMOTE_NOTICE_LOCAL_STORAGE_PATH = path.join(STORE_PATH, REMOTE_NOTICE_LOCAL_STORAGE_FILE)
|
||||
const REMOTE_NOTICE_LOCAL_STORAGE_PATH = path.join(defaultDir(), REMOTE_NOTICE_LOCAL_STORAGE_FILE)
|
||||
|
||||
class RemoteNoticeHandler {
|
||||
private remoteNotice: IRemoteNotice | null = null
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import bus from '@core/bus'
|
||||
import db from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
import logger from '@core/picgo/logger'
|
||||
import shortKeyService from 'apis/app/shortKey/shortKeyService'
|
||||
@@ -23,7 +22,7 @@ class ShortKeyHandler {
|
||||
}
|
||||
|
||||
private initBuiltInShortKey() {
|
||||
const commands = db.get(configPaths.settings.shortKey._path) as IShortKeyConfigs
|
||||
const commands = picgo.getConfig<IShortKeyConfigs>(configPaths.settings.shortKey._path) || {}
|
||||
Object.keys(commands)
|
||||
.filter(item => item.includes('picgo:'))
|
||||
.forEach(command => {
|
||||
@@ -51,8 +50,8 @@ class ShortKeyHandler {
|
||||
const commands = plugin.commands(picgo) as IPluginShortKeyConfig[]
|
||||
for (const cmd of commands) {
|
||||
const command = `${item}:${cmd.name}`
|
||||
if (db.has(`settings.shortKey[${command}]`)) {
|
||||
const commandConfig = db.get(`settings.shortKey.${command}`) as IShortKeyConfig
|
||||
const commandConfig = picgo.getConfig<IShortKeyConfig | undefined>(`settings.shortKey[${command}]`)
|
||||
if (commandConfig) {
|
||||
// if disabled, don't register #534
|
||||
if (commandConfig.enable) {
|
||||
this.registerShortKey(commandConfig, command, cmd.handle, false)
|
||||
@@ -159,8 +158,8 @@ class ShortKeyHandler {
|
||||
const commands = plugin.commands(picgo) as IPluginShortKeyConfig[]
|
||||
for (const cmd of commands) {
|
||||
const command = `${pluginName}:${cmd.name}`
|
||||
if (db.has(`settings.shortKey[${command}]`)) {
|
||||
const commandConfig = db.get(`settings.shortKey[${command}]`) as IShortKeyConfig
|
||||
const commandConfig = picgo.getConfig<IShortKeyConfig | undefined>(`settings.shortKey[${command}]`)
|
||||
if (commandConfig) {
|
||||
this.registerShortKey(commandConfig, command, cmd.handle, false)
|
||||
} else {
|
||||
this.registerShortKey(cmd, command, cmd.handle, true)
|
||||
@@ -170,7 +169,7 @@ class ShortKeyHandler {
|
||||
}
|
||||
|
||||
unregisterPluginShortKey(pluginName: string) {
|
||||
const commands = db.get(configPaths.settings.shortKey._path) as IShortKeyConfigs
|
||||
const commands = picgo.getConfig<IShortKeyConfigs>(configPaths.settings.shortKey._path) || {}
|
||||
const keyList = Object.keys(commands)
|
||||
.filter(command => command.includes(pluginName))
|
||||
.map(command => {
|
||||
@@ -182,7 +181,7 @@ class ShortKeyHandler {
|
||||
keyList.forEach(item => {
|
||||
globalShortcut.unregister(item.key)
|
||||
shortKeyService.unregisterCommand(item.command)
|
||||
db.unset(configPaths.settings.shortKey._path, item.command)
|
||||
picgo.removeConfig(configPaths.settings.shortKey._path, item.command)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import db, { GalleryDB } from '@core/datastore'
|
||||
import { GalleryDB } from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
import uploader from 'apis/app/uploader'
|
||||
import { uploadClipboardFiles } from 'apis/app/uploader/apis'
|
||||
@@ -36,7 +36,7 @@ import uploadDarkPng from '../../../../../resources/upload-dark.png?asset&asarUn
|
||||
let contextMenu: Menu | null
|
||||
|
||||
export function setDockMenu() {
|
||||
const isListeningClipboard = db.get(configPaths.settings.isListeningClipboard) || false
|
||||
const isListeningClipboard = picgo.getConfig<boolean | undefined>(configPaths.settings.isListeningClipboard) || false
|
||||
const dockMenu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: $t('OPEN_MAIN_WINDOW'),
|
||||
@@ -45,7 +45,7 @@ export function setDockMenu() {
|
||||
{
|
||||
label: $t('START_WATCH_CLIPBOARD'),
|
||||
click() {
|
||||
db.set(configPaths.settings.isListeningClipboard, true)
|
||||
picgo.saveConfig({ [configPaths.settings.isListeningClipboard]: true })
|
||||
clipboardPoll.startListening()
|
||||
clipboardPoll.on('change', () => {
|
||||
picgo.log.info('clipboard changed')
|
||||
@@ -58,7 +58,7 @@ export function setDockMenu() {
|
||||
{
|
||||
label: $t('STOP_WATCH_CLIPBOARD'),
|
||||
click() {
|
||||
db.set(configPaths.settings.isListeningClipboard, false)
|
||||
picgo.saveConfig({ [configPaths.settings.isListeningClipboard]: false })
|
||||
clipboardPoll.stopListening()
|
||||
clipboardPoll.removeAllListeners()
|
||||
setDockMenu()
|
||||
@@ -108,12 +108,12 @@ export function createMenu() {
|
||||
|
||||
export function createContextMenu() {
|
||||
const ClipboardWatcher = clipboardPoll
|
||||
const isListeningClipboard = db.get(configPaths.settings.isListeningClipboard) || false
|
||||
const isListeningClipboard = picgo.getConfig<boolean | undefined>(configPaths.settings.isListeningClipboard) || false
|
||||
const isMiniWindowVisible =
|
||||
windowManager.has(IWindowList.MINI_WINDOW) && windowManager.get(IWindowList.MINI_WINDOW)!.isVisible()
|
||||
|
||||
const startWatchClipboard = () => {
|
||||
db.set(configPaths.settings.isListeningClipboard, true)
|
||||
picgo.saveConfig({ [configPaths.settings.isListeningClipboard]: true })
|
||||
ClipboardWatcher.startListening()
|
||||
ClipboardWatcher.on('change', () => {
|
||||
picgo.log.info('clipboard changed')
|
||||
@@ -123,7 +123,7 @@ export function createContextMenu() {
|
||||
}
|
||||
|
||||
const stopWatchClipboard = () => {
|
||||
db.set(configPaths.settings.isListeningClipboard, false)
|
||||
picgo.saveConfig({ [configPaths.settings.isListeningClipboard]: false })
|
||||
ClipboardWatcher.stopListening()
|
||||
ClipboardWatcher.removeAllListeners()
|
||||
createContextMenu()
|
||||
@@ -284,7 +284,8 @@ export function createTray(tooltip: string) {
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
|
||||
}
|
||||
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
||||
const autoCloseMiniWindow = db.get(configPaths.settings.autoCloseMiniWindow) || false
|
||||
const autoCloseMiniWindow =
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.autoCloseMiniWindow) || false
|
||||
settingWindow!.show()
|
||||
settingWindow!.focus()
|
||||
if (windowManager.has(IWindowList.MINI_WINDOW) && autoCloseMiniWindow) {
|
||||
@@ -309,13 +310,13 @@ export function createTray(tooltip: string) {
|
||||
// so the tray window must be available
|
||||
if (process.platform === 'darwin') {
|
||||
;(tray as any).on('drop-files', async (_: Event, files: string[]) => {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const pasteStyle = picgo.getConfig<string>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const rawInput = cloneDeep(files)
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
||||
const res = await uploader.setWebContents(trayWindow.webContents).uploadReturnCtx(files)
|
||||
const imgs = res[0] ? res[0] : false
|
||||
const backImgs = res[1] ? res[1] : false
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
const deleteLocalFile = picgo.getConfig<boolean | undefined>(configPaths.settings.deleteLocalFile) || false
|
||||
if (imgs !== false) {
|
||||
const pasteText: string[] = []
|
||||
for (let i = 0; i < imgs.length; i++) {
|
||||
@@ -325,14 +326,14 @@ export function createTray(tooltip: string) {
|
||||
const [pasteTextItem, shortUrl] = await pasteTemplate(
|
||||
pasteStyle,
|
||||
imgs[i],
|
||||
db.get(configPaths.settings.customLink),
|
||||
picgo.getConfig<string | undefined>(configPaths.settings.customLink),
|
||||
)
|
||||
imgs[i].shortUrl = shortUrl
|
||||
pasteText.push(pasteTextItem)
|
||||
const isShowResultNotification =
|
||||
db.get(configPaths.settings.uploadResultNotification) === undefined
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.uploadResultNotification)
|
||||
: !!picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification)
|
||||
if (isShowResultNotification) {
|
||||
const notification = new Notification({
|
||||
title: $t('UPLOAD_SUCCEED'),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import db, { GalleryDB } from '@core/datastore'
|
||||
import { GalleryDB } from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
import uploader from 'apis/app/uploader'
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
@@ -14,9 +14,9 @@ import pasteTemplate from '~/utils/pasteTemplate'
|
||||
|
||||
const handleClipboardUploadingReturnCtx = async (img?: IUploadOption): Promise<(ImgInfo[] | false)[]> => {
|
||||
const useBuiltinClipboard =
|
||||
db.get(configPaths.settings.useBuiltinClipboard) === undefined
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.useBuiltinClipboard) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.useBuiltinClipboard)
|
||||
: !!picgo.getConfig<boolean | undefined>(configPaths.settings.useBuiltinClipboard)
|
||||
const win = windowManager.getAvailableWindow()
|
||||
if (useBuiltinClipboard) {
|
||||
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboardReturnCtx(img)
|
||||
@@ -33,14 +33,18 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
if (img !== false) {
|
||||
if (img.length > 0) {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const [pastedText, shortUrl] = await pasteTemplate(pasteStyle, img[0], db.get(configPaths.settings.customLink))
|
||||
const pasteStyle = picgo.getConfig<string>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const [pastedText, shortUrl] = await pasteTemplate(
|
||||
pasteStyle,
|
||||
img[0],
|
||||
picgo.getConfig<string | undefined>(configPaths.settings.customLink),
|
||||
)
|
||||
img[0].shortUrl = shortUrl
|
||||
handleCopyUrl(pastedText)
|
||||
const isShowResultNotification =
|
||||
db.get(configPaths.settings.uploadResultNotification) === undefined
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.uploadResultNotification)
|
||||
: !!picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification)
|
||||
if (isShowResultNotification) {
|
||||
const notification = new Notification({
|
||||
title: $t('UPLOAD_SUCCEED'),
|
||||
@@ -101,8 +105,8 @@ export const uploadChoosedFiles = async (
|
||||
backImgs = res[1] ? res[1] : false
|
||||
const result = []
|
||||
if (imgs !== false) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
const pasteStyle = picgo.getConfig<string>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const deleteLocalFile = picgo.getConfig<boolean>(configPaths.settings.deleteLocalFile) || false
|
||||
const pasteText: string[] = []
|
||||
const imgLength = imgs.length
|
||||
for (let i = 0; i < imgLength; i++) {
|
||||
@@ -118,14 +122,14 @@ export const uploadChoosedFiles = async (
|
||||
const [pasteTextItem, shortUrl] = await pasteTemplate(
|
||||
pasteStyle,
|
||||
imgs[i],
|
||||
db.get(configPaths.settings.customLink),
|
||||
picgo.getConfig<string | undefined>(configPaths.settings.customLink),
|
||||
)
|
||||
imgs[i].shortUrl = shortUrl
|
||||
pasteText.push(pasteTextItem)
|
||||
const isShowResultNotification =
|
||||
db.get(configPaths.settings.uploadResultNotification) === undefined
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.uploadResultNotification)
|
||||
: !!picgo.getConfig<boolean | undefined>(configPaths.settings.uploadResultNotification)
|
||||
if (isShowResultNotification) {
|
||||
if (imgLength <= 3) {
|
||||
const notification = new Notification({
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import path from 'node:path'
|
||||
import util from 'node:util'
|
||||
|
||||
import db from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
import logger from '@core/picgo/logger'
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
@@ -49,7 +48,7 @@ class Uploader {
|
||||
})
|
||||
|
||||
picgo.on(ICOREBuildInEvent.BEFORE_TRANSFORM, () => {
|
||||
if (db.get(configPaths.settings.uploadNotification)) {
|
||||
if (picgo.getConfig<boolean | undefined>(configPaths.settings.uploadNotification)) {
|
||||
const notification = new Notification({
|
||||
title: $t('UPLOAD_PROGRESS'),
|
||||
body: $t('UPLOADING'),
|
||||
@@ -62,9 +61,9 @@ class Uploader {
|
||||
handle: async (ctx: IPicGo) => {
|
||||
const uploaderType = getUploaderType(ctx)
|
||||
|
||||
const globalRename = db.get(configPaths.settings.rename)
|
||||
const globalAutoRename = db.get(configPaths.settings.autoRename)
|
||||
const buildInList = db.get(configPaths.buildIn.list._name) || []
|
||||
const globalRename = picgo.getConfig<boolean | undefined>(configPaths.settings.rename)
|
||||
const globalAutoRename = picgo.getConfig<boolean | undefined>(configPaths.settings.autoRename)
|
||||
const buildInList = picgo.getConfig<any[]>(configPaths.buildIn.list._name) || []
|
||||
const idSpecificRename = buildInList.find((item: any) => item.id === uploaderType.id)?.manualRename
|
||||
const idSpecificAutoRename = buildInList.find((item: any) => item.id === uploaderType.id)?.autoRename
|
||||
const rename = idSpecificRename !== undefined ? !!idSpecificRename : !!globalRename
|
||||
@@ -118,25 +117,6 @@ class Uploader {
|
||||
return filePath
|
||||
}
|
||||
|
||||
/**
|
||||
* use electron's clipboard image to upload
|
||||
*/
|
||||
async uploadWithBuildInClipboard(): Promise<ImgInfo[] | false> {
|
||||
let imgPath: string | false = false
|
||||
try {
|
||||
imgPath = await this.getClipboardImagePath()
|
||||
if (!imgPath) return false
|
||||
return await this.upload([imgPath])
|
||||
} catch (e: any) {
|
||||
logger.error(e)
|
||||
return false
|
||||
} finally {
|
||||
if (imgPath && imgPath.startsWith(path.join(picgo.baseDir, CLIPBOARD_IMAGE_FOLDER))) {
|
||||
fs.remove(imgPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async uploadWithBuildInClipboardReturnCtx(img?: IUploadOption): Promise<(ImgInfo[] | false)[]> {
|
||||
let imgPath: string | false = false
|
||||
try {
|
||||
@@ -160,14 +140,14 @@ class Uploader {
|
||||
|
||||
if (Array.isArray(res.output) && res.output.some((item: ImgInfo) => item.imgUrl)) {
|
||||
res.output.forEach((item: ImgInfo) => {
|
||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||
item.config = JSON.parse(JSON.stringify(picgo.getConfig<any>(`picBed.${item.type}`)))
|
||||
})
|
||||
result[0] = res.output
|
||||
}
|
||||
|
||||
if (Array.isArray(res.backupOutput) && res.backupOutput.some((item: ImgInfo) => item.imgUrl)) {
|
||||
res.backupOutput.forEach((item: ImgInfo) => {
|
||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||
item.config = JSON.parse(JSON.stringify(picgo.getConfig<any>(`picBed.${item.type}`)))
|
||||
})
|
||||
result[1] = res.backupOutput
|
||||
}
|
||||
@@ -186,29 +166,6 @@ class Uploader {
|
||||
ipcMain.removeAllListeners(GET_RENAME_FILE_NAME)
|
||||
}
|
||||
}
|
||||
|
||||
async upload(img?: IUploadOption): Promise<ImgInfo[] | false> {
|
||||
try {
|
||||
const output = await picgo.upload(img)
|
||||
if (!Array.isArray(output) || !output.some((item: ImgInfo) => item.imgUrl)) return false
|
||||
output.forEach((item: ImgInfo) => {
|
||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||
})
|
||||
return output.filter(item => item.imgUrl)
|
||||
} catch (e: any) {
|
||||
logger.error(e)
|
||||
setTimeout(() => {
|
||||
showNotification({
|
||||
title: $t('UPLOAD_FAILED'),
|
||||
body: util.format(e.stack),
|
||||
clickToCopy: true,
|
||||
})
|
||||
}, 500)
|
||||
return false
|
||||
} finally {
|
||||
ipcMain.removeAllListeners(GET_RENAME_FILE_NAME)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Uploader()
|
||||
|
||||
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
|
||||
|
||||
import bus from '@core/bus'
|
||||
import { CREATE_APP_MENU } from '@core/bus/constants'
|
||||
import db from '@core/datastore'
|
||||
import picgo from '@core/picgo'
|
||||
import { app, BrowserWindow, Rectangle } from 'electron'
|
||||
|
||||
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '~/events/constant'
|
||||
@@ -16,10 +16,8 @@ import logo from '../../../../../resources/logo.png?asset&asarUnpack'
|
||||
const windowList = new Map<string, IWindowListItem>()
|
||||
|
||||
const getDefaultWindowSizes = (): { width: number; height: number } => {
|
||||
const [mainWindowWidth, mainWindowHeight] = db.get([
|
||||
configPaths.settings.mainWindowWidth,
|
||||
configPaths.settings.mainWindowHeight,
|
||||
])
|
||||
const mainWindowWidth = picgo.getConfig<number>(configPaths.settings.mainWindowWidth)
|
||||
const mainWindowHeight = picgo.getConfig<number>(configPaths.settings.mainWindowHeight)
|
||||
return {
|
||||
width: mainWindowWidth || 1200,
|
||||
height: mainWindowHeight || 800,
|
||||
@@ -115,7 +113,7 @@ const miniWindowOptions = {
|
||||
},
|
||||
} as IBrowserWindowOptions
|
||||
|
||||
if (db.get(configPaths.settings.miniWindowOntop)) {
|
||||
if (picgo.getConfig<boolean>(configPaths.settings.miniWindowOntop)) {
|
||||
miniWindowOptions.alwaysOnTop = true
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
import path from 'node:path'
|
||||
|
||||
import { getLogger } from '@core/utils/localLogger'
|
||||
import { appConfigBackupPath, appConfigPath, galleryDBBackupPath, galleryDBPath } from '@core/datastore/dirs'
|
||||
import dayjs from 'dayjs'
|
||||
import { app } from 'electron'
|
||||
import fs from 'fs-extra'
|
||||
import writeFile from 'write-file-atomic'
|
||||
|
||||
import { T as $t } from '~/i18n'
|
||||
import { notificationList } from '~/utils/notification'
|
||||
|
||||
const STORE_PATH = app.getPath('userData')
|
||||
|
||||
const configFilePath = path.join(STORE_PATH, 'data.json')
|
||||
const configFileBackupPath = path.join(STORE_PATH, 'data.bak.json')
|
||||
export const defaultConfigPath = configFilePath
|
||||
|
||||
let _configFilePath = ''
|
||||
let hasCheckPath = false
|
||||
const configFileBackupPath = appConfigBackupPath()
|
||||
|
||||
const errorMsg = {
|
||||
broken: $t('TIPS_PICGO_CONFIG_FILE_BROKEN_WITH_DEFAULT'),
|
||||
@@ -27,7 +17,8 @@ function dbChecker() {
|
||||
if (process.type !== 'renderer') {
|
||||
// db save bak
|
||||
try {
|
||||
const { dbPath, dbBackupPath } = getGalleryDBPath()
|
||||
const dbPath = galleryDBPath()
|
||||
const dbBackupPath = galleryDBBackupPath()
|
||||
if (fs.existsSync(dbPath)) {
|
||||
fs.copyFileSync(dbPath, dbBackupPath)
|
||||
}
|
||||
@@ -35,7 +26,7 @@ function dbChecker() {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
const configFilePath = dbPathChecker()
|
||||
const configFilePath = appConfigPath()
|
||||
if (!fs.existsSync(configFilePath)) {
|
||||
return
|
||||
}
|
||||
@@ -77,64 +68,4 @@ function dbChecker() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config path
|
||||
*/
|
||||
function dbPathChecker(): string {
|
||||
if (_configFilePath) {
|
||||
return _configFilePath
|
||||
}
|
||||
_configFilePath = defaultConfigPath
|
||||
// if defaultConfig path is not exit
|
||||
// do not parse the content of config
|
||||
if (!fs.existsSync(defaultConfigPath)) {
|
||||
return _configFilePath
|
||||
}
|
||||
try {
|
||||
const configString = fs.readFileSync(defaultConfigPath, {
|
||||
encoding: 'utf-8',
|
||||
})
|
||||
const config = JSON.parse(configString)
|
||||
const userConfigPath: string = config.configPath || ''
|
||||
if (userConfigPath) {
|
||||
if (fs.existsSync(userConfigPath) && userConfigPath.endsWith('.json')) {
|
||||
_configFilePath = userConfigPath
|
||||
return _configFilePath
|
||||
}
|
||||
}
|
||||
return _configFilePath
|
||||
} catch (e) {
|
||||
const piclistLogPath = path.join(STORE_PATH, 'piclist-gui-local.log')
|
||||
const logger = getLogger(piclistLogPath, 'PicList')
|
||||
if (!hasCheckPath) {
|
||||
const optionsTpl = {
|
||||
title: $t('TIPS_NOTICE'),
|
||||
body: $t('TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR'),
|
||||
}
|
||||
notificationList.push(optionsTpl)
|
||||
hasCheckPath = true
|
||||
}
|
||||
logger('error', e)
|
||||
_configFilePath = defaultConfigPath
|
||||
return _configFilePath
|
||||
}
|
||||
}
|
||||
|
||||
function dbPathDir() {
|
||||
return path.dirname(dbPathChecker())
|
||||
}
|
||||
|
||||
function getGalleryDBPath(): {
|
||||
dbPath: string
|
||||
dbBackupPath: string
|
||||
} {
|
||||
const configPath = dbPathChecker()
|
||||
const dbPath = path.join(path.dirname(configPath), 'piclist.db')
|
||||
const dbBackupPath = path.join(path.dirname(dbPath), 'piclist.bak.db')
|
||||
return {
|
||||
dbPath,
|
||||
dbBackupPath,
|
||||
}
|
||||
}
|
||||
|
||||
export { dbChecker, dbPathChecker, dbPathDir, getGalleryDBPath }
|
||||
export { dbChecker }
|
||||
|
||||
163
src/main/apis/core/datastore/dirs.ts
Normal file
163
src/main/apis/core/datastore/dirs.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import path from 'node:path'
|
||||
|
||||
import { getLogger } from '@core/utils/localLogger'
|
||||
import { app } from 'electron'
|
||||
import fs from 'fs-extra'
|
||||
|
||||
import { T as $t } from '~/i18n'
|
||||
import { notificationList } from '~/utils/notification'
|
||||
|
||||
let _configFilePath = ''
|
||||
let _manageConfigFilePath = ''
|
||||
let hasCheckPath = false
|
||||
let hasCheckManagePath = false
|
||||
|
||||
export function isPortable() {
|
||||
return fs.existsSync(path.join(exeDir(), 'PORTABLE'))
|
||||
}
|
||||
|
||||
export function exePath() {
|
||||
return app.getPath('exe')
|
||||
}
|
||||
|
||||
export function exeDir() {
|
||||
return path.dirname(exePath())
|
||||
}
|
||||
|
||||
export function userDataDir() {
|
||||
return app.getPath('userData')
|
||||
}
|
||||
|
||||
export function dataDir() {
|
||||
const configDir = path.dirname(appConfigPath())
|
||||
try {
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirsSync(configDir)
|
||||
}
|
||||
} catch (_e) {}
|
||||
return configDir
|
||||
}
|
||||
|
||||
export function defaultDir() {
|
||||
if (isPortable()) return path.join(exeDir(), 'data')
|
||||
return userDataDir()
|
||||
}
|
||||
|
||||
export function defaultConfigPath() {
|
||||
if (isPortable()) {
|
||||
return path.join(exeDir(), 'data', 'data.json')
|
||||
}
|
||||
return path.join(userDataDir(), 'data.json')
|
||||
}
|
||||
|
||||
export function defaultManageConfigPath() {
|
||||
if (isPortable()) {
|
||||
return path.join(exeDir(), 'data', 'manage.json')
|
||||
}
|
||||
return path.join(userDataDir(), 'manage.json')
|
||||
}
|
||||
|
||||
export function appConfigPath() {
|
||||
if (_configFilePath) return _configFilePath
|
||||
_configFilePath = defaultConfigPath()
|
||||
if (!fs.existsSync(_configFilePath)) return _configFilePath
|
||||
try {
|
||||
const configString = fs.readFileSync(_configFilePath, {
|
||||
encoding: 'utf-8',
|
||||
})
|
||||
const config = JSON.parse(configString)
|
||||
// extract custom data dir
|
||||
const userConfigPath: string = config.configPath || ''
|
||||
if (userConfigPath) {
|
||||
if (fs.existsSync(userConfigPath) && userConfigPath.endsWith('.json')) {
|
||||
_configFilePath = userConfigPath
|
||||
return _configFilePath
|
||||
}
|
||||
}
|
||||
return _configFilePath
|
||||
} catch (e) {
|
||||
const piclistLogPath = appGUILogPath()
|
||||
const logger = getLogger(piclistLogPath, 'PicList')
|
||||
if (!hasCheckPath) {
|
||||
const optionsTpl = {
|
||||
title: $t('TIPS_NOTICE'),
|
||||
body: $t('TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR'),
|
||||
}
|
||||
notificationList.push(optionsTpl)
|
||||
hasCheckPath = true
|
||||
}
|
||||
logger('error', e)
|
||||
_configFilePath = defaultConfigPath()
|
||||
return _configFilePath
|
||||
}
|
||||
}
|
||||
|
||||
export function themesDir() {
|
||||
return path.join(defaultDir(), 'themes')
|
||||
}
|
||||
|
||||
export function appConfigBackupPath() {
|
||||
return path.join(dataDir(), 'data.bak.json')
|
||||
}
|
||||
|
||||
export function galleryDBPath() {
|
||||
return path.join(dataDir(), 'piclist.db')
|
||||
}
|
||||
|
||||
export function galleryDBBackupPath() {
|
||||
return path.join(dataDir(), 'piclist.bak.db')
|
||||
}
|
||||
|
||||
export function manageConfigPath() {
|
||||
if (_manageConfigFilePath) return _manageConfigFilePath
|
||||
_manageConfigFilePath = defaultManageConfigPath()
|
||||
if (!fs.existsSync(_manageConfigFilePath)) return _manageConfigFilePath
|
||||
try {
|
||||
const configString = fs.readFileSync(_manageConfigFilePath, {
|
||||
encoding: 'utf-8',
|
||||
})
|
||||
const config = JSON.parse(configString)
|
||||
const userConfigPath: string = config.configPath || ''
|
||||
if (userConfigPath) {
|
||||
if (fs.existsSync(userConfigPath) && userConfigPath.endsWith('.json')) {
|
||||
_manageConfigFilePath = userConfigPath
|
||||
return _manageConfigFilePath
|
||||
}
|
||||
}
|
||||
return _manageConfigFilePath
|
||||
} catch (e) {
|
||||
const manageLogPath = manageGUILogPath()
|
||||
const logger = getLogger(manageLogPath, 'Manage')
|
||||
if (!hasCheckManagePath) {
|
||||
const optionsTpl = {
|
||||
title: $t('TIPS_NOTICE'),
|
||||
body: $t('TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR'),
|
||||
}
|
||||
notificationList.push(optionsTpl)
|
||||
hasCheckManagePath = true
|
||||
}
|
||||
logger('error', e)
|
||||
_manageConfigFilePath = defaultManageConfigPath()
|
||||
return _manageConfigFilePath
|
||||
}
|
||||
}
|
||||
|
||||
export function manageConfigBackupPath() {
|
||||
return path.join(dataDir(), 'manage.bak.json')
|
||||
}
|
||||
|
||||
export function appLogPath() {
|
||||
return path.join(defaultDir(), 'piclist.log')
|
||||
}
|
||||
|
||||
export function appGUILogPath() {
|
||||
return path.join(defaultDir(), 'piclist-gui-local.log')
|
||||
}
|
||||
|
||||
export function manageGUILogPath() {
|
||||
return path.join(defaultDir(), 'manage-gui-local.log')
|
||||
}
|
||||
|
||||
export function manageLogPath() {
|
||||
return path.join(defaultDir(), 'manage.log')
|
||||
}
|
||||
@@ -1,104 +1,7 @@
|
||||
import { dbPathChecker, dbPathDir, getGalleryDBPath } from '@core/datastore/dbChecker'
|
||||
import { DBStore, JSONStore } from '@piclist/store'
|
||||
import fs from 'fs-extra'
|
||||
import type { IConfig } from 'piclist'
|
||||
import { galleryDBPath } from '@core/datastore/dirs'
|
||||
import { DBStore } from '@piclist/store'
|
||||
|
||||
import { T as $t } from '~/i18n'
|
||||
import { configPaths } from '~/utils/configPaths'
|
||||
interface IJSON {
|
||||
[propsName: string]: string | number | IJSON
|
||||
}
|
||||
const STORE_PATH = dbPathDir()
|
||||
|
||||
if (!fs.pathExistsSync(STORE_PATH)) {
|
||||
fs.mkdirpSync(STORE_PATH)
|
||||
}
|
||||
const CONFIG_PATH: string = dbPathChecker()
|
||||
export const DB_PATH: string = getGalleryDBPath().dbPath
|
||||
|
||||
class ConfigStore {
|
||||
#db: JSONStore
|
||||
|
||||
constructor() {
|
||||
this.#db = new JSONStore(CONFIG_PATH)
|
||||
|
||||
if (!this.#db.has('picBed')) {
|
||||
this.#db.set('picBed', {
|
||||
current: 'smms', // deprecated
|
||||
uploader: 'smms',
|
||||
smms: {
|
||||
token: '',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (!this.#db.has(configPaths.settings.shortKey._path)) {
|
||||
this.#db.set(configPaths.settings.shortKey['picgo:upload'], {
|
||||
enable: true,
|
||||
key: 'CommandOrControl+Alt+P',
|
||||
name: 'upload',
|
||||
label: $t('QUICK_UPLOAD'),
|
||||
})
|
||||
}
|
||||
this.read()
|
||||
}
|
||||
|
||||
read(flush?: boolean): IJSON {
|
||||
return this.#db.read(flush)
|
||||
}
|
||||
|
||||
getSingle(key = ''): any {
|
||||
if (key === '') {
|
||||
return this.#db.read(true)
|
||||
}
|
||||
this.read(true)
|
||||
return this.#db.get(key)
|
||||
}
|
||||
|
||||
get(key: string): any
|
||||
get(key: string[]): any[]
|
||||
get(key: string | string[] = ''): any {
|
||||
if (Array.isArray(key)) {
|
||||
return key.map(k => this.getSingle(k))
|
||||
}
|
||||
return this.getSingle(key)
|
||||
}
|
||||
|
||||
set(key: string, value: any): void {
|
||||
this.read(true)
|
||||
return this.#db.set(key, value)
|
||||
}
|
||||
|
||||
has(key: string) {
|
||||
this.read(true)
|
||||
return this.#db.has(key)
|
||||
}
|
||||
|
||||
unset(key: string, value: any): boolean {
|
||||
this.read(true)
|
||||
return this.#db.unset(key, value)
|
||||
}
|
||||
|
||||
saveConfig(config: Partial<IConfig>): void {
|
||||
Object.keys(config).forEach((name: string) => {
|
||||
this.set(name, config[name])
|
||||
})
|
||||
}
|
||||
|
||||
removeConfig(config: IConfig): void {
|
||||
Object.keys(config).forEach((name: string) => {
|
||||
this.unset(name, config[name])
|
||||
})
|
||||
}
|
||||
|
||||
getConfigPath() {
|
||||
return CONFIG_PATH
|
||||
}
|
||||
}
|
||||
|
||||
const db = new ConfigStore()
|
||||
|
||||
export default db
|
||||
export const DB_PATH: string = galleryDBPath()
|
||||
|
||||
// v2.3.0 add gallery db
|
||||
class GalleryDB {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import db from '@core/datastore'
|
||||
import { dbChecker, dbPathChecker } from '@core/datastore/dbChecker'
|
||||
import { debounce } from 'lodash-es'
|
||||
import { dbChecker } from '@core/datastore/dbChecker'
|
||||
import { appConfigPath } from '@core/datastore/dirs'
|
||||
import { PicGo } from 'piclist'
|
||||
import pkg from 'root/package.json'
|
||||
|
||||
const CONFIG_PATH = dbPathChecker()
|
||||
import { T as $t } from '~/i18n'
|
||||
import { configPaths } from '~/utils/configPaths'
|
||||
const CONFIG_PATH = appConfigPath()
|
||||
|
||||
dbChecker()
|
||||
|
||||
@@ -15,20 +16,19 @@ picgo.saveConfig({
|
||||
PICGO_ENV: 'GUI',
|
||||
})
|
||||
|
||||
const shortKeySetting = picgo.getConfig<any>(configPaths.settings.shortKey._path)
|
||||
|
||||
if (!shortKeySetting) {
|
||||
picgo.saveConfig({
|
||||
'settings.shortKey[picgo:upload]': {
|
||||
enable: true,
|
||||
key: 'CommandOrControl+Alt+P',
|
||||
name: 'upload',
|
||||
label: $t('QUICK_UPLOAD'),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
picgo.GUI_VERSION = pkg.version
|
||||
|
||||
const originPicGoSaveConfig = picgo.saveConfig.bind(picgo)
|
||||
|
||||
function flushDB() {
|
||||
db.read(true)
|
||||
}
|
||||
|
||||
const debounced = debounce(flushDB, 1000)
|
||||
|
||||
picgo.saveConfig = (config: IStringKeyMap) => {
|
||||
originPicGoSaveConfig(config)
|
||||
// flush electron's db
|
||||
debounced()
|
||||
}
|
||||
|
||||
export default picgo
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getSettingWindowId, getWindowId } from '@core/bus/apis'
|
||||
import db, { GalleryDB } from '@core/datastore'
|
||||
import { dbPathChecker, defaultConfigPath, getGalleryDBPath } from '@core/datastore/dbChecker'
|
||||
import { GalleryDB } from '@core/datastore'
|
||||
import { appConfigPath, defaultConfigPath as defaultConfigPathF, galleryDBPath } from '@core/datastore/dirs'
|
||||
import picgo from '@core/picgo'
|
||||
import { DBStore } from '@piclist/store'
|
||||
import uploader from 'apis/app/uploader'
|
||||
import { BrowserWindow, dialog, ipcMain, IpcMainEvent, MessageBoxOptions, Notification } from 'electron'
|
||||
@@ -78,8 +79,8 @@ class GuiApi implements IGuiApi {
|
||||
const backImgs = res[1] ? res[1] : false
|
||||
let result: ImgInfo[] = []
|
||||
if (imgs !== false) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
const pasteStyle = picgo.getConfig<string>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const deleteLocalFile = picgo.getConfig<boolean>(configPaths.settings.deleteLocalFile) || false
|
||||
const pasteText: string[] = []
|
||||
for (let i = 0; i < imgs.length; i++) {
|
||||
if (deleteLocalFile) {
|
||||
@@ -88,14 +89,14 @@ class GuiApi implements IGuiApi {
|
||||
const [pasteTextItem, shortUrl] = await pasteTemplate(
|
||||
pasteStyle,
|
||||
imgs[i],
|
||||
db.get(configPaths.settings.customLink),
|
||||
picgo.getConfig<string>(configPaths.settings.customLink),
|
||||
)
|
||||
imgs[i].shortUrl = shortUrl
|
||||
pasteText.push(pasteTextItem)
|
||||
const isShowResultNotification =
|
||||
db.get(configPaths.settings.uploadResultNotification) === undefined
|
||||
picgo.getConfig<boolean>(configPaths.settings.uploadResultNotification) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.uploadResultNotification)
|
||||
: !!picgo.getConfig<boolean>(configPaths.settings.uploadResultNotification)
|
||||
if (isShowResultNotification) {
|
||||
const notification = new Notification({
|
||||
title: $t('UPLOAD_SUCCEED'),
|
||||
@@ -161,12 +162,12 @@ class GuiApi implements IGuiApi {
|
||||
* get picgo config/data path
|
||||
*/
|
||||
async getConfigPath() {
|
||||
const currentConfigPath = dbPathChecker()
|
||||
const galleryDBPath = getGalleryDBPath().dbPath
|
||||
const currentConfigPath = appConfigPath()
|
||||
const galleryDBPathValue = galleryDBPath()
|
||||
return {
|
||||
defaultConfigPath,
|
||||
defaultConfigPath: defaultConfigPathF(),
|
||||
currentConfigPath,
|
||||
galleryDBPath,
|
||||
galleryDBPath: galleryDBPathValue,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user