Files
frontend/shared/object-storage.ts

32 lines
1.3 KiB
TypeScript

/** Model files are read directly from storage; business APIs keep their own origin. */
export const objectStorageOrigin =
"https://linkerbot.oss-cn-beijing.aliyuncs.com";
/** Raw OSS object keys (as returned in component details), encoded once per segment. */
export function objectStorageUrl(key: string): string {
return `${objectStorageOrigin}/${key.replace(/^\/+/, "").split("/").map(encodeURIComponent).join("/")}`;
}
/** Convert download API URLs or object paths without changing the stored environment prefix. */
export function resourceDownloadUrl(
value: string,
pageOrigin = typeof window === "undefined"
? "http://localhost"
: window.location.origin,
): string {
const url = new URL(value, pageOrigin);
if (!["http:", "https:"].includes(url.protocol))
throw Error("模型资源地址无效。");
const prefixes = ["/admin-api/component-assets/", "/api/robot-model-assets/"];
const prefix = prefixes.find((path) => url.pathname.startsWith(path));
const path = prefix
? url.pathname.slice(prefix.length)
: /^\/cadworld(?:_[^/]+)?\/components\//.test(url.pathname) &&
!/^https?:\/\//i.test(value)
? url.pathname.slice(1)
: undefined;
if (path === undefined) return url.href;
const key = path.split("/").map(decodeURIComponent).join("/");
return `${objectStorageUrl(key)}${url.search}`;
}