photo_server_json_flutter_c.../watcher_logic3.mjs
2026-03-23 12:31:01 +01:00

84 lines
1.8 KiB
JavaScript

import readline from "readline";
const user = process.argv[2];
// Estensioni foto/video
const photoExt = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".tiff", ".heic", ".heif"];
const videoExt = [".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4v"];
function isMediaFile(filename) {
const lower = filename.toLowerCase();
return photoExt.some(ext => lower.endsWith(ext)) ||
videoExt.some(ext => lower.endsWith(ext));
}
console.log(`Node attivo per utente: ${user}`);
const rl = readline.createInterface({
input: process.stdin,
crlfDelay: Infinity
});
rl.on("line", line => {
const parts = line.trim().split(/\s+/);
const path = parts[0];
const action = parts[1];
const file = parts[2];
if (!file) return;
const isDir = action.includes("ISDIR");
// 🔥 Se è directory → NON controllare mediafile
if (!isDir && !isMediaFile(file)) {
console.log(`Ignorato (non media): ${file}`);
return;
}
let type = null;
//
// 🔵 DIRECTORY EVENTS
//
// ADD_DIR → MOVED_TO,ISDIR
if (action === "MOVED_TO,ISDIR") {
type = "ADD_DIR";
}
// DEL_DIR → MOVED_FROM,ISDIR
else if (action === "MOVED_FROM,ISDIR") {
type = "DEL_DIR";
}
//
// 🔵 FILE EVENTS
//
// ADD → CLOSE_WRITE o CLOSE_WRITE,CLOSE
else if (/^CLOSE_WRITE(,CLOSE)?$/.test(action)) {
type = "ADD";
}
// ADD → MOVED_TO
else if (action === "MOVED_TO") {
type = "ADD";
}
// DEL → MOVED_FROM
else if (action === "MOVED_FROM") {
type = "DEL";
}
// DEL → DELETE
else if (action === "DELETE") {
type = "DEL";
}
else {
return;
}
console.log(`${type} ${file} ${path} ${user}`);
});