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,51 @@
import os from 'node:os'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import fs from 'fs-extra'
import { afterEach, describe, expect, it } from 'vitest'
import { getClipboardTextFilePath } from '../src/main/utils/clipboardFilePath'
const tempDirs: string[] = []
const createTempFile = (fileName = 'clipboard image.png') => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'piclist-clipboard-'))
const filePath = path.join(dir, fileName)
tempDirs.push(dir)
fs.writeFileSync(filePath, 'test')
return filePath
}
afterEach(() => {
tempDirs.splice(0).forEach(dir => fs.removeSync(dir))
})
describe('getClipboardTextFilePath', () => {
it('returns an existing absolute file path from clipboard text', () => {
const filePath = createTempFile()
expect(getClipboardTextFilePath(` ${filePath} `)).toBe(path.normalize(filePath))
})
it('unwraps quoted file paths', () => {
const filePath = createTempFile()
expect(getClipboardTextFilePath(`"${filePath}"`)).toBe(path.normalize(filePath))
})
it('supports file URL text', () => {
const filePath = createTempFile()
expect(getClipboardTextFilePath(pathToFileURL(filePath).href)).toBe(path.normalize(filePath))
})
it('rejects relative paths, missing files, directories, and multi-line text', () => {
const filePath = createTempFile()
expect(getClipboardTextFilePath(path.basename(filePath))).toBe('')
expect(getClipboardTextFilePath(path.join(os.tmpdir(), 'missing-clipboard-image.png'))).toBe('')
expect(getClipboardTextFilePath(path.dirname(filePath))).toBe('')
expect(getClipboardTextFilePath(`${filePath}\n${filePath}`)).toBe('')
})
})

View File

@@ -93,7 +93,7 @@ describe('getRawData 深度扩展工具测试', () => {
})
it('应当正确克隆 Date 和 RegExp', () => {
const date = new Date('2024-01-01')
const date = new Date('2024-01-02')
const reg = /test/g
const data = reactive({ date, reg })