droboticflow/src/lib/video-preview.js
2026-06-26 00:59:03 +08:00

81 lines
2.1 KiB
JavaScript

const SOURCE_DATA_CONTENT_PATH_PREFIX = "/api/source-data/collections/";
const PACKAGE_CONTENT_PATH_PREFIX = "/api/packages/";
const OBJECT_STORAGE_CONTENT_PATH = "/api/object-storage/content";
const RUN_ARTIFACT_CONTENT_PATH_PREFIX = "/api/runs/";
function normalizeURL(input) {
if (!input || typeof input !== "string") {
return null;
}
try {
return new URL(input, "http://droboticflow.invalid");
} catch {
return null;
}
}
export function isSourceDataContentURL(input) {
const normalized = normalizeURL(input);
if (!normalized) {
return false;
}
return (
normalized.pathname.startsWith(SOURCE_DATA_CONTENT_PATH_PREFIX) &&
normalized.pathname.endsWith("/content")
);
}
export function isPackageContentURL(input) {
const normalized = normalizeURL(input);
if (!normalized) {
return false;
}
return (
normalized.pathname.startsWith(PACKAGE_CONTENT_PATH_PREFIX) &&
normalized.pathname.endsWith("/content")
);
}
export function isObjectStorageContentURL(input) {
const normalized = normalizeURL(input);
if (!normalized) {
return false;
}
return normalized.pathname === OBJECT_STORAGE_CONTENT_PATH;
}
export function isRunArtifactContentURL(input) {
const normalized = normalizeURL(input);
if (!normalized) {
return false;
}
return (
normalized.pathname.startsWith(RUN_ARTIFACT_CONTENT_PATH_PREFIX) &&
normalized.pathname.includes("/artifacts/") &&
normalized.pathname.endsWith("/content")
);
}
export function buildBrowserPlayableVideoURL(input) {
const normalized = normalizeURL(input);
if (
!normalized ||
(!isSourceDataContentURL(input) &&
!isPackageContentURL(input) &&
!isObjectStorageContentURL(input) &&
!isRunArtifactContentURL(input))
) {
return typeof input === "string" ? input : "";
}
if (normalized.searchParams.get("transcode") !== "browser") {
normalized.searchParams.set("transcode", "browser");
}
if (/^https?:\/\//i.test(input)) {
return normalized.toString();
}
const query = normalized.searchParams.toString();
return `${normalized.pathname}${query ? `?${query}` : ""}${normalized.hash}`;
}