mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-05 12:41:27 +00:00
26 lines
882 B
JavaScript
26 lines
882 B
JavaScript
/**
|
|
* Construit l'URL sécurisée pour accéder à un fichier media via le proxy Next.js.
|
|
* Le proxy `/api/download` injecte le JWT côté serveur avant de transmettre au backend Django.
|
|
*
|
|
* Gère les chemins relatifs ("/data/some/file.pdf") et les URLs absolues du backend
|
|
* ("http://backend:8000/data/some/file.pdf").
|
|
*
|
|
* @param {string} filePath - Chemin ou URL complète du fichier
|
|
* @returns {string|null} URL vers /api/download?path=... ou null si pas de chemin
|
|
*/
|
|
export const getSecureFileUrl = (filePath) => {
|
|
if (!filePath) return null;
|
|
|
|
// Si c'est une URL absolue, extraire le chemin /data/...
|
|
if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
|
|
try {
|
|
const url = new URL(filePath);
|
|
filePath = url.pathname;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return `/api/download?path=${encodeURIComponent(filePath)}`;
|
|
};
|