forked from Yara724/api
Fix car-damage links
This commit is contained in:
@@ -4,35 +4,59 @@ type StoredFileCapture = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a public file URL from a relative storage path (e.g. `files/...`).
|
* Reduce any stored file reference to the canonical relative storage path
|
||||||
* Uses the current `URL` env so deployments with custom routing stay correct.
|
* (e.g. `files/claim-captures/x.jpg`).
|
||||||
|
*
|
||||||
|
* Handles values that were persisted as absolute URLs on a different portal
|
||||||
|
* (e.g. `https://host/car-damage/user/api/files/...`) so we never leak a
|
||||||
|
* baked-in host/prefix into responses served from another origin.
|
||||||
*/
|
*/
|
||||||
export function buildFileLink(path: string): string {
|
function toRelativeFilePath(value: string | null | undefined): string {
|
||||||
const normalized = String(path ?? "").trim();
|
const s = String(value ?? "").trim();
|
||||||
if (!normalized) return normalized;
|
if (!s) return "";
|
||||||
if (/^https?:\/\//i.test(normalized)) {
|
|
||||||
return normalized;
|
// Prefer the `files/...` segment wherever it appears in the string.
|
||||||
|
const match = s.match(/(?:^|\/)(files\/.+)$/i);
|
||||||
|
if (match) return match[1];
|
||||||
|
|
||||||
|
// No recognizable file root: keep external URLs intact, otherwise strip
|
||||||
|
// any leading slashes so we can safely prefix the base URL.
|
||||||
|
if (/^https?:\/\//i.test(s)) return s;
|
||||||
|
return s.replace(/^\/+/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a public file URL from a stored path (relative or absolute).
|
||||||
|
*
|
||||||
|
* The link is always rebuilt from the current `URL` env, so deployments with
|
||||||
|
* custom routing (different sub-paths per portal) stay correct even when the
|
||||||
|
* persisted value was created elsewhere.
|
||||||
|
*/
|
||||||
|
export function buildFileLink(path: string): string {
|
||||||
|
const relative = toRelativeFilePath(path);
|
||||||
|
if (!relative) return "";
|
||||||
|
|
||||||
|
// Already an absolute external URL with no recognizable file root.
|
||||||
|
if (/^https?:\/\//i.test(relative)) return relative;
|
||||||
|
|
||||||
const baseUrl = (process.env.URL ?? "").replace(/\/+$/, "");
|
const baseUrl = (process.env.URL ?? "").replace(/\/+$/, "");
|
||||||
const relativePath = normalized.replace(/^\/+/, "");
|
|
||||||
if (process.env.NODE_ENV === "local") {
|
if (process.env.NODE_ENV === "local") {
|
||||||
return `${baseUrl}/${relativePath}`;
|
return `${baseUrl}/${relative}`;
|
||||||
}
|
}
|
||||||
return `${baseUrl}/api/${relativePath}`;
|
return `${baseUrl}/api/${relative}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve a file URL from persisted capture metadata.
|
* 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.
|
* Prefer `path`, fall back to `url`, and always rebuild through
|
||||||
|
* {@link buildFileLink} so stale portal-prefixed values (e.g. `.../user/api/...`)
|
||||||
|
* are normalized to the current server origin.
|
||||||
*/
|
*/
|
||||||
export function resolveStoredFileUrl(
|
export function resolveStoredFileUrl(
|
||||||
stored?: StoredFileCapture | null,
|
stored?: StoredFileCapture | null,
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
const path = stored?.path?.trim();
|
const source = stored?.path?.trim() || stored?.url?.trim();
|
||||||
if (path) {
|
if (!source) return undefined;
|
||||||
return buildFileLink(path);
|
return buildFileLink(source);
|
||||||
}
|
|
||||||
return stored?.url;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user