// routes/scanStatus.js const express = require('express'); const router = express.Router(); const path = require('path'); const fsp = require('fs/promises'); const photosStatusPath = path.resolve(__dirname, '..', 'public/photos/scan_status.json'); const activitiesStatusPath = path.resolve(__dirname, '..', 'public/activities/scan_status.json'); async function readJsonWithRetry(filePath) { for (let i = 0; i < 3; i++) { try { const raw = await fsp.readFile(filePath, 'utf8'); return JSON.parse(raw); } catch (err) { if (i === 2) throw err; await new Promise(resolve => setTimeout(resolve, 30)); } } } router.get('/photos', async (req, res) => { try { res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate'); res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0'); const data = await readJsonWithRetry(photosStatusPath); res.json(data); } catch (err) { res.status(200).json({ current: 0, total: 0, percent: 0, eta: 'calcolo...', error: err.message }); } }); router.get('/activities', async (req, res) => { try { res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate'); res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0'); const data = await readJsonWithRetry(activitiesStatusPath); res.json(data); } catch (err) { res.status(200).json({ current: 0, total: 0, percent: 0, eta: 'calcolo...', error: err.message }); } }); module.exports = router;