Feature(custom): support secondary picbed upload

ISSUES CLOSED: #226
This commit is contained in:
Kuingsmile 2024-07-31 11:21:55 +08:00
parent c4aadfbd66
commit 8962a46e85
15 changed files with 257 additions and 73 deletions

View File

@ -25,6 +25,7 @@ NO_MORE_NOTICE: No More Notice
SHOW_DEVTOOLS: Show Devtools
FEEDBACK: Feedback
CURRENT_PICBED: Current Picbed
CURRENT_SECOND_PICBED: Current Second Picbed
START_WATCH_CLIPBOARD: Start Watch Clipboard
STOP_WATCH_CLIPBOARD: Stop Watch Clipboard
OPEN_TOOLBOX: Open Toolbox
@ -183,6 +184,8 @@ SETTINGS_TIMESTAMP_RENAME: Timestamp Rename
SETTINGS_ADVANCED_RENAME: Advanced Rename
SETTINGS_ADVANCED_RENAME_ENABLE: Enable Advanced Rename
SETTINGS_ADVANCED_RENAME_FORMAT: Advanced Rename Format
SETTINGS_ENABLE_SECOND_PICBED: Enable Second Picbed
SETTINGS_SET_SECOND_PICBED: Set Second Picbed
SETTINGS_OPEN_UPLOAD_TIPS: Open Upload Tips
SETTINGS_OPEN_UPLOAD_RESULT_TIPS: Open Upload Result Tips
SETTINGS_MINI_WINDOW_ON_TOP: Mini Window On Top

View File

@ -25,6 +25,7 @@ NO_MORE_NOTICE: 以后不再提醒
SHOW_DEVTOOLS: 打开开发者工具
FEEDBACK: 反馈问题
CURRENT_PICBED: 当前图床
CURRENT_SECOND_PICBED: 当前第二图床
START_WATCH_CLIPBOARD: 开始监听剪贴板
STOP_WATCH_CLIPBOARD: 停止监听剪贴板
OPEN_TOOLBOX: 打开修复工具箱
@ -185,6 +186,8 @@ SETTINGS_TIMESTAMP_RENAME: 时间戳重命名
SETTINGS_ADVANCED_RENAME: 高级重命名
SETTINGS_ADVANCED_RENAME_ENABLE: 开启高级重命名
SETTINGS_ADVANCED_RENAME_FORMAT: 重命名格式
SETTINGS_ENABLE_SECOND_PICBED: 启用第二图床同步上传
SETTINGS_SET_SECOND_PICBED: 设置第二图床
SETTINGS_OPEN_UPLOAD_TIPS: 开启上传进度提示
SETTINGS_OPEN_UPLOAD_RESULT_TIPS: 开启上传结果提示
SETTINGS_MINI_WINDOW_ON_TOP: Mini窗口置顶

View File

@ -25,6 +25,7 @@ NO_MORE_NOTICE: 以後不再提醒
SHOW_DEVTOOLS: 開啟開發者工具
FEEDBACK: 問題反饋
CURRENT_PICBED: 當前圖床
CURRENT_SECOND_PICBED: 當前第二圖床
START_WATCH_CLIPBOARD: 開始監聽剪貼簿
STOP_WATCH_CLIPBOARD: 停止監聽剪貼簿
OPEN_TOOLBOX: 開啟修復工具箱
@ -183,6 +184,8 @@ SETTINGS_TIMESTAMP_RENAME: 以時間戳命名
SETTINGS_ADVANCED_RENAME: 高級命名
SETTINGS_ADVANCED_RENAME_ENABLE: 啟用高級命名
SETTINGS_ADVANCED_RENAME_FORMAT: 高級命名格式
SETTINGS_ENABLE_SECOND_PICBED: 啟用第二圖床同步上傳
SETTINGS_SET_SECOND_PICBED: 設定第二圖床
SETTINGS_OPEN_UPLOAD_TIPS: 開啟上傳提示
SETTINGS_OPEN_UPLOAD_RESULT_TIPS: 開啟上傳結果提示
SETTINGS_MINI_WINDOW_ON_TOP: Mini視窗置頂

View File

