openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 3.39 kB
JavaScript
import { d as realpathOrResolve } from "./path-existence-Co7-GyVr.mjs";
import { s as relativePathEscapesRoot } from "./path-guards-TIjxtj6H.mjs";
import { t as hasHttpUrlPrefix } from "./url-protocol-DXxSDuQZ.mjs";
import { n as normalizeMediaReferenceSource, r as parseInboundMediaUri, t as MediaReferenceError } from "./inbound-media-uri-DpK4_BLW.mjs";
import { t as getMediaDir } from "./store-path-D4uVfy73.mjs";
import { n as maybeLocalPathFromSource } from "./inbound-media-path-C6oH727p.mjs";
import { u as resolveMediaBufferPath } from "./store-zsTAVy24.mjs";
import path from "node:path";
//#region src/media/media-reference.ts
/** Classifies media reference schemes before local resolution or sandbox rewriting. */
function classifyMediaReferenceSource(e, t) {
const n = t?.allowDataUrl ?? true;
const r = /^[a-zA-Z]:[\\/]/.test(e);
const i = /^[a-z][a-z0-9+.-]*:/i.test(e);
const a = /^file:/i.test(e);
const o = hasHttpUrlPrefix(e);
const s = /^data:/i.test(e);
const c = /^media:\/\//i.test(e);
return {
hasUnsupportedScheme: i && !r && !a && !o && !c && !(n && s),
isDataUrl: s,
isFileUrl: a,
isHttpUrl: o,
isMediaStoreUrl: c,
looksLikeWindowsDrivePath: r
};
}
/** Rewrites inbound media-store URIs to sandbox-relative paths for staged agent inputs. */
function resolveMediaReferenceSandboxPath(e, t = "media/inbound") {
const n = normalizeMediaReferenceSource(e);
const r = parseInboundMediaUri(n);
if (!r) return { resolved: n };
return {
resolved: path.posix.join(t.replace(/\\/g, "/"), r.id),
rewrittenFrom: r.normalizedSource
};
}
/** Resolves inbound media:// URIs or first-level inbound file paths to concrete store files. */
async function resolveInboundMediaReference(e) {
const t = normalizeMediaReferenceSource(e);
if (!t) return null;
const n = parseInboundMediaUri(t);
if (n) return {
id: n.id,
physicalPath: await resolveInboundMediaPath(n.id, n.normalizedSource)
};
const r = maybeLocalPathFromSource(t);
if (!r) return null;
const i = path.resolve(getMediaDir(), "inbound");
const a = path.resolve(r);
const o = path.relative(i, a);
const s = o && !relativePathEscapesRoot(o) ? o : path.relative(await realpathOrResolve(i), await realpathOrResolve(r));
if (!s || relativePathEscapesRoot(s) || s.includes(path.sep)) return null;
return {
id: s,
physicalPath: await resolveInboundMediaPath(s, t)
};
}
/** Resolves a media reference while preserving whether it belongs to the inbound store. */
async function resolveMediaReferenceLocalPathInfo(e) {
const t = normalizeMediaReferenceSource(e);
const n = await resolveInboundMediaReference(t);
return n ? {
kind: "inbound",
path: n.physicalPath
} : {
kind: "local",
path: t
};
}
/** Converts inbound media references for callers that need a direct local file path. */
async function resolveMediaReferenceLocalPath(e) {
return (await resolveMediaReferenceLocalPathInfo(e)).path;
}
async function resolveInboundMediaPath(e, t) {
try {
return await resolveMediaBufferPath(e, "inbound");
} catch (e) {
throw new MediaReferenceError("invalid-path", e instanceof Error ? e.message : `Invalid media reference: ${t}`, { cause: e });
}
}
//#endregion
export { resolveMediaReferenceSandboxPath as a, resolveMediaReferenceLocalPathInfo as i, resolveInboundMediaReference as n, resolveMediaReferenceLocalPath as r, classifyMediaReferenceSource as t };