fixed the bug of invalid environment variables.

This commit is contained in:
unknown 2024-03-03 20:11:58 +08:00
parent 28f3ed000f
commit 467f33bc99
4 changed files with 22 additions and 34 deletions

View File

@ -98,24 +98,20 @@ function parseEnvPath(path: string) {
}
/**
*
* @param path
*
* @returns
*/
function getAbsolutePath(path: string) {
function parsePath(path: string) {
// 尝试解析环境变量
path = parseEnvPath(path);
// 是否是相对路径
if (!isAbsolutePath(path)) {
// 尝试解析环境变量
let newPath = parseEnvPath(path);
// 判断解析之后的路径是否是绝对路径
if (isAbsolutePath(newPath)) {
return newPath;
} else {
return resolve(
process.env.NODE_ENV === "development"
? resolve(".")
: dirname(process.execPath),
path
);
}
return resolve(
process.env.NODE_ENV === "development"
? resolve(".")
: dirname(process.execPath),
path
);
}
return path;
}
@ -146,10 +142,4 @@ function getFileIcon(filePath: string | null) {
return icon;
}
export {
getURLParams,
getAbsolutePath,
getFileIcon,
iconExts,
getRandomUserAgent,
};
export { getURLParams, parsePath, getFileIcon, iconExts, getRandomUserAgent };

View File

@ -1,6 +1,6 @@
import { BrowserWindow, shell, dialog, app } from "electron";
import { join } from "node:path";
import { getAbsolutePath, getURLParams } from "../../commons/utils";
import { parsePath, getURLParams } from "../../commons/utils";
import { Item } from "../../../types/item";
import {
batchAdd,
@ -326,7 +326,7 @@ function run(
// 获取绝对路径
if (item.type === 0 || item.type === 1) {
// 获取路径
item.data.target = getAbsolutePath(item.data.target);
item.data.target = parsePath(item.data.target);
}
try {
// 判断文件或文件夹是否存在
@ -496,7 +496,7 @@ async function createShortcut(item: Item) {
let target = item.data.target;
if (item.type === 0 || item.type === 1) {
// 获取绝对路径
target = getAbsolutePath(target);
target = parsePath(target);
}
// 保存路径
let savePath =

View File

@ -5,9 +5,8 @@ import {
ipcMain,
clipboard,
MenuItemConstructorOptions,
screen,
} from "electron";
import { getAbsolutePath } from "../../commons/utils";
import { parsePath } from "../../commons/utils";
import {
convertTarget,
createAddEditWindow,
@ -267,7 +266,7 @@ export default function () {
.getNativeWindowHandle()
.readInt32LE(0),
item.type === 0 || item.type === 1
? getAbsolutePath(item.data.target)
? parsePath(item.data.target)
: item.data.target,
point[0],
point[1]
@ -296,7 +295,7 @@ export default function () {
click: () => {
clipboard.writeText(
item.type === 0 || item.type === 1
? getAbsolutePath(item.data.target)
? parsePath(item.data.target)
: item.data.target
);
},
@ -679,7 +678,7 @@ export default function () {
}
let icon = join(process.env.VITE_PUBLIC, "drag-and-drop.png");
event.sender.startDrag({
file: getAbsolutePath(item.data.target),
file: parsePath(item.data.target),
icon: icon,
});
} finally {

View File

@ -7,11 +7,10 @@ import {
import { CommonItem, Item } from "../../types/item";
import { parse, join, extname } from "node:path";
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { execSync } from "node:child_process";
import xml2js from "xml2js";
import { newCommonItem, newCommonItemData } from "../../commons/utils/common";
import { ShortcutInfo } from "../../types/common";
import { getAbsolutePath, getFileIcon } from "../commons/utils";
import { parsePath, getFileIcon } from "../commons/utils";
// AppxInfo
export interface AppxInfo {
@ -753,7 +752,7 @@ function checkInvalidItem(itemList: Array<Item>) {
// 只校验文件和文件夹
if (item.type === 0 || item.type === 1) {
// 获取绝对路径
let path = getAbsolutePath(item.data.target);
let path = parsePath(item.data.target);
try {
statSync(path);
} catch (e) {