24 lines
1.0 KiB
TypeScript
24 lines
1.0 KiB
TypeScript
/**
|
||
* Baut eine Download-URL für ein im Backend gespeichertes Upload-File.
|
||
*
|
||
* Geht über `GET /api/files/download?path=...` – der Backend-Controller
|
||
* macht einen Per-File-Ownership-Check (Pfad → Resource → canAccessCustomer
|
||
* / canAccessContract). Damit kann auch ein eingeloggter User keine
|
||
* fremden Dateien abrufen, selbst wenn er den Pfad kennen würde.
|
||
*
|
||
* <a href> und window.open senden keinen Authorization-Header, daher
|
||
* Token als Query-Parameter (auth-Middleware akzeptiert `?token=<jwt>`).
|
||
*
|
||
* Trade-off: Tokens in URLs können in Logs/Referrer landen. Eine
|
||
* sauberere Lösung mit kurzlebigen Download-Tokens (signierte URLs)
|
||
* wäre v1.1-Item.
|
||
*/
|
||
export function fileUrl(path: string | null | undefined): string {
|
||
if (!path) return '';
|
||
const token = localStorage.getItem('token');
|
||
const normalizedPath = path.startsWith('/') ? path : '/' + path;
|
||
const base = `/api/files/download?path=${encodeURIComponent(normalizedPath)}`;
|
||
if (!token) return base;
|
||
return `${base}&token=${encodeURIComponent(token)}`;
|
||
}
|