Implement unique filename generation to prevent overwriting existing session files
This commit is contained in:
22
handler.ts
22
handler.ts
@@ -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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user