🐛 Fix: windows upload clipboard file with builtin-clipboard not work

This commit is contained in:
PiEgg
2022-08-25 22:46:21 +08:00
parent 20e38f4eb9
commit 7b50ba7ddb
3 changed files with 43 additions and 11 deletions

View File

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