diff --git a/src/main/apis/app/uploader/index.ts b/src/main/apis/app/uploader/index.ts index dc117c4..667bcd6 100644 --- a/src/main/apis/app/uploader/index.ts +++ b/src/main/apis/app/uploader/index.ts @@ -12,7 +12,7 @@ import windowManager from 'apis/app/window/windowManager' import { IWindowList } from '#/types/enum' import util from 'util' import { IPicGo } from 'picgo' -import { showNotification, calcDurationRange } from '~/main/utils/common' +import { showNotification, calcDurationRange, getClipboardFilePath } from '~/main/utils/common' import { RENAME_FILE_NAME, TALKING_DATA_EVENT } from '~/universal/events/constants' import logger from '@core/picgo/logger' import { T } from '~/main/i18n' @@ -121,16 +121,21 @@ class Uploader { async uploadWithBuildInClipboard (): Promise { let filePath = '' try { - const nativeImage = clipboard.readImage() - if (nativeImage.isEmpty()) { - return false + 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.upload([filePath]) + } else { + return await this.upload([imgPath]) } - 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.upload([filePath]) } catch (e: any) { logger.error(e) return false diff --git a/src/main/events/ipcList.ts b/src/main/events/ipcList.ts index f354acb..8a69261 100644 --- a/src/main/events/ipcList.ts +++ b/src/main/events/ipcList.ts @@ -48,7 +48,8 @@ export default { // from macOS tray ipcMain.on('uploadClipboardFiles', async () => { const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)! - const img = await uploader.setWebContents(trayWindow.webContents).upload() + // macOS use builtin clipboard is OK + const img = await uploader.setWebContents(trayWindow.webContents).uploadWithBuildInClipboard() if (img !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink'))) diff --git a/src/main/utils/common.ts b/src/main/utils/common.ts index b3c9865..04dd6a5 100644 --- a/src/main/utils/common.ts +++ b/src/main/utils/common.ts @@ -86,3 +86,29 @@ export const ensureFilePath = (filePath: string, prefix = 'file://'): string => } return '' } + +/** + * for builtin clipboard to get image path from clipboard + * @returns + */ +export const getClipboardFilePath = (): string => { + // TODO: linux support + const img = clipboard.readImage() + if (img.isEmpty()) { + if (process.platform === 'win32') { + const imgPath = clipboard.readBuffer('FileNameW')?.toString('ucs2')?.replace(RegExp(String.fromCharCode(0), 'g'), '') + if (imgPath) { + return imgPath + } + } + } else { + if (process.platform === 'darwin') { + let imgPath = clipboard.read('public.file-url') // will get file://xxx/xxx + imgPath = ensureFilePath(imgPath) + if (imgPath) { + return imgPath.replace('file://', '') + } + } + } + return '' +}