// api_v1/scanner/scanNewCartella.js const path = require('path'); const fsp = require('fs/promises'); const { WEB_ROOT, SUPPORTED_EXTS } = require('../config'); const scanPhoto = require('./scanPhoto'); // --------------------------------------------------------- // Conta ricorsivamente i file in /photos//original// // --------------------------------------------------------- async function countFilesInUserFolder(userName, folderName) { const rootDir = path.join(WEB_ROOT, userName, "original", folderName); let count = 0; async function walk(dir) { let entries = []; try { entries = await fsp.readdir(dir, { withFileTypes: true }); } catch { return; } for (const e of entries) { const abs = path.join(dir, e.name); if (e.isDirectory()) { await walk(abs); } else { const ext = path.extname(e.name).toLowerCase(); if (SUPPORTED_EXTS.has(ext)) { count++; } } } } await walk(rootDir); return count; } // --------------------------------------------------------- // Crea thumbs/ e scansiona la cartella come fa /scan // --------------------------------------------------------- async function scanNewCartella(userName, folderName, db) { // 1) crea la cartella root in thumbs const thumbsDir = path.join(WEB_ROOT, userName, "thumbs", folderName); await fsp.mkdir(thumbsDir, { recursive: true }); // 2) conta i file ricorsivamente const TOTAL_FILES = await countFilesInUserFolder(userName, folderName); // 3) esegui lo scan della cartella const CURRENT = { value: 0 }; const start = Date.now(); const newFiles = await scanPhoto( folderName, userName, db, CURRENT, TOTAL_FILES, start ); return { ok: true, folder: folderName, totalFiles: TOTAL_FILES, newFiles }; } module.exports = { scanNewCartella, countFilesInUserFolder };