photo_server_json_flutter_c.../public/admin.html
2026-03-23 12:31:01 +01:00

278 lines
No EOL
6.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Photo Manager</title>
<style>
#progressContainer {
width: 100%;
background: #ddd;
height: 25px;
border-radius: 5px;
margin-top: 20px;
overflow: hidden;
}
#progressBar {
height: 100%;
width: 0%;
background: #4caf50;
transition: width 0.3s linear;
}
#scanInfo {
font-family: monospace;
margin-top: 10px;
font-size: 16px;
}
#changesBox {
margin-top: 20px;
padding: 10px;
border: 1px solid #aaa;
border-radius: 5px;
background: #f7f7f7;
width: 350px;
}
</style>
</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="searchPhotoById()">Cerca Foto (nuovo /byIds)</button>
<button onclick="window.location.href='index.html'">Torna alla galleria</button>
<!-- 🔥 NUOVO BLOCCO: /photos/changes -->
<div id="changesBox">
<h4>Controlla /photos/changes</h4>
<label>Since (data/ora):</label><br>
<input type="datetime-local" id="sinceInput" style="width: 100%; margin-top:5px;"><br><br>
<button onclick="showChanges()">Mostra cambiamenti</button>
</div>
<!-- Barra di avanzamento -->
<div id="progressContainer">
<div id="progressBar"></div>
</div>
<!-- Info avanzamento -->
<div id="scanInfo">
<div id="scanProgress"></div>
<div id="scanEta"></div>
</div>
<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 = [];
if (!token) {
window.location.href = "index.html";
}
// -------------------------
// FUNZIONI ESISTENTI
// -------------------------
async function searchPhotoById() {
const id = prompt("Inserisci l'ID della foto da cercare:");
if (!id) return;
const payload = parseJwt(token);
const user = payload.name;
const url = `${BASE_URL}/photos/byIds?id=${encodeURIComponent(id)}&user=${encodeURIComponent(user)}`;
console.log("🔍 [searchPhotoById] URL chiamato:", url);
const res = await fetch(url, {
headers: { "Authorization": "Bearer " + token }
});
const out = await res.json();
console.log("🔍 [searchPhotoById] Risposta:", out);
document.getElementById("out").textContent =
JSON.stringify(out, null, 2);
}
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`;
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();
}
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 resetDB() {
await fetch(`${BASE_URL}/initDB`, {
headers: { "Authorization": "Bearer " + token }
});
await readDB();
}
// -------------------------
// NUOVA FUNZIONE: /photos/changes
// -------------------------
async function showChanges() {
const sinceInput = document.getElementById("sinceInput").value;
if (!sinceInput) {
alert("Seleziona una data/ora valida.");
return;
}
// Convertiamo datetime-local → ISO
const sinceISO = new Date(sinceInput).toISOString();
const payload = parseJwt(token);
const user = payload.name;
const url = `${BASE_URL}/photos/changes?since=${encodeURIComponent(sinceISO)}&user=${encodeURIComponent(user)}`;
const res = await fetch(url, {
headers: { "Authorization": "Bearer " + token }
});
const out = await res.json();
document.getElementById("out").textContent =
JSON.stringify(out, null, 2);
}
// -------------------------
// SCAN + BARRA
// -------------------------
let scanInterval = null;
async function scan() {
fetch(`${BASE_URL}/scan`, {
headers: { "Authorization": "Bearer " + token }
});
startScanStatusPolling();
}
async function startScanStatusPolling() {
if (scanInterval) clearInterval(scanInterval);
scanInterval = setInterval(async () => {
try {
const res = await fetch(`/photos/scan_status.json?ts=` + Date.now());
if (!res.ok) return;
const data = await res.json();
document.getElementById("scanProgress").textContent =
`Progresso: ${data.current}/${data.total} (${data.percent}%)`;
document.getElementById("scanEta").textContent =
`Tempo stimato rimanente: ${data.eta}`;
document.getElementById("progressBar").style.width = data.percent + "%";
if (data.current >= data.total && data.total > 0) {
clearInterval(scanInterval);
scanInterval = null;
await readDB();
}
} catch (err) {
console.log("Errore polling scan:", err);
}
}, 1000);
}
window.onload = async () => {
await loadConfig();
};
</script>
</body>
</html>