Skip to content

Commit 7b50ba7

Browse files
committed
🐛 Fix: windows upload clipboard file with builtin-clipboard not work
1 parent 20e38f4 commit 7b50ba7

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/main/apis/app/uploader/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import windowManager from 'apis/app/window/windowManager'
1212
import { IWindowList } from '#/types/enum'
1313
import util from 'util'
1414
import { IPicGo } from 'picgo'
15-
import { showNotification, calcDurationRange } from '~/main/utils/common'
15+
import { showNotification, calcDurationRange, getClipboardFilePath } from '~/main/utils/common'
1616
import { RENAME_FILE_NAME, TALKING_DATA_EVENT } from '~/universal/events/constants'
1717
import logger from '@core/picgo/logger'
1818
import { T } from '~/main/i18n'
@@ -121,16 +121,21 @@ class Uploader {
121121
async uploadWithBuildInClipboard (): Promise<ImgInfo[]|false> {
122122
let filePath = ''
123123
try {
124-
const nativeImage = clipboard.readImage()
125-
if (nativeImage.isEmpty()) {
126-
return false
124+
const imgPath = getClipboardFilePath()
125+
if (!imgPath) {
126+
const nativeImage = clipboard.readImage()
127+
if (nativeImage.isEmpty()) {
128+
return false
129+
}
130+
const buffer = nativeImage.toPNG()
131+
const baseDir = picgo.baseDir
132+
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
133+
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
134+
await writeFile(filePath, buffer)
135+
return await this.upload([filePath])
136+
} else {
137+
return await this.upload([imgPath])
127138
}
128-
const buffer = nativeImage.toPNG()
129-
const baseDir = picgo.baseDir
130-
const fileName = `${dayjs().format('YYYYMMDDHHmmSSS')}.png`
131-
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
132-
await writeFile(filePath, buffer)
133-
return await this.upload([filePath])
134139
} catch (e: any) {
135140
logger.error(e)
136141
return false

src/main/events/ipcList.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export default {
4848
// from macOS tray
4949
ipcMain.on('uploadClipboardFiles', async () => {
5050
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
51-
const img = await uploader.setWebContents(trayWindow.webContents).upload()
51+
// macOS use builtin clipboard is OK
52+
const img = await uploader.setWebContents(trayWindow.webContents).uploadWithBuildInClipboard()
5253
if (img !== false) {
5354
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
5455
handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink')))

src/main/utils/common.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,29 @@ export const ensureFilePath = (filePath: string, prefix = 'file://'): string =>
8686
}
8787
return ''
8888
}
89+
90+
/**
91+
* for builtin clipboard to get image path from clipboard
92+
* @returns
93+
*/
94+
export const getClipboardFilePath = (): string => {
95+
// TODO: linux support
96+
const img = clipboard.readImage()
97+
if (img.isEmpty()) {
98+
if (process.platform === 'win32') {
99+
const imgPath = clipboard.readBuffer('FileNameW')?.toString('ucs2')?.replace(RegExp(String.fromCharCode(0), 'g'), '')
100+
if (imgPath) {
101+
return imgPath
102+
}
103+
}
104+
} else {
105+
if (process.platform === 'darwin') {
106+
let imgPath = clipboard.read('public.file-url') // will get file://xxx/xxx
107+
imgPath = ensureFilePath(imgPath)
108+
if (imgPath) {
109+
return imgPath.replace('file://', '')
110+
}
111+
}
112+
}
113+
return ''
114+
}

0 commit comments

Comments
 (0)