From f0eeea8483f66c1c562d4ef4926fc5ca426e5fce Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 5 Jul 2024 22:23:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D(UNC)=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=A2=AB=E8=AF=86=E5=88=AB=E6=88=90=E7=9B=B8?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commons/utils/common.ts | 2 +- electron/commons/utils.ts | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/commons/utils/common.ts b/commons/utils/common.ts index 164f482..7a8f976 100644 --- a/commons/utils/common.ts +++ b/commons/utils/common.ts @@ -14,7 +14,7 @@ function convert(from: F): T { * @returns */ function isAbsolutePath(path: string) { - const regex = /^[a-zA-Z]:\\/; + const regex = /^[a-zA-Z]:\\|^\\\\/; return regex.test(path); } diff --git a/electron/commons/utils.ts b/electron/commons/utils.ts index f10ce3c..663ed90 100644 --- a/electron/commons/utils.ts +++ b/electron/commons/utils.ts @@ -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 mime from "mime"; import { readFileSync } from "node:fs"; @@ -59,12 +59,21 @@ function getURLParams(paramsMap: Map) { function parseEnvPath(path: string) { // 尝试解析路径中的环境变量 let parsedPath = parse(path); - let isBase = false; + // 路径数组 let pathArr: Array = []; + // 判断是否是网络路径,以\\开头 + let isNetwork = false; + if (path.indexOf("\\\\") === 0) { + isNetwork = true; + } + // 是否是一级路径 + let isBase = false; if (!parsedPath.dir || parsedPath.dir.trim() === "") { + // 如果为空代表路径只有一级,也就是当前文件本身,比如C:/1.txt pathArr = parsedPath.base.split("\\"); isBase = true; } else { + // 不为空代表有父级目录 pathArr = parsedPath.dir.split("\\"); } // 新路径 @@ -93,8 +102,13 @@ function parseEnvPath(path: string) { if (!isBase) { newPathArr.push(parsedPath.base); } - // 拼接并返回 - return newPathArr.join("\\"); + // 拼接 + let newPath = join(...newPathArr); + // 如果是网络路径,在最前面加\\ + if (isNetwork) { + newPath = "\\\\" + newPath; + } + return newPath; } /**