Feature(custom): support upload absolute file path text in clipboard

ISSUES CLOSED: #538
This commit is contained in:
Kuingsmile
2026-06-12 00:34:05 -07:00
parent 1eca4d7ea2
commit d3458e679a
4 changed files with 104 additions and 2 deletions

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

View File

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