@ -16,7 +16,7 @@ import db, { GalleryDB } from '@core/datastore'
import picgo from '@core/picgo'
import uploader from 'apis/app/uploader'
import { uploadClipboardFiles } from 'apis/app/uploader/apis'
import { handleSecondaryUpload, uploadClipboardFiles } from 'apis/app/uploader/apis'
import windowManager from 'apis/app/window/windowManager'
import { buildPicBedListMenu } from '~/events/remotes/menu'
@ -309,6 +309,7 @@ export function createTray(tooltip: string) {
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
const rawInput = cloneDeep(files)
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
await handleSecondaryUpload(trayWindow.webContents, files, 'tray')
const imgs = await uploader.setWebContents(trayWindow.webContents).upload(files)
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
if (imgs !== false) {

View File

@ -14,6 +14,7 @@ import pasteTemplate from '~/utils/pasteTemplate'
import { IPasteStyle, IWindowList } from '#/types/enum'
import { configPaths } from '#/utils/configPaths'
import { changeCurrentUploader } from '~/utils/handleUploaderConfig'
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
const useBuiltinClipboard =
@ -28,6 +29,7 @@ const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
}
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
await handleSecondaryUpload(undefined, undefined, 'clipboard')
const img = await handleClipboardUploading()
if (img !== false) {
if (img.length > 0) {
@ -84,6 +86,7 @@ export const uploadChoosedFiles = async (
): Promise<IStringKeyMap[]> => {
const input = files.map(item => item.path)
const rawInput = cloneDeep(input)
await handleSecondaryUpload(webContents, input)
const imgs = await uploader.setWebContents(webContents).upload(input)
const result = []
if (imgs !== false) {
@ -132,3 +135,65 @@ export const uploadChoosedFiles = async (
return []
}
}
export const handleSecondaryUpload = async (
webContents?: WebContents,
input?: string[],
uploadType: 'clipboard' | 'file' | 'tray' = 'file'
): Promise<void> => {
const enableSecondUploader = db.get(configPaths.settings.enableSecondUploader) || false
let currentPicBedType = ''
let currentPicBedConfig = {} as IStringKeyMap
let currentPicBedConfigId = ''
let needRestore = false
if (enableSecondUploader) {
const secondUploader = db.get(configPaths.picBed.secondUploader)
const secondUploaderConfig = db.get(configPaths.picBed.secondUploaderConfig)
const secondUploaderId = db.get(configPaths.picBed.secondUploaderId)
const currentPicBed = db.get('picBed') || ({} as IStringKeyMap)
currentPicBedType = currentPicBed.uploader || currentPicBed.current || 'smms'
currentPicBedConfig = currentPicBed[currentPicBedType] || ({} as IStringKeyMap)
currentPicBedConfigId = currentPicBedConfig._id
if (
secondUploader === currentPicBedType &&
secondUploaderConfig._configName === currentPicBedConfig._configName &&
secondUploaderId === currentPicBedConfigId
) {
picgo.log.info('second uploader is the same as current uploader')
} else {
needRestore = true
let secondImgs: ImgInfo[] | false = false
changeCurrentUploader(secondUploader, secondUploaderConfig, secondUploaderId)
if (uploadType === 'clipboard') {
secondImgs = await handleClipboardUploading()
} else {
secondImgs = await uploader.setWebContents(webContents!).upload(input)
}
if (secondImgs !== false) {
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
if (uploadType === 'clipboard') {
if (secondImgs.length > 0) {
await GalleryDB.getInstance().insert(secondImgs[0])
trayWindow?.webContents?.send('clipboardFiles', [])
trayWindow?.webContents?.send('uploadFiles', secondImgs)
}
} else {
for (let i = 0; i < secondImgs.length; i++) {
await GalleryDB.getInstance().insert(secondImgs[i])
}
if (uploadType === 'tray') {
trayWindow?.webContents?.send('dragFiles', secondImgs)
} else {
trayWindow?.webContents?.send('uploadFiles', secondImgs)
}
}
if (windowManager.has(IWindowList.SETTING_WINDOW) && uploadType !== 'tray') {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
}
}
}
}
if (needRestore) {
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
}
}

View File

@ -17,6 +17,7 @@ import pasteTemplate from '~/utils/pasteTemplate'
import { SHOW_INPUT_BOX } from '#/events/constants'
import { IPasteStyle } from '#/types/enum'
import { configPaths } from '#/utils/configPaths'
import { handleSecondaryUpload } from '../app/uploader/apis'
// Cross-process support may be required in the future
class GuiApi implements IGuiApi {
@ -78,6 +79,7 @@ class GuiApi implements IGuiApi {
this.windowId = await getWindowId()
const webContents = this.getWebcontentsByWindowId(this.windowId)
const rawInput = cloneDeep(input)
await handleSecondaryUpload(webContents!, input)
const imgs = await uploader.setWebContents(webContents!).upload(input)
if (imgs !== false) {
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN

View File

@ -13,7 +13,7 @@ import { T } from '~/i18n'
import clipboardPoll from '~/utils/clipboardPoll'
import { setTrayToolTip } from '~/utils/common'
import getPicBeds from '~/utils/getPicBeds'
import { changeCurrentUploader } from '~/utils/handleUploaderConfig'
import { changeCurrentUploader, changeSecondUploader } from '~/utils/handleUploaderConfig'
import {
PICGO_CONFIG_PLUGIN,
@ -139,6 +139,59 @@ const buildMainPageMenu = (win: BrowserWindow) => {
return Menu.buildFromTemplate(template)
}
const buildSecondPicBedMenu = () => {
const picBeds = getPicBeds()
const secondUploader = picgo.getConfig(configPaths.picBed.secondUploader)
const defaultSecondUploaderId = picgo.getConfig(configPaths.picBed.secondUploaderId)
const currentPicBedName = picBeds.find(item => item.type === secondUploader)?.name
const picBedConfigList = picgo.getConfig<IUploaderConfig>('uploader')
const currentPicBedMenuItem = [
{
label: `${T('CURRENT_SECOND_PICBED')} - ${currentPicBedName || 'None'}`,
enabled: false
},
{
type: 'separator'
}
]
let submenu = picBeds
.filter(item => item.visible)
.map(item => {
const configList = picBedConfigList?.[item.type]?.configList
const hasSubmenu = !!configList
return {
label: item.name,
type: !hasSubmenu ? 'checkbox' : undefined,
checked: !hasSubmenu ? secondUploader === item.type : undefined,
submenu: hasSubmenu
? configList.map(config => {
return {
label: config._configName || 'Default',
// if only one config, use checkbox, or radio will checked as default
// see: https://github.com/electron/electron/issues/21292
type: 'checkbox',
checked: config._id === defaultSecondUploaderId && item.type === secondUploader,
click: function () {
changeSecondUploader(item.type, config, config._id)
}
}
})
: undefined,
click: !hasSubmenu
? function () {
picgo.saveConfig({
[configPaths.picBed.secondUploader]: item.type
})
}
: undefined
}
})
// @ts-expect-error submenu type
submenu = currentPicBedMenuItem.concat(submenu)
// @ts-expect-error submenu type
return Menu.buildFromTemplate(submenu)
}
const buildPicBedListMenu = () => {
const picBeds = getPicBeds()
const currentPicBed = picgo.getConfig(configPaths.picBed.uploader)
@ -339,4 +392,4 @@ const buildPluginPageMenu = (plugin: IPicGoPlugin) => {
return Menu.buildFromTemplate(menu)
}
export { buildMiniPageMenu, buildMainPageMenu, buildPicBedListMenu, buildPluginPageMenu }
export { buildMiniPageMenu, buildMainPageMenu, buildPicBedListMenu, buildPluginPageMenu, buildSecondPicBedMenu }

View File

@ -15,17 +15,12 @@ import { manageRouter } from '~/events/rpc/routes/manage'
import { IRPCActionType, IRPCType } from '#/types/enum'
import { RPC_ACTIONS, RPC_ACTIONS_INVOKE } from '#/events/constants'
const isDevelopment = process.env.NODE_ENV !== 'production'
class RPCServer implements IRPCServer {
private routes: IRPCRoutes = new Map()
private routesWithResponse: IRPCRoutes = new Map()
private rpcEventHandler = async (event: IpcMainEvent, action: IRPCActionType, args: any[]) => {
try {
if (isDevelopment) {
console.log(`action: ${action} args: ${JSON.stringify(args)}`)
}
const route = this.routes.get(action)
await route?.handler?.(event, args)
} catch (e: any) {
@ -35,9 +30,6 @@ class RPCServer implements IRPCServer {
private rpcEventHandlerWithResponse = async (event: IpcMainInvokeEvent, action: IRPCActionType, args: any[]) => {
try {
if (isDevelopment) {
console.log(`action: ${action} args: ${JSON.stringify(args)}`)
}
const route = this.routesWithResponse.get(action)
return await route?.handler?.(event, args)
} catch (e: any) {

View File

@ -2,7 +2,13 @@ import { app, BrowserWindow } from 'electron'
import windowManager from 'apis/app/window/windowManager'
import { buildMainPageMenu, buildMiniPageMenu, buildPicBedListMenu, buildPluginPageMenu } from '~/events/remotes/menu'
import {
buildMainPageMenu,
buildMiniPageMenu,
buildPicBedListMenu,
buildPluginPageMenu,
buildSecondPicBedMenu
} from '~/events/remotes/menu'
import { openMiniWindow } from '~/utils/windowHelper'
import { IRPCActionType, IWindowList } from '#/types/enum'
@ -83,6 +89,16 @@ export default [
})
}
},
{
action: IRPCActionType.SHOW_SECOND_UPLOADER_MENU,
handler: async () => {
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
const menu = buildSecondPicBedMenu()
menu.popup({
window
})
}
},
{
action: IRPCActionType.SHOW_PLUGIN_PAGE_MENU,
handler: async (_: IIPCEvent, args: [plugin: IPicGoPlugin]) => {

View File

@ -56,6 +56,25 @@ export const getPicBedConfig = (type: string) => {
}
}
export const changeSecondUploader = (type: string, config?: IStringKeyMap, id?: string) => {
if (!type) {
return
}
if (id) {
picgo.saveConfig({
[configPaths.picBed.secondUploaderId]: id
})
}
if (config) {
picgo.saveConfig({
[configPaths.picBed.secondUploaderConfig]: config
})
}
picgo.saveConfig({
[configPaths.picBed.secondUploader]: type
})
}
export const changeCurrentUploader = (type: string, config?: IStringKeyMap, id?: string) => {
if (!type) {
return

View File

@ -215,6 +215,18 @@
<el-option v-for="item in picBedGlobal" :key="item.type" :label="item.name" :value="item.type" />
</el-select>
</el-form-item>
<el-form-item :label="$T('SETTINGS_ENABLE_SECOND_PICBED')">
<el-switch
v-model="formOfSetting.enableSecondUploader"
:active-text="$T('SETTINGS_OPEN')"
:inactive-text="$T('SETTINGS_CLOSE')"
/>
</el-form-item>
<el-form-item :label="$T('SETTINGS_SET_SECOND_PICBED')">
<el-button type="primary" round size="small" @click="handleChangeSecondPicBed">
{{ $T('SETTINGS_CLICK_TO_SET') }}
</el-button>
</el-form-item>
<el-form-item :label="$T('SETTINGS_SYNC_DELETE_CLOUD')">
<el-switch
v-model="formOfSetting.deleteCloudFile"
@ -1048,7 +1060,8 @@ const formOfSetting = ref<ISettingForm>({
registry: '',
proxy: '',
mainWindowWidth: 1200,
mainWindowHeight: 800
mainWindowHeight: 800,
enableSecondUploader: false
})
const proxy = ref('')
@ -1080,7 +1093,8 @@ const autoWatchKeys = [
'proxy',
'autoCopy',
'encodeOutputURL',
'useShortUrl'
'useShortUrl',
'enableSecondUploader'
]
const addWatch = () => {
@ -1259,6 +1273,10 @@ function initArray(arrayT: string | string[], defaultValue: string[]) {
return arrayT
}
async function handleChangeSecondPicBed() {
sendRPC(IRPCActionType.SHOW_SECOND_UPLOADER_MENU)
}
function openFile(file: string) {
sendRPC(IRPCActionType.PICLIST_OPEN_FILE, file)
}

View File

@ -96,6 +96,7 @@ export enum IRPCActionType {
SHOW_MINI_PAGE_MENU = 'SHOW_MINI_PAGE_MENU',
SHOW_MAIN_PAGE_MENU = 'SHOW_MAIN_PAGE_MENU',
SHOW_UPLOAD_PAGE_MENU = 'SHOW_UPLOAD_PAGE_MENU',
SHOW_SECOND_UPLOADER_MENU = 'SHOW_SECOND_UPLOADER_MENU',
SHOW_PLUGIN_PAGE_MENU = 'SHOW_PLUGIN_PAGE_MENU',
SET_MINI_WINDOW_POS = 'SET_MINI_WINDOW_POS',
MINI_WINDOW_ON_TOP = 'MINI_WINDOW_ON_TOP',

View File

@ -26,6 +26,7 @@ interface ILocales {
SHOW_DEVTOOLS: string
FEEDBACK: string
CURRENT_PICBED: string
CURRENT_SECOND_PICBED: string
START_WATCH_CLIPBOARD: string
STOP_WATCH_CLIPBOARD: string
OPEN_TOOLBOX: string
@ -178,6 +179,8 @@ interface ILocales {
SETTINGS_ADVANCED_RENAME: string
SETTINGS_ADVANCED_RENAME_ENABLE: string
SETTINGS_ADVANCED_RENAME_FORMAT: string
SETTINGS_ENABLE_SECOND_PICBED: string
SETTINGS_SET_SECOND_PICBED: string
SETTINGS_OPEN_UPLOAD_TIPS: string
SETTINGS_OPEN_UPLOAD_RESULT_TIPS: string
SETTINGS_MINI_WINDOW_ON_TOP: string

View File

@ -1,58 +1,59 @@
interface ISettingForm {
showUpdateTip: boolean
autoStart: boolean
rename: boolean
autoRename: boolean
uploadNotification: boolean
uploadResultNotification: boolean
miniWindowOntop: boolean
autoCloseMiniWindow: boolean
autoCloseMainWindow: boolean
logLevel: string[]
autoCopy: boolean
useBuiltinClipboard: boolean
logFileSizeLimit: number
deleteCloudFile: boolean
isCustomMiniIcon: boolean
customMiniIcon: string
isHideDock: boolean
autoImport: boolean
autoImportPicBed: string[]
encodeOutputURL: boolean
isAutoListenClipboard: boolean
useShortUrl: boolean
shortUrlServer: string
c1nToken: string
yourlsDomain: string
yourlsSignature: string
cfWorkerHost: string
deleteLocalFile: boolean
serverKey: string
aesPassword: string
enableWebServer: boolean
webServerHost: string
webServerPort: number
webServerPath: string
registry: string
proxy: string
mainWindowWidth: number
mainWindowHeight: number
}
interface IShortKeyMap {
[propName: string]: string
}
interface IToolboxItem {
title: string
status: import('#/types/enum').IToolboxItemCheckStatus
msg?: string
value?: any // for handler
hasNoFixMethod?: boolean
handler?: (value: any) => Promise<void> | void
handlerText?: string
}
type IToolboxMap = {
[id in import('#/types/enum').IToolboxItemType]: IToolboxItem
}
interface ISettingForm {
showUpdateTip: boolean
autoStart: boolean
rename: boolean
autoRename: boolean
uploadNotification: boolean
uploadResultNotification: boolean
miniWindowOntop: boolean
autoCloseMiniWindow: boolean
autoCloseMainWindow: boolean
logLevel: string[]
autoCopy: boolean
useBuiltinClipboard: boolean
logFileSizeLimit: number
deleteCloudFile: boolean
isCustomMiniIcon: boolean
customMiniIcon: string
isHideDock: boolean
autoImport: boolean
autoImportPicBed: string[]
encodeOutputURL: boolean
isAutoListenClipboard: boolean
useShortUrl: boolean
shortUrlServer: string
c1nToken: string
yourlsDomain: string
yourlsSignature: string
cfWorkerHost: string
deleteLocalFile: boolean
serverKey: string
aesPassword: string
enableWebServer: boolean
webServerHost: string
webServerPort: number
webServerPath: string
registry: string
proxy: string
mainWindowWidth: number
mainWindowHeight: number
enableSecondUploader: boolean
}
interface IShortKeyMap {
[propName: string]: string
}
interface IToolboxItem {
title: string
status: import('#/types/enum').IToolboxItemCheckStatus
msg?: string
value?: any // for handler
hasNoFixMethod?: boolean
handler?: (value: any) => Promise<void> | void
handlerText?: string
}
type IToolboxMap = {
[id in import('#/types/enum').IToolboxItemType]: IToolboxItem
}

View File

@ -107,6 +107,9 @@ export const configPaths = {
picBed: {
current: 'picBed.current',
uploader: 'picBed.uploader',
secondUploader: 'picBed.secondUploader',
secondUploaderId: 'picBed.secondUploaderId',
secondUploaderConfig: 'picBed.secondUploaderConfig',
proxy: 'picBed.proxy',
transformer: 'picBed.transformer',
list: 'picBed.list'
@ -165,7 +168,8 @@ export const configPaths = {
useBuiltinClipboard: 'settings.useBuiltinClipboard',
autoStart: 'settings.autoStart',
autoImport: 'settings.autoImport',
autoImportPicBed: 'settings.autoImportPicBed'
autoImportPicBed: 'settings.autoImportPicBed',
enableSecondUploader: 'settings.enableSecondUploader'
},
needReload: 'needReload',
picgoPlugins: 'picgoPlugins',