mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 15:30:39 +00:00
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 7a088e6801
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Reviewed-by: @mbelinky
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import crypto from "node:crypto";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
function sanitizePrefix(prefix: string): string {
|
|
const normalized = prefix.replace(/[^a-zA-Z0-9_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
return normalized || "tmp";
|
|
}
|
|
|
|
function sanitizeExtension(extension?: string): string {
|
|
if (!extension) {
|
|
return "";
|
|
}
|
|
const normalized = extension.startsWith(".") ? extension : `.${extension}`;
|
|
const suffix = normalized.match(/[a-zA-Z0-9._-]+$/)?.[0] ?? "";
|
|
const token = suffix.replace(/^[._-]+/, "");
|
|
if (!token) {
|
|
return "";
|
|
}
|
|
return `.${token}`;
|
|
}
|
|
|
|
export function buildRandomTempFilePath(params: {
|
|
prefix: string;
|
|
extension?: string;
|
|
tmpDir?: string;
|
|
now?: number;
|
|
uuid?: string;
|
|
}): string {
|
|
const prefix = sanitizePrefix(params.prefix);
|
|
const extension = sanitizeExtension(params.extension);
|
|
const nowCandidate = params.now;
|
|
const now =
|
|
typeof nowCandidate === "number" && Number.isFinite(nowCandidate)
|
|
? Math.trunc(nowCandidate)
|
|
: Date.now();
|
|
const uuid = params.uuid?.trim() || crypto.randomUUID();
|
|
return path.join(params.tmpDir ?? os.tmpdir(), `${prefix}-${now}-${uuid}${extension}`);
|
|
}
|