mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-22 08:03:48 +08:00
✨ Feature(custom): support upload absolute file path text in clipboard
ISSUES CLOSED: #538
This commit is contained in:
46
src/main/utils/clipboardFilePath.ts
Normal file
46
src/main/utils/clipboardFilePath.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import fs from 'fs-extra'
|
||||
|
||||
const unwrapClipboardPathText = (text: string): string => {
|
||||
if ((text.startsWith('"') && text.endsWith('"')) || (text.startsWith("'") && text.endsWith("'"))) {
|
||||
return text.slice(1, -1).trim()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
const normalizeClipboardPathText = (text: string): string => {
|
||||
const lines = text
|
||||
.trim()
|
||||
.split(/\r?\n/)
|
||||
.map(line => line.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
if (lines.length !== 1) return ''
|
||||
|
||||
const filePath = unwrapClipboardPathText(lines[0])
|
||||
if (!filePath) return ''
|
||||
|
||||
if (/^file:\/\//i.test(filePath)) {
|
||||
try {
|
||||
return fileURLToPath(filePath)
|
||||
} catch (_e) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
return filePath
|
||||
}
|
||||
|
||||
export const getClipboardTextFilePath = (text: string): string => {
|
||||
const filePath = normalizeClipboardPathText(text)
|
||||
if (!filePath || !path.isAbsolute(filePath)) return ''
|
||||
|
||||
try {
|
||||
const stats = fs.statSync(filePath)
|
||||
return stats.isFile() ? path.normalize(filePath) : ''
|
||||
} catch (_e) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import fs from 'fs-extra'
|
||||
import { IPicGo } from 'piclist'
|
||||
import { isProxy, isRef, toRaw, unref } from 'vue'
|
||||
|
||||
import { getClipboardTextFilePath } from '~/utils/clipboardFilePath'
|
||||
import { configPaths } from '~/utils/configPaths'
|
||||
import { IShortUrlServer } from '~/utils/enum'
|
||||
|
||||
@@ -147,7 +148,11 @@ export const getClipboardFilePath = (): string => {
|
||||
.readBuffer('FileNameW')
|
||||
?.toString('ucs2')
|
||||
?.replace(RegExp(String.fromCharCode(0), 'g'), '')
|
||||
return imgPath || ''
|
||||
if (imgPath) return imgPath
|
||||
}
|
||||
|
||||
if (img.isEmpty()) {
|
||||
return getClipboardTextFilePath(clipboard.readText())
|
||||
}
|
||||
|
||||
return ''
|
||||
|
||||
Reference in New Issue
Block a user