144 lines
No EOL
3.6 KiB
JavaScript
144 lines
No EOL
3.6 KiB
JavaScript
// api_v1/scanner/scanPhotoSingle.js
|
|
const processFile = require('./processFile');
|
|
const { sha256 } = require('./utils');
|
|
const { log } = require('./logger');
|
|
|
|
async function scanPhotoSingle(db, user, cartella, f, newFiles, prefix = '') {
|
|
const fileName = f.name;
|
|
const id = f.id;
|
|
const st = f.stat;
|
|
|
|
// Recupero precedente SENZA contentHash
|
|
let prev = await db("photos")
|
|
.select("id", "size_bytes", "mtimeMs", "fast_hash", "path")
|
|
.where({ id })
|
|
.first();
|
|
|
|
// Hash veloce basato su size + mtime
|
|
const fastHash = sha256(`${st.size}-${st.mtimeMs}`);
|
|
|
|
// ---------------------------------------------------------
|
|
// PATH CAMBIATO = NUOVO FILE
|
|
// ---------------------------------------------------------
|
|
if (prev && prev.path !== f.path) {
|
|
log(`${prefix} 🟢 Nuovo/Modificato (path changed): ${fileName}`);
|
|
prev = null;
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// NUOVO FILE
|
|
// ---------------------------------------------------------
|
|
if (!prev) {
|
|
log(`${prefix} 🟢 Nuovo: ${fileName}`);
|
|
|
|
const meta = await processFile(
|
|
user,
|
|
cartella,
|
|
f.relPath,
|
|
f.absPath,
|
|
f.ext,
|
|
st
|
|
);
|
|
|
|
meta.id = id;
|
|
meta.path = f.path;
|
|
|
|
await db("photos").insert({
|
|
id: meta.id,
|
|
user,
|
|
cartella,
|
|
name: meta.name,
|
|
path: meta.path,
|
|
thub1: meta.thub1,
|
|
thub2: meta.thub2,
|
|
mime_type: meta.mime_type,
|
|
width: meta.width,
|
|
height: meta.height,
|
|
rotation: meta.rotation,
|
|
size_bytes: meta.size_bytes,
|
|
mtimeMs: meta.mtimeMs,
|
|
duration_ms: meta.duration_ms,
|
|
taken_at: meta.taken_at,
|
|
data: meta.data,
|
|
lat: meta.gps?.lat ?? null,
|
|
lon: meta.gps?.lng ?? null,
|
|
alt: meta.gps?.alt ?? null,
|
|
location: meta.location ? JSON.stringify(meta.location) : null,
|
|
fast_hash: fastHash,
|
|
|
|
// ⭐ AGGIUNTA FONDAMENTALE
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
});
|
|
|
|
newFiles.push(meta);
|
|
return meta;
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// IDENTICO (fastHash uguale)
|
|
// ---------------------------------------------------------
|
|
if (prev.fast_hash === fastHash) {
|
|
await db("photos")
|
|
.where({ id })
|
|
.update({
|
|
path: f.path,
|
|
size_bytes: st.size,
|
|
mtimeMs: st.mtimeMs,
|
|
fast_hash: fastHash,
|
|
|
|
// ⭐ AGGIUNTA: anche un file identico deve aggiornare updated_at
|
|
updated_at: new Date().toISOString()
|
|
});
|
|
|
|
return null;
|
|
}
|
|
|
|
// ---------------------------------------------------------
|
|
// MODIFICATO (fastHash diverso)
|
|
// ---------------------------------------------------------
|
|
log(`${prefix} 🟠 Modificato: ${fileName}`);
|
|
|
|
const meta = await processFile(
|
|
user,
|
|
cartella,
|
|
f.relPath,
|
|
f.absPath,
|
|
f.ext,
|
|
st
|
|
);
|
|
|
|
meta.id = id;
|
|
meta.path = f.path;
|
|
|
|
await db("photos")
|
|
.where({ id })
|
|
.update({
|
|
name: meta.name,
|
|
path: meta.path,
|
|
thub1: meta.thub1,
|
|
thub2: meta.thub2,
|
|
mime_type: meta.mime_type,
|
|
width: meta.width,
|
|
height: meta.height,
|
|
rotation: meta.rotation,
|
|
size_bytes: meta.size_bytes,
|
|
mtimeMs: meta.mtimeMs,
|
|
duration_ms: meta.duration_ms,
|
|
taken_at: meta.taken_at,
|
|
data: meta.data,
|
|
lat: meta.gps?.lat ?? null,
|
|
lon: meta.gps?.lng ?? null,
|
|
alt: meta.gps?.alt ?? null,
|
|
location: meta.location ? JSON.stringify(meta.location) : null,
|
|
fast_hash: fastHash,
|
|
|
|
// ⭐ AGGIUNTA FONDAMENTALE
|
|
updated_at: new Date().toISOString()
|
|
});
|
|
|
|
newFiles.push(meta);
|
|
return meta;
|
|
}
|
|
|
|
module.exports = scanPhotoSingle; |