修正windows下无法保存的问题
This commit is contained in:
@@ -1,18 +1,50 @@
|
||||
import { joinPath } from "../platform/paths";
|
||||
import { getPref } from "../../../../utils/prefs";
|
||||
import { getString } from "../../../../utils/locale";
|
||||
import {
|
||||
joinPath,
|
||||
normalizeStoragePath,
|
||||
sanitizePathSegment,
|
||||
} from "../platform/paths";
|
||||
|
||||
function profileDir(): string {
|
||||
const profile =
|
||||
(Zotero as any).Profile?.dir ||
|
||||
(Zotero as any).getProfileDirectory?.()?.path;
|
||||
if (!profile) {
|
||||
throw new Error("Cannot resolve Zotero profile directory");
|
||||
function resolveProfileDir(): string {
|
||||
const z = Zotero as any;
|
||||
const candidates: unknown[] = [
|
||||
z.Profile?.dir,
|
||||
typeof z.getProfileDirectory === "function" ? z.getProfileDirectory() : undefined,
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const path =
|
||||
typeof candidate === "string"
|
||||
? candidate
|
||||
: candidate &&
|
||||
typeof candidate === "object" &&
|
||||
"path" in candidate &&
|
||||
typeof (candidate as { path: unknown }).path === "string"
|
||||
? (candidate as { path: string }).path
|
||||
: "";
|
||||
if (path.trim()) return normalizeStoragePath(path.trim());
|
||||
}
|
||||
return profile;
|
||||
|
||||
throw new Error("Cannot resolve Zotero profile directory");
|
||||
}
|
||||
|
||||
/** `{Profile}/chatpapers/lecture/` — voice lecture data root */
|
||||
function customLectureDataDir(): string {
|
||||
return String(getPref("lectureDataDir") || "").trim();
|
||||
}
|
||||
|
||||
/** Map logical cacheKey (may contain `|`) to a filesystem-safe directory/file stem. */
|
||||
export function cacheKeyDirName(cacheKey: string): string {
|
||||
return sanitizePathSegment(cacheKey, 100);
|
||||
}
|
||||
|
||||
/** Resolved voice-lecture data root (custom pref or `{Profile}/chatpapers/lecture/`). */
|
||||
export function lectureDataRoot(): string {
|
||||
return joinPath(profileDir(), "chatpapers", "lecture");
|
||||
const custom = customLectureDataDir();
|
||||
if (custom) {
|
||||
return normalizeStoragePath(custom);
|
||||
}
|
||||
return joinPath(resolveProfileDir(), "chatpapers", "lecture");
|
||||
}
|
||||
|
||||
export function lectureDbPath(): string {
|
||||
@@ -24,12 +56,16 @@ export function lectureIndexPath(): string {
|
||||
return joinPath(lectureDataRoot(), "cache-index.json");
|
||||
}
|
||||
|
||||
export function lectureDataFilePath(cacheKey: string): string {
|
||||
return joinPath(lectureDataRoot(), "data", `${cacheKeyDirName(cacheKey)}.json`);
|
||||
}
|
||||
|
||||
export function audioRootDir(): string {
|
||||
return joinPath(lectureDataRoot(), "audio");
|
||||
}
|
||||
|
||||
export function audioDirForPaper(cacheKey: string): string {
|
||||
return joinPath(audioRootDir(), cacheKey);
|
||||
return joinPath(audioRootDir(), cacheKeyDirName(cacheKey));
|
||||
}
|
||||
|
||||
export function audioFilePath(
|
||||
@@ -37,30 +73,41 @@ export function audioFilePath(
|
||||
unitId: string,
|
||||
ext: "wav" | "mp3" = "wav",
|
||||
): string {
|
||||
return joinPath(audioDirForPaper(cacheKey), `${unitId}.${ext}`);
|
||||
return joinPath(
|
||||
audioDirForPaper(cacheKey),
|
||||
`${sanitizePathSegment(unitId, 80)}.${ext}`,
|
||||
);
|
||||
}
|
||||
|
||||
export function lectureSamplePath(name: string): string {
|
||||
return joinPath(lectureDataRoot(), "samples", name);
|
||||
return joinPath(lectureDataRoot(), "samples", sanitizePathSegment(name, 80));
|
||||
}
|
||||
|
||||
export async function ensureSampleDir(): Promise<string> {
|
||||
const dir = joinPath(lectureDataRoot(), "samples");
|
||||
await IOUtils.makeDirectory(dir, {
|
||||
await ensureDir(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
async function ensureDir(path: string): Promise<void> {
|
||||
await IOUtils.makeDirectory(path, {
|
||||
createAncestors: true,
|
||||
ignoreExisting: true,
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
|
||||
export async function ensureLectureDirs(): Promise<void> {
|
||||
const root = lectureDataRoot();
|
||||
await IOUtils.makeDirectory(root, {
|
||||
createAncestors: true,
|
||||
ignoreExisting: true,
|
||||
});
|
||||
await IOUtils.makeDirectory(audioRootDir(), {
|
||||
createAncestors: true,
|
||||
ignoreExisting: true,
|
||||
});
|
||||
try {
|
||||
await ensureDir(root);
|
||||
await ensureDir(joinPath(root, "data"));
|
||||
await ensureDir(audioRootDir());
|
||||
} catch (e) {
|
||||
const detail = e instanceof Error ? e.message : String(e);
|
||||
throw new Error(
|
||||
getString("lecture-storage-write-failed", {
|
||||
args: { path: root, detail },
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user