修正windows下无法保存的问题

This commit is contained in:
yhy
2026-08-30 08:46:04 +08:00
parent 3702527613
commit 455059eebe
12 changed files with 147 additions and 32 deletions
@@ -22,6 +22,43 @@ export function joinPath(...parts: string[]): string {
return PathUtils.join(...parts.filter(Boolean));
}
/** Normalize a filesystem path for the current OS. */
export function normalizeStoragePath(input: string): string {
const trimmed = (input || "").trim();
if (!trimmed) return "";
const joined = trimmed.replace(/[\\/]+/g, "/");
if (/^[A-Za-z]:\//.test(joined)) {
const drive = joined.slice(0, 2);
const rest = joined
.slice(2)
.split("/")
.filter((part) => part && part !== ".")
.join("\\");
return `${drive}\\${rest}`.replace(/\\+$/, "");
}
return joined
.split("/")
.filter((part) => part && part !== ".")
.join("/")
.replace(/\/+$/, "");
}
/** Safe single path segment for file or directory names (cross-platform). */
export function sanitizePathSegment(input: string, maxLen = 120): string {
const cleaned = String(input || "")
.replace(/[<>:"/\\|?*\x00-\x1f]/g, "_")
.replace(/[.\s]+$/, "")
.trim();
if (!cleaned) return "_";
if (cleaned.length <= maxLen) return cleaned;
let hash = 0;
for (let i = 0; i < input.length; i++) {
hash = (hash * 31 + input.charCodeAt(i)) | 0;
}
const tail = Math.abs(hash).toString(36);
return `${cleaned.slice(0, Math.max(1, maxLen - tail.length - 1))}_${tail}`;
}
/** Convert local file path to file:// URL for <audio src>. */
export function pathToFileUrl(filePath: string): string {
const normalized = filePath.replace(/\\/g, "/");