Template
WIP: preserve frontend redesign
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 跨平台路径拼接助手。
|
||||
*
|
||||
* 前端仅用于「预览」派生路径,实际创建目录由后端完成。为了与后端
|
||||
* PathUtils.joinPath 的行为保持一致,这里统一:
|
||||
* - 将反斜杠 \ 归一化为正斜杠 /
|
||||
* - 去除父路径末尾多余的分隔符
|
||||
* - 去除子路径开头多余的分隔符
|
||||
* - 以单个 / 连接
|
||||
*
|
||||
* 注意:这只是显示层的规范化,不做真实文件系统解析。
|
||||
*/
|
||||
|
||||
/** 归一化根路径:转 POSIX 分隔符并去掉结尾斜杠。 */
|
||||
export function normalizeRoot(raw: string): string {
|
||||
if (!raw) {
|
||||
return '';
|
||||
}
|
||||
return raw.replace(/\\/g, '/').replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
/** 以规范化方式拼接父/子路径,返回 POSIX 形式字符串。 */
|
||||
export function joinPath(parent: string, child: string): string {
|
||||
const parentPosix = normalizeRoot(parent);
|
||||
if (!parentPosix) {
|
||||
return child ? child.replace(/\\/g, '/') : '';
|
||||
}
|
||||
if (!child) {
|
||||
return parentPosix;
|
||||
}
|
||||
const childPosix = child.replace(/\\/g, '/').replace(/^\/+/, '');
|
||||
return `${parentPosix}/${childPosix}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验路径是否合法(与后端 PathUtils.isValidPath 对齐)。
|
||||
* Windows 盘符后的冒号是合法的,其余情况下不允许出现 < > " | ? *。
|
||||
*/
|
||||
export function isValidPath(path: string): boolean {
|
||||
if (!path || !path.trim()) {
|
||||
return false;
|
||||
}
|
||||
let pathToValidate = path.trim();
|
||||
if (/^[A-Za-z]:[/\\]/.test(pathToValidate)) {
|
||||
pathToValidate = pathToValidate.substring(2);
|
||||
}
|
||||
return !/[<>"|?*]/.test(pathToValidate);
|
||||
}
|
||||
Reference in New Issue
Block a user