支持文件检索呼起插件

This commit is contained in:
muwoo
2021-12-20 18:33:00 +08:00
parent ced8aa846b
commit ff118dfe2d
25 changed files with 351 additions and 70 deletions

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