photo_server_json_flutter_c.../public/js/config.js
2026-03-05 17:07:30 +01:00

63 lines
1.8 KiB
JavaScript

// ===============================
// CONFIG DINAMICA DAL SERVER
// ===============================
window.BASE_URL = null;
window.PHOTOS_URL = null;
window.MEDIA_BASE_ORIGIN = null;
window.configReady = false;
window.PATH_FULL = false;
// Carica /config dal backend
(async () => {
try {
const res = await fetch('/config');
const cfg = await res.json();
window.BASE_URL = cfg.baseUrl;
window.PATH_FULL = cfg.pathFull;
window.PHOTOS_URL = `${window.BASE_URL}/photos`;
window.MEDIA_BASE_ORIGIN = new URL(window.PHOTOS_URL).origin;
console.log("[config] BASE_URL:", window.BASE_URL);
console.log("[config] PHOTOS_URL:", window.PHOTOS_URL);
console.log("[config] MEDIA_BASE_ORIGIN:", window.MEDIA_BASE_ORIGIN);
console.log("[config] PATH_FULL:", window.PATH_FULL);
window.configReady = true;
} catch (err) {
console.error("[config] Errore nel caricamento della config:", err);
}
})();
// ===============================
// Utility: normalizza URL dei media
// ===============================
function toAbsoluteUrl1(pathOrUrl) {
if (!pathOrUrl) return '';
if (/^https?:\/\//i.test(pathOrUrl)) return pathOrUrl;
const normalized = pathOrUrl.startsWith('/') ? pathOrUrl : `/${pathOrUrl}`;
return `${window.MEDIA_BASE_ORIGIN}${normalized}`;
}
function toAbsoluteUrl(pathOrUrl, name, type, cartella) {
const BASE_URL = 'https://prova.patachina.it';
if (!pathOrUrl) return '';
if (!name || !type || !cartella) {
throw new Error('name, type e cartella sono obbligatori');
}
// Normalizza il path rimuovendo slash iniziali/finali
const cleanedPath = String(pathOrUrl).replace(/^\/+|\/+$/g, '');
// Costruzione dell'URL finale
return `${BASE_URL}/photos/${encodeURIComponent(name)}/${encodeURIComponent(type)}/${encodeURIComponent(cartella)}/${cleanedPath}`;
}
window.toAbsoluteUrl = toAbsoluteUrl;