修复(UNC)网络路径被识别成相对路径的问题。

This commit is contained in:
unknown 2024-07-05 22:23:10 +08:00
parent 8c17275169
commit f0eeea8483
2 changed files with 19 additions and 5 deletions

View File

@ -14,7 +14,7 @@ function convert<F, T>(from: F): T {
* @returns * @returns
*/ */
function isAbsolutePath(path: string) { function isAbsolutePath(path: string) {
const regex = /^[a-zA-Z]:\\/; const regex = /^[a-zA-Z]:\\|^\\\\/;
return regex.test(path); return regex.test(path);
} }

View File

@ -1,4 +1,4 @@
import { resolve, dirname, parse } from "node:path"; import { resolve, dirname, parse, join } from "node:path";
import { isAbsolutePath } from "../../commons/utils/common"; import { isAbsolutePath } from "../../commons/utils/common";
import mime from "mime"; import mime from "mime";
import { readFileSync } from "node:fs"; import { readFileSync } from "node:fs";
@ -59,12 +59,21 @@ function getURLParams(paramsMap: Map<string, any>) {
function parseEnvPath(path: string) { function parseEnvPath(path: string) {
// 尝试解析路径中的环境变量 // 尝试解析路径中的环境变量
let parsedPath = parse(path); let parsedPath = parse(path);
let isBase = false; // 路径数组
let pathArr: Array<string> = []; let pathArr: Array<string> = [];
// 判断是否是网络路径,以\\开头
let isNetwork = false;
if (path.indexOf("\\\\") === 0) {
isNetwork = true;
}
// 是否是一级路径
let isBase = false;
if (!parsedPath.dir || parsedPath.dir.trim() === "") { if (!parsedPath.dir || parsedPath.dir.trim() === "") {
// 如果为空代表路径只有一级也就是当前文件本身比如C:/1.txt
pathArr = parsedPath.base.split("\\"); pathArr = parsedPath.base.split("\\");
isBase = true; isBase = true;
} else { } else {
// 不为空代表有父级目录
pathArr = parsedPath.dir.split("\\"); pathArr = parsedPath.dir.split("\\");
} }
// 新路径 // 新路径
@ -93,8 +102,13 @@ function parseEnvPath(path: string) {
if (!isBase) { if (!isBase) {
newPathArr.push(parsedPath.base); newPathArr.push(parsedPath.base);
} }
// 拼接并返回 // 拼接
return newPathArr.join("\\"); let newPath = join(...newPathArr);
// 如果是网络路径,在最前面加\\
if (isNetwork) {
newPath = "\\\\" + newPath;
}
return newPath;
} }
/** /**