publicUrl.ts
Ricky/Cat Project Final/fp_fixed /src/lib/publicUrl.ts
/**
* Base URL for API and uploaded assets.
* Set NEXT_PUBLIC_API_BASE to "" (empty) to use same-origin URLs (/api/..., /uploads/...).
* Omit or leave unset for local dev default http://localhost:5050.
*/
export function getPublicApiBase(): string {
const raw = process.env.NEXT_PUBLIC_API_BASE;
if (raw === "") return "";
if (raw == null || raw === undefined) return "http://localhost:5050";
return String(raw).replace(/\/$/, "");
}
/**
* Map legacy /uploads/... paths to /api/uploads/... so images load when nginx only proxies /api/ to Flask.
*/
export function mapLegacyUploadPath(path: string): string {
if (path === "/uploads" || path.startsWith("/uploads/")) {
return `/api/uploads${path.slice("/uploads".length)}`;
}
return path;
}
/** Absolute or same-origin URL for paths returned by the API (e.g. /uploads/..., /api/uploads/...). */
export function publicAssetUrl(path: string | undefined | null): string {
if (path == null) return "";
const p = String(path).trim();
if (!p) return "";
if (/^https?:\/\//i.test(p)) return p;
let normalized = p.startsWith("/") ? p : `/${p}`;
normalized = mapLegacyUploadPath(normalized);
const base = getPublicApiBase();
return base ? `${base}${normalized}` : normalized;
}
/** Build /api/... URL for fetch(). */
export function apiUrl(apiPath: string): string {
const path = apiPath.startsWith("/") ? apiPath : `/${apiPath}`;
const base = getPublicApiBase();
return base ? `${base}${path}` : path;
}
相關文章
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
閱讀文章 →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
閱讀文章 →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
閱讀文章 →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
閱讀文章 →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
閱讀文章 →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
閱讀文章 →