refactor(cli): extract shared node media helpers

This commit is contained in:
Peter Steinberger
2026-02-18 23:32:32 +00:00
parent 65ef7fb4a4
commit 3b7c8fe79a
5 changed files with 76 additions and 47 deletions

View File

@@ -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}`);
}