mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 14:48:13 -05:00
🐛 Fix: windows upload clipboard file with builtin-clipboard not work
This commit is contained in:
parent
20e38f4eb9
commit
7b50ba7ddb
@ -12,7 +12,7 @@ import windowManager from 'apis/app/window/windowManager'
|
|||||||
import { IWindowList } from '#/types/enum'
|
import { IWindowList } from '#/types/enum'
|
||||||
import util from 'util'
|
import util from 'util'
|
||||||
import { IPicGo } from 'picgo'
|
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 { RENAME_FILE_NAME, TALKING_DATA_EVENT } from '~/universal/events/constants'
|
||||||
import logger from '@core/picgo/logger'
|
import logger from '@core/picgo/logger'
|
||||||
import { T } from '~/main/i18n'
|
import { T } from '~/main/i18n'
|
||||||
@ -121,6 +121,8 @@ class Uploader {
|
|||||||
async uploadWithBuildInClipboard (): Promise<ImgInfo[]|false> {
|
async uploadWithBuildInClipboard (): Promise<ImgInfo[]|false> {
|
||||||
let filePath = ''
|
let filePath = ''
|
||||||
try {
|
try {
|
||||||
|
const imgPath = getClipboardFilePath()
|
||||||
|
if (!imgPath) {
|
||||||
const nativeImage = clipboard.readImage()
|
const nativeImage = clipboard.readImage()
|
||||||
if (nativeImage.isEmpty()) {
|
if (nativeImage.isEmpty()) {
|
||||||
return false
|
return false
|
||||||
@ -131,6 +133,9 @@ class Uploader {
|
|||||||
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
filePath = path.join(baseDir, CLIPBOARD_IMAGE_FOLDER, fileName)
|
||||||
await writeFile(filePath, buffer)
|
await writeFile(filePath, buffer)
|
||||||
return await this.upload([filePath])
|
return await this.upload([filePath])
|
||||||
|
} else {
|
||||||
|
return await this.upload([imgPath])
|
||||||
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return false
|
return false
|
||||||
|
@ -48,7 +48,8 @@ export default {
|
|||||||
// from macOS tray
|
// from macOS tray
|
||||||
ipcMain.on('uploadClipboardFiles', async () => {
|
ipcMain.on('uploadClipboardFiles', async () => {
|
||||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
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) {
|
if (img !== false) {
|
||||||
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
||||||
handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink')))
|
handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink')))
|
||||||
|
@ -86,3 +86,29 @@ export const ensureFilePath = (filePath: string, prefix = 'file://'): string =>
|
|||||||
}
|
}
|
||||||
return ''
|
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 ''
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user