mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-05 20:51:26 +00:00
feat: Securisation du téléchargement de fichier
This commit is contained in:
25
Front-End/src/utils/fileUrl.js
Normal file
25
Front-End/src/utils/fileUrl.js
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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)}`;
|
||||
};
|
||||
Reference in New Issue
Block a user