Implement unique filename generation to prevent overwriting existing session files

This commit is contained in:
Space-Banane
2026-04-08 20:12:36 +02:00
parent c626c73e40
commit 058e2f4df4
3 changed files with 31 additions and 1 deletions

View File

@@ -197,6 +197,26 @@ function buildSlugExcerpt(full: string): string {
return tail.slice(-3000);
}
async function getUniqueFilePath(dir: string, filename: string): Promise<string> {
const candidate = path.join(dir, filename);
try {
await fs.access(candidate);
} catch {
return candidate;
}
const parsed = path.parse(filename);
for (let i = 1; ; i += 1) {
const name = `${parsed.name}-${i}${parsed.ext}`;
const p = path.join(dir, name);
try {
await fs.access(p);
} catch {
return p;
}
}
}
async function generateSlug(params: {
excerpt: string;
cfg: AnyObj;
@@ -274,7 +294,7 @@ const handler = async (event: {
await fs.mkdir(memoryDir, { recursive: true });
const filename = `${dateStr}-${slug}.md`;
const memoryFilePath = path.join(memoryDir, filename);
const memoryFilePath = await getUniqueFilePath(memoryDir, filename);
const source = typeof context.commandSource === "string" ? context.commandSource : "unknown";