photo_server_json_flutter_c.../public/admin.html
2026-03-11 11:31:52 +01:00

132 lines
3 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Photo Manager</title>
</head>
<body>
<div id="app" style="padding:20px;">
<h2>Gestione Foto</h2>
<button onclick="scan()">Scansiona Foto</button>
<button onclick="resetDB()">Reset DB</button>
<button onclick="readDB()">Leggi DB</button>
<button onclick="deletePhoto()">Cancella Foto per ID</button>
<button onclick="findIdIndex()">Cerca ID in index.json</button>
<button onclick="resetDBuser()">Reset DB Utente</button>
<button onclick="window.location.href='index.html'">Torna alla galleria</button>
<pre id="out"></pre>
</div>
<!-- Eruda Debug Console -->
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>
eruda.init();
console.log("Eruda inizializzato");
</script>
<script>
let BASE_URL = null;
let token = localStorage.getItem("token");
let db = [];
// Se non c'è token → torna alla galleria (login avviene lì)
if (!token) {
window.location.href = "index.html";
}
async function deletePhoto() {
const id = prompt("Inserisci l'ID della foto da cancellare:");
if (!id) return;
const res = await fetch(`${BASE_URL}/delphoto/${id}`, {
method: "DELETE",
headers: { "Authorization": "Bearer " + token }
});
const out = await res.json();
document.getElementById("out").textContent =
JSON.stringify(out, null, 2);
await readDB();
}
async function findIdIndex() {
const id = prompt("Inserisci l'ID da cercare in index.json:");
if (!id) return;
const res = await fetch(`${BASE_URL}/findIdIndex/${id}`, {
headers: { "Authorization": "Bearer " + token }
});
const out = await res.json();
document.getElementById("out").textContent =
JSON.stringify(out, null, 2);
}
async function resetDBuser() {
let url = `${BASE_URL}/initDBuser`;
// Se Admin → chiedi quale utente cancellare
const payload = parseJwt(token);
if (payload.name === "Admin") {
const user = prompt("Inserisci il nome dell'utente da cancellare:");
if (!user) return;
url += `?user=${encodeURIComponent(user)}`;
}
await fetch(url, {
headers: { "Authorization": "Bearer " + token }
});
await readDB();
}
// Utility per leggere il token JWT
function parseJwt(t) {
try {
return JSON.parse(atob(t.split('.')[1]));
} catch {
return {};
}
}
async function loadConfig() {
const res = await fetch('/config');
const cfg = await res.json();
BASE_URL = cfg.baseUrl;
}
async function readDB() {
const res = await fetch(`${BASE_URL}/photos`, {
headers: { "Authorization": "Bearer " + token }
});
db = await res.json();
document.getElementById("out").textContent = JSON.stringify(db, null, 2);
}
async function scan() {
await fetch(`${BASE_URL}/scan`, {
headers: { "Authorization": "Bearer " + token }
});
await readDB();
}
async function resetDB() {
await fetch(`${BASE_URL}/initDB`, {
headers: { "Authorization": "Bearer " + token }
});
await readDB();
}
window.onload = async () => {
await loadConfig();
};
</script>
</body>
</html>