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

@@ -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)
}
}