189 lines
4.9 KiB
TypeScript
189 lines
4.9 KiB
TypeScript
function normalizeSplatLabel(label: string) {
|
|
return String(label || "").trim().toLowerCase();
|
|
}
|
|
|
|
function parseResolutionScore(label: string) {
|
|
const normalized = normalizeSplatLabel(label);
|
|
if (!normalized) return 0;
|
|
|
|
if (
|
|
normalized.includes("full") ||
|
|
normalized.includes("origin") ||
|
|
normalized.includes("source") ||
|
|
normalized.includes("max")
|
|
) {
|
|
return Number.MAX_SAFE_INTEGER;
|
|
}
|
|
|
|
const match = normalized.match(/(\d+(?:\.\d+)?)\s*([km]?)/);
|
|
if (!match) return 0;
|
|
|
|
const value = Number(match[1]);
|
|
if (!Number.isFinite(value)) return 0;
|
|
|
|
const unit = match[2];
|
|
if (unit === "m") return Math.round(value * 1_000_000);
|
|
if (unit === "k") return Math.round(value * 1_000);
|
|
return Math.round(value);
|
|
}
|
|
|
|
function compareSplatEntries(
|
|
a: [string, string],
|
|
b: [string, string]
|
|
) {
|
|
const scoreDelta = parseResolutionScore(b[0]) - parseResolutionScore(a[0]);
|
|
if (scoreDelta !== 0) return scoreDelta;
|
|
return a[0].localeCompare(b[0], "en");
|
|
}
|
|
|
|
export function getSortedWorldLabsSplatEntries(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return Object.entries(spzUrls || {})
|
|
.filter(
|
|
(entry): entry is [string, string] =>
|
|
Boolean(entry[0]) && typeof entry[1] === "string" && entry[1].trim().length > 0
|
|
)
|
|
.sort(compareSplatEntries);
|
|
}
|
|
|
|
export function pickBestWorldLabsSplatEntry(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getSortedWorldLabsSplatEntries(spzUrls)[0] || null;
|
|
}
|
|
|
|
export function pickBestWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return pickBestWorldLabsSplatEntry(spzUrls)?.[1] || null;
|
|
}
|
|
|
|
const INLINE_WORLDLABS_SPLAT_TARGET_SCORE = 1_500_000;
|
|
|
|
function pickClosestResolutionEntry(
|
|
entries: Array<[string, string]>,
|
|
targetScore: number
|
|
) {
|
|
if (!entries.length) return null;
|
|
|
|
const scoredEntries = entries
|
|
.map((entry) => ({
|
|
entry,
|
|
score: parseResolutionScore(entry[0]),
|
|
}))
|
|
.filter(
|
|
(item) =>
|
|
Number.isFinite(item.score) &&
|
|
item.score > 0 &&
|
|
item.score !== Number.MAX_SAFE_INTEGER
|
|
);
|
|
|
|
if (!scoredEntries.length) {
|
|
return entries[0] || null;
|
|
}
|
|
|
|
return scoredEntries
|
|
.slice()
|
|
.sort((left, right) => {
|
|
const distanceDelta =
|
|
Math.abs(left.score - targetScore) - Math.abs(right.score - targetScore);
|
|
if (distanceDelta !== 0) return distanceDelta;
|
|
return left.score - right.score;
|
|
})[0]?.entry || null;
|
|
}
|
|
|
|
const KNOWN_ARCHIVED_ASSET_HOSTS = [
|
|
"blob.vercel-storage.com",
|
|
"s3.1080p.hk",
|
|
"img.yydd.ai",
|
|
];
|
|
|
|
function isKnownArchivedAssetHost(hostname: string) {
|
|
return KNOWN_ARCHIVED_ASSET_HOSTS.some(
|
|
(host) => hostname === host || hostname.endsWith(`.${host}`)
|
|
);
|
|
}
|
|
|
|
export function isLikelyWorldLabsTemporaryAssetUrl(value?: string | null) {
|
|
const raw = String(value || "").trim();
|
|
if (!raw) return false;
|
|
if (raw.startsWith("/") || raw.startsWith("blob:") || raw.startsWith("data:")) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const parsed = new URL(raw);
|
|
const hostname = parsed.hostname.toLowerCase();
|
|
const pathname = parsed.pathname.toLowerCase();
|
|
|
|
if (isKnownArchivedAssetHost(hostname)) {
|
|
return false;
|
|
}
|
|
|
|
if (hostname.includes("worldlabs") || hostname.includes("marble")) {
|
|
return true;
|
|
}
|
|
|
|
const likelyWorldAsset =
|
|
pathname.includes("worldlabs") ||
|
|
pathname.includes("marble") ||
|
|
pathname.includes("spz") ||
|
|
pathname.includes("splat") ||
|
|
pathname.includes("mesh") ||
|
|
pathname.endsWith(".glb") ||
|
|
pathname.endsWith(".gltf");
|
|
|
|
return (
|
|
likelyWorldAsset &&
|
|
(hostname.includes("googleapis.com") ||
|
|
hostname.includes("googleusercontent.com") ||
|
|
hostname.includes("cloudfront.net"))
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isArchivedWorldLabsAssetUrl(value?: string | null) {
|
|
const raw = String(value || "").trim();
|
|
return Boolean(raw) && !isLikelyWorldLabsTemporaryAssetUrl(raw);
|
|
}
|
|
|
|
export function getArchivedWorldLabsSplatEntries(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getSortedWorldLabsSplatEntries(spzUrls).filter(([, url]) =>
|
|
isArchivedWorldLabsAssetUrl(url)
|
|
);
|
|
}
|
|
|
|
export function pickBestArchivedWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getArchivedWorldLabsSplatEntries(spzUrls)[0]?.[1] || null;
|
|
}
|
|
|
|
export function pickPreferredInlineWorldLabsSplatEntry(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
const archivedEntries = getArchivedWorldLabsSplatEntries(spzUrls);
|
|
if (archivedEntries.length > 0) {
|
|
return pickClosestResolutionEntry(
|
|
archivedEntries,
|
|
INLINE_WORLDLABS_SPLAT_TARGET_SCORE
|
|
);
|
|
}
|
|
|
|
return pickClosestResolutionEntry(
|
|
getSortedWorldLabsSplatEntries(spzUrls),
|
|
INLINE_WORLDLABS_SPLAT_TARGET_SCORE
|
|
);
|
|
}
|
|
|
|
export function pickPreferredInlineWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return pickPreferredInlineWorldLabsSplatEntry(spzUrls)?.[1] || null;
|
|
}
|