45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
// api_v1/scanner/scanActivityFiles.js
|
|
const fsp = require('fs/promises');
|
|
const path = require('path');
|
|
|
|
const SUPPORTED_ACTIVITY_EXTS = new Set(['.fit', '.gpx', '.tcx']);
|
|
|
|
async function* scanActivityFiles(user, cartella, absCartella, db) {
|
|
async function* walk(dir) {
|
|
// Gli errori non vengono nascosti: una scansione incompleta
|
|
// non deve causare la cancellazione delle attività esistenti.
|
|
const entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
|
|
for (const entry of entries) {
|
|
const absPath = path.join(dir, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
yield* walk(absPath);
|
|
continue;
|
|
}
|
|
|
|
if (!entry.isFile()) continue;
|
|
|
|
const ext = path.extname(entry.name).toLowerCase();
|
|
if (!SUPPORTED_ACTIVITY_EXTS.has(ext)) continue;
|
|
|
|
const stat = await fsp.stat(absPath);
|
|
const relPath = path.relative(absCartella, absPath)
|
|
.split(path.sep).join('/');
|
|
|
|
yield {
|
|
id: `${cartella}/${relPath}`,
|
|
file_name: entry.name,
|
|
relPath,
|
|
absPath,
|
|
ext,
|
|
stat,
|
|
path: `/activities/${user}/original/${cartella}/${relPath}`,
|
|
};
|
|
}
|
|
}
|
|
|
|
yield* walk(absCartella);
|
|
}
|
|
|
|
module.exports = scanActivityFiles;
|