mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-07 16:50:10 +08:00
✨ Feature(custom): optimize second uploader
ISSUES CLOSED: #241,#238
This commit is contained in:
@@ -309,8 +309,16 @@ 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 { needRestore, ctx } = await handleSecondaryUpload(trayWindow.webContents, files, 'tray')
|
||||
let imgs: ImgInfo[] | false = false
|
||||
if (needRestore) {
|
||||
const res = await uploader
|
||||
.setWebContents(trayWindow.webContents)
|
||||
.uploadReturnCtx(ctx ? ctx.processedInput : files, true)
|
||||
imgs = res ? res.output : false
|
||||
} else {
|
||||
imgs = await uploader.setWebContents(trayWindow.webContents).upload(files)
|
||||
}
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
if (imgs !== false) {
|
||||
const pasteText: string[] = []
|
||||
|
||||
@@ -15,6 +15,7 @@ import pasteTemplate from '~/utils/pasteTemplate'
|
||||
import { IPasteStyle, IWindowList } from '#/types/enum'
|
||||
import { configPaths } from '#/utils/configPaths'
|
||||
import { changeCurrentUploader } from '~/utils/handleUploaderConfig'
|
||||
import { IPicGo } from 'piclist'
|
||||
|
||||
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
||||
const useBuiltinClipboard =
|
||||
@@ -28,9 +29,27 @@ const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
||||
return await uploader.setWebContents(win!.webContents).upload()
|
||||
}
|
||||
|
||||
const handleClipboardUploadingReturnCtx = async (img?: IUploadOption, skipProcess = false): Promise<false | IPicGo> => {
|
||||
const useBuiltinClipboard =
|
||||
db.get(configPaths.settings.useBuiltinClipboard) === undefined
|
||||
? true
|
||||
: !!db.get(configPaths.settings.useBuiltinClipboard)
|
||||
const win = windowManager.getAvailableWindow()
|
||||
if (useBuiltinClipboard) {
|
||||
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboardReturnCtx(img, skipProcess)
|
||||
}
|
||||
return await uploader.setWebContents(win!.webContents).uploadReturnCtx(img, skipProcess)
|
||||
}
|
||||
|
||||
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
await handleSecondaryUpload(undefined, undefined, 'clipboard')
|
||||
const img = await handleClipboardUploading()
|
||||
const { needRestore, ctx } = await handleSecondaryUpload(undefined, undefined, 'clipboard')
|
||||
let img: ImgInfo[] | false = false
|
||||
if (needRestore) {
|
||||
const res = await handleClipboardUploadingReturnCtx(ctx ? ctx.processedInput : undefined, true)
|
||||
img = res ? res.output : false
|
||||
} else {
|
||||
img = await handleClipboardUploading()
|
||||
}
|
||||
if (img !== false) {
|
||||
if (img.length > 0) {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
@@ -86,8 +105,14 @@ 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 { needRestore, ctx } = await handleSecondaryUpload(webContents, input)
|
||||
let imgs: ImgInfo[] | false = false
|
||||
if (needRestore) {
|
||||
const res = await uploader.setWebContents(webContents).uploadReturnCtx(ctx ? ctx.processedInput : input, true)
|
||||
imgs = res ? res.output : false
|
||||
} else {
|
||||
imgs = await uploader.setWebContents(webContents).upload(input)
|
||||
}
|
||||
const result = []
|
||||
if (imgs !== false) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
@@ -140,12 +165,13 @@ export const handleSecondaryUpload = async (
|
||||
webContents?: WebContents,
|
||||
input?: string[],
|
||||
uploadType: 'clipboard' | 'file' | 'tray' = 'file'
|
||||
): Promise<void> => {
|
||||
): Promise<{ needRestore: boolean; ctx: IPicGo | false }> => {
|
||||
const enableSecondUploader = db.get(configPaths.settings.enableSecondUploader) || false
|
||||
let currentPicBedType = ''
|
||||
let currentPicBedConfig = {} as IStringKeyMap
|
||||
let currentPicBedConfigId = ''
|
||||
let needRestore = false
|
||||
let ctx: IPicGo | false = false
|
||||
if (enableSecondUploader) {
|
||||
const secondUploader = db.get(configPaths.picBed.secondUploader)
|
||||
const secondUploaderConfig = db.get(configPaths.picBed.secondUploaderConfig)
|
||||
@@ -165,10 +191,11 @@ export const handleSecondaryUpload = async (
|
||||
let secondImgs: ImgInfo[] | false = false
|
||||
changeCurrentUploader(secondUploader, secondUploaderConfig, secondUploaderId)
|
||||
if (uploadType === 'clipboard') {
|
||||
secondImgs = await handleClipboardUploading()
|
||||
ctx = await handleClipboardUploadingReturnCtx(undefined)
|
||||
} else {
|
||||
secondImgs = await uploader.setWebContents(webContents!).upload(input)
|
||||
ctx = await uploader.setWebContents(webContents!).uploadReturnCtx(input)
|
||||
}
|
||||
secondImgs = ctx ? ctx.output : false
|
||||
if (secondImgs !== false) {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
if (uploadType === 'clipboard') {
|
||||
@@ -196,4 +223,8 @@ export const handleSecondaryUpload = async (
|
||||
if (needRestore) {
|
||||
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
|
||||
}
|
||||
return {
|
||||
needRestore,
|
||||
ctx
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,69 @@ class Uploader {
|
||||
}
|
||||
}
|
||||
|
||||
async uploadWithBuildInClipboardReturnCtx(img?: IUploadOption, skipProcess = false): Promise<IPicGo | false> {
|
||||
let filePath = ''
|
||||
try {
|
||||
const imgPath = getClipboardFilePath()
|
||||
if (!imgPath) {
|
||||
const nativeImage = clipboard.readImage()
|
||||
if (nativeImage.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
const buffer = nativeImage.toPNG()
|
||||
const baseDir = picgo.baseDir
|
||||
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
|
||||
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
||||
await writeFile(filePath, buffer)
|
||||
return await this.uploadReturnCtx(img ?? [filePath], skipProcess)
|
||||
} else {
|
||||
return await this.uploadReturnCtx(img ?? [imgPath], skipProcess)
|
||||
}
|
||||
} catch (e: any) {
|
||||
logger.error(e)
|
||||
return false
|
||||
} finally {
|
||||
if (filePath) {
|
||||
fs.remove(filePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async uploadReturnCtx(img?: IUploadOption, skipProcess = false): Promise<IPicGo | false> {
|
||||
try {
|
||||
const startTime = Date.now()
|
||||
const ctx = await picgo.uploadReturnCtx(img, skipProcess)
|
||||
if (Array.isArray(ctx.output) && ctx.output.some((item: ImgInfo) => item.imgUrl)) {
|
||||
if (this.webContents) {
|
||||
handleTalkingData(this.webContents, {
|
||||
fromClipboard: !img,
|
||||
type: db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms',
|
||||
count: img ? img.length : 1,
|
||||
duration: Date.now() - startTime
|
||||
} as IAnalyticsData)
|
||||
}
|
||||
ctx.output.forEach((item: ImgInfo) => {
|
||||
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
|
||||
})
|
||||
return ctx
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
async upload(img?: IUploadOption): Promise<ImgInfo[] | false> {
|
||||
try {
|
||||
const startTime = Date.now()
|
||||
|
||||
@@ -79,8 +79,14 @@ 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)
|
||||
const { needRestore, ctx } = await handleSecondaryUpload(webContents!, input)
|
||||
let imgs: ImgInfo[] | false = false
|
||||
if (needRestore) {
|
||||
const res = await uploader.setWebContents(webContents!).uploadReturnCtx(ctx ? ctx.processedInput : input, true)
|
||||
imgs = res ? res.output : false
|
||||
} else {
|
||||
imgs = await uploader.setWebContents(webContents!).upload(input)
|
||||
}
|
||||
if (imgs !== false) {
|
||||
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
|
||||
const deleteLocalFile = db.get(configPaths.settings.deleteLocalFile) || false
|
||||
|
||||
Reference in New Issue
Block a user