mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 04:51:36 +00:00
refactor(cli): extract shared node media helpers
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import * as fs from "node:fs/promises";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { resolveCliName } from "./cli-name.js";
|
||||
import {
|
||||
asBoolean,
|
||||
asNumber,
|
||||
asRecord,
|
||||
asString,
|
||||
resolveTempPathParts,
|
||||
} from "./nodes-media-utils.js";
|
||||
|
||||
const MAX_CAMERA_URL_DOWNLOAD_BYTES = 250 * 1024 * 1024;
|
||||
|
||||
@@ -24,22 +29,6 @@ export type CameraClipPayload = {
|
||||
hasAudio: boolean;
|
||||
};
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
|
||||
function asString(value: unknown): string | undefined {
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
function asNumber(value: unknown): number | undefined {
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function asBoolean(value: unknown): boolean | undefined {
|
||||
return typeof value === "boolean" ? value : undefined;
|
||||
}
|
||||
|
||||
export function parseCameraSnapPayload(value: unknown): CameraSnapPayload {
|
||||
const obj = asRecord(value);
|
||||
const format = asString(obj.format);
|
||||
@@ -73,10 +62,12 @@ export function cameraTempPath(opts: {
|
||||
tmpDir?: string;
|
||||
id?: string;
|
||||
}) {
|
||||
const tmpDir = opts.tmpDir ?? os.tmpdir();
|
||||
const id = opts.id ?? randomUUID();
|
||||
const { tmpDir, id, ext } = resolveTempPathParts({
|
||||
tmpDir: opts.tmpDir,
|
||||
id: opts.id,
|
||||
ext: opts.ext,
|
||||
});
|
||||
const facingPart = opts.facing ? `-${opts.facing}` : "";
|
||||
const ext = opts.ext.startsWith(".") ? opts.ext : `.${opts.ext}`;
|
||||
const cliName = resolveCliName();
|
||||
return path.join(tmpDir, `${cliName}-camera-${opts.kind}${facingPart}-${id}${ext}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user