Feature(custom): optimize second uploader

ISSUES CLOSED: #241,#238
This commit is contained in:
Kuingsmile 2024-09-11 14:41:28 +08:00
parent 07cf63a41e
commit 0565ce34cd
7 changed files with 124 additions and 20 deletions

View File

@ -67,7 +67,7 @@
"multer": "^1.4.5-lts.1",
"node-ssh-no-cpu-features": "^2.0.0",
"nodejs-file-downloader": "^4.12.1",
"piclist": "^1.9.4",
"piclist": "^1.9.5",
"pinia": "^2.1.7",
"pinia-plugin-persistedstate": "^3.2.1",
"proxy-agent": "^5.0.0",

View File

@ -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[] = []

View File

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

View File

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

View File

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

View File

@ -428,9 +428,7 @@ function handleDetectShiftKey(event: KeyboardEvent) {
}
const filterList = computed(() => {
const start = new Date().getTime()
const res = getGallery()
console.log(`filterList: ${new Date().getTime() - start}ms`)
return res
})
@ -487,9 +485,7 @@ function getGallery(): IGalleryItem[] {
}
async function updateGallery() {
const start = new Date().getTime()
images.value = (await $$db.get({ orderBy: 'desc' }))!.data
console.log(`updateGallery: ${new Date().getTime() - start}ms`)
}
watch(

View File

@ -11956,10 +11956,10 @@ performance-now@^2.1.0:
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==
piclist@^1.9.4:
version "1.9.4"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.9.4.tgz#9bc3b25a224071c9a99da2b0bcb5a848a5481f84"
integrity sha512-DgmarGQum+3M06o/4JPgLx2tbKh/rCL/GEBKPwTjOKrF2P2Aa3eV8xkqpEI6VqQQp1cEMB4jk424lcV790vvTw==
piclist@^1.9.5:
version "1.9.5"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.9.5.tgz#699381c743a91a99f89cd4778e70a44700643e79"
integrity sha512-qT+dOxXr8lpSypprmXVmmUX7piM46vOYA5U5RTnWkNINUo3rYAKTdytLFhWzON7MKYV6WRFq7PJr7tn82SEgtQ==
dependencies:
"@aws-sdk/client-s3" "3.421.0"
"@aws-sdk/lib-storage" "3.421.0"