mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-26 12:42:34 +08:00
✨ 支持文件检索呼起插件
This commit is contained in:
49
src/common/utils/getCopyFiles.ts
Normal file
49
src/common/utils/getCopyFiles.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import commonConst from "./commonConst";
|
||||
import { clipboard, remote } from "electron";
|
||||
import plist from "plist";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import ofs from "original-fs";
|
||||
|
||||
export default function getCopyFiles(): Array<any> | null {
|
||||
let fileInfo;
|
||||
if (commonConst.macOS()) {
|
||||
if (!clipboard.has("NSFilenamesPboardType")) return null;
|
||||
const result = clipboard.read("NSFilenamesPboardType");
|
||||
if (!result) return null;
|
||||
try {
|
||||
fileInfo = plist.parse(result);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
} else if (commonConst.windows()) {
|
||||
// todo
|
||||
} else {
|
||||
if (!commonConst.linux()) return null;
|
||||
if (!clipboard.has("text/uri-list")) return null;
|
||||
const result = clipboard.read("text/uri-list").match(/^file:\/\/\/.*/gm);
|
||||
if (!result || !result.length) return null;
|
||||
fileInfo = result.map((e) =>
|
||||
decodeURIComponent(e).replace(/^file:\/\//, "")
|
||||
);
|
||||
}
|
||||
if (!Array.isArray(fileInfo)) return null;
|
||||
const target: any = fileInfo
|
||||
.map((p) => {
|
||||
if (!fs.existsSync(p)) return false;
|
||||
let info;
|
||||
try {
|
||||
info = ofs.lstatSync(p);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return {
|
||||
isFile: info.isFile(),
|
||||
isDirectory: info.isDirectory(),
|
||||
name: path.basename(p) || p,
|
||||
path: p,
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
return target.length ? target : null;
|
||||
}
|
||||
Reference in New Issue
Block a user