forked from Yara724/api
Fix links
This commit is contained in:
@@ -1,9 +1,38 @@
|
||||
type StoredFileCapture = {
|
||||
url?: string;
|
||||
path?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build a public file URL from a relative storage path (e.g. `files/...`).
|
||||
* Uses the current `URL` env so deployments with custom routing stay correct.
|
||||
*/
|
||||
export function buildFileLink(path: string): string {
|
||||
const baseUrl = process.env.URL;
|
||||
if(process.env.NODE_ENV === 'local'){
|
||||
return baseUrl + "/" + path;
|
||||
}else{
|
||||
return baseUrl + "/api/" + path;
|
||||
const normalized = String(path ?? "").trim();
|
||||
if (!normalized) return normalized;
|
||||
if (/^https?:\/\//i.test(normalized)) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const baseUrl = (process.env.URL ?? "").replace(/\/+$/, "");
|
||||
const relativePath = normalized.replace(/^\/+/, "");
|
||||
if (process.env.NODE_ENV === "local") {
|
||||
return `${baseUrl}/${relativePath}`;
|
||||
}
|
||||
return `${baseUrl}/api/${relativePath}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a file URL from persisted capture metadata.
|
||||
* Prefer rebuilding from `path` so stale baked-in URLs (e.g. user-portal prefix)
|
||||
* do not leak into expert/other API responses.
|
||||
*/
|
||||
export function resolveStoredFileUrl(
|
||||
stored?: StoredFileCapture | null,
|
||||
): string | undefined {
|
||||
const path = stored?.path?.trim();
|
||||
if (path) {
|
||||
return buildFileLink(path);
|
||||
}
|
||||
return stored?.url;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user