Initial 智念AIGC platform
This commit is contained in:
57
lib/server/runtime.ts
Normal file
57
lib/server/runtime.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
export const DEFAULT_OWNER_ID = "demo-merchant";
|
||||
|
||||
export function rootDir(): string {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
export function runtimeDir(): string {
|
||||
return process.env.NIANXXPLAY_RUNTIME_DIR || join(rootDir(), ".runtime");
|
||||
}
|
||||
|
||||
export function dataDir(): string {
|
||||
return process.env.NIANXXPLAY_DATA_DIR || join(runtimeDir(), "data");
|
||||
}
|
||||
|
||||
export function uploadDir(): string {
|
||||
return process.env.NIANXXPLAY_UPLOAD_DIR || join(runtimeDir(), "uploads");
|
||||
}
|
||||
|
||||
export function resultDir(): string {
|
||||
return process.env.NIANXXPLAY_RESULT_DIR || join(runtimeDir(), "generated-results");
|
||||
}
|
||||
|
||||
export async function ensureRuntimeDirs(): Promise<void> {
|
||||
await Promise.all([
|
||||
mkdir(dataDir(), { recursive: true }),
|
||||
mkdir(uploadDir(), { recursive: true }),
|
||||
mkdir(resultDir(), { recursive: true })
|
||||
]);
|
||||
}
|
||||
|
||||
export function localRuntimePath(...parts: string[]): string {
|
||||
return resolve(runtimeDir(), ...parts);
|
||||
}
|
||||
|
||||
export function requestOrigin(request: Request): string {
|
||||
const configured = process.env.NEXT_PUBLIC_APP_URL || process.env.NIANXXPLAY_PUBLIC_BASE_URL;
|
||||
if (configured) return normalizePublicOrigin(configured);
|
||||
return normalizePublicOrigin(new URL(request.url).origin);
|
||||
}
|
||||
|
||||
export function toAbsoluteUrl(url: string, origin: string): string {
|
||||
if (/^https?:\/\//i.test(url) || url.startsWith("data:")) return url;
|
||||
return new URL(url, origin).toString();
|
||||
}
|
||||
|
||||
function normalizePublicOrigin(origin: string): string {
|
||||
try {
|
||||
const url = new URL(origin);
|
||||
if (url.hostname === "0.0.0.0") url.hostname = "127.0.0.1";
|
||||
return url.origin;
|
||||
} catch {
|
||||
return origin.replace(/\/$/, "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user