112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
import readline from "readline";
|
|
import fs from "fs";
|
|
import dotenv from "dotenv";
|
|
|
|
dotenv.config();
|
|
|
|
const user = process.argv[2];
|
|
|
|
// ===============================
|
|
// BASE_URL DAL .env
|
|
// ===============================
|
|
const BASE_URL = process.env.BASE_URL;
|
|
if (!BASE_URL) {
|
|
console.error("ERRORE: BASE_URL non definita in .env");
|
|
process.exit(1);
|
|
}
|
|
|
|
// ===============================
|
|
// TOKEN JWT STATICO PER IL WATCHER
|
|
// ===============================
|
|
const WATCHER_TOKEN = process.env.WATCHER_TOKEN;
|
|
|
|
if (!WATCHER_TOKEN) {
|
|
console.error("❌ ERRORE: WATCHER_TOKEN non definito nel .env");
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`Node attivo per utente: ${user}`);
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
crlfDelay: Infinity
|
|
});
|
|
|
|
// ===============================
|
|
// ESTENSIONI MEDIA
|
|
// ===============================
|
|
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));
|
|
}
|
|
|
|
// ===============================
|
|
// CHIAMATA A /auto_scan
|
|
// ===============================
|
|
async function callAutoScan(type, file, path, user) {
|
|
|
|
const headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer " + WATCHER_TOKEN // 🔥 token fisso
|
|
};
|
|
|
|
path = path.replace(/^\/app/, "");
|
|
|
|
const res = await fetch(`${BASE_URL}/api_v1/auto_scan`, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify({ type, file, path, user })
|
|
});
|
|
|
|
if (!res.ok) {
|
|
console.error(`❌ Errore auto_scan: ${res.status}`);
|
|
}
|
|
|
|
return res.ok;
|
|
}
|
|
|
|
// ===============================
|
|
// LOGICA EVENTI
|
|
// ===============================
|
|
function startWatcher() {
|
|
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");
|
|
|
|
if (!isDir && !isMediaFile(file)) {
|
|
console.log(`Ignorato (non media): ${file}`);
|
|
return;
|
|
}
|
|
|
|
let type = null;
|
|
|
|
if (action === "MOVED_TO,ISDIR") type = "ADD_DIR";
|
|
else if (action === "MOVED_FROM,ISDIR") type = "DEL_DIR";
|
|
else if (/^CLOSE_WRITE(,CLOSE)?$/.test(action)) type = "ADD";
|
|
else if (action === "MOVED_TO") type = "ADD";
|
|
else if (action === "MOVED_FROM") type = "DEL";
|
|
else if (action === "DELETE") type = "DEL";
|
|
else return;
|
|
|
|
console.log(`${type} ${file} ${path} ${user}`);
|
|
|
|
callAutoScan(type, file, path, user);
|
|
});
|
|
}
|
|
|
|
// ===============================
|
|
// AVVIO WATCHER
|
|
// ===============================
|
|
console.log(`Watcher per ${user} avviato con token JWT`);
|
|
startWatcher();
|