gallery_server_client_s190js/routes/scan.js
2026-10-26 11:00:14 +01:00

49 lines
1.2 KiB
JavaScript

'use strict';
const express = require('express');
const router = express.Router();
const scanPhotosUser = require('../api_v1/scanner/scanPhotosUser');
const photoScanProgress = require('../api_v1/scanner/photoScanProgress');
const db = require('../db/knex');
const fs = require('fs');
const userdb = JSON.parse(fs.readFileSync('./api_v1/users.json', 'utf-8'));
router.get('/', async (req, res) => {
let scanStarted = false;
try {
await photoScanProgress.beginGlobalPhotoScan(db);
scanStarted = true;
const loggedUser = req.user.name;
if (loggedUser === 'Admin') {
await scanPhotosUser('Common', db);
for (const u of userdb.users) {
await scanPhotosUser(u.name, db);
}
return res.send({
status: 'Scansione completata',
user: 'Admin',
scope: 'tutti gli utenti + Common'
});
}
await scanPhotosUser(loggedUser, db);
return res.send({
status: 'Scansione completata',
user: loggedUser,
scope: 'utente corrente'
});
} catch (err) {
return res.status(500).json({
error: 'Errore durante lo scan',
details: err.message
});
} finally {
if (scanStarted) photoScanProgress.finishGlobalPhotoScan();
}
});
module.exports = router;