scanner::scan can now be called dynamically to re-scan

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2018-09-30 21:43:18 -07:00
parent b7459e3157
commit 085cc5a772

View File

@ -11,7 +11,7 @@ let photoDB = null;
const picturesPath = config.get("picturesPath").replace(/\/$/, "") + "/";
let processQueue = [], triedClean = [], lastScan;
let processQueue = [], triedClean = [], lastScan = new Date("1900-01-01");
//const rawExtension = /\.(nef|orf)$/i, extensions = [ "jpg", "jpeg", "png", "gif", "nef", "orf" ];
@ -220,7 +220,7 @@ function processBlock(items) {
if (results.length == 0) {
query = "INSERT INTO photohashes (hash,photoId) VALUES(:hash,:id)";
} else if (results[0].hash != asset.hash) {
query = "UPDATE photohashes SET hash=:hash WHERE photoId=:id)";
query = "UPDATE photohashes SET hash=:hash WHERE photoId=:id";
} else if (results[0].photoId != asset.id) {
console.log("Duplicate asset: " +
"'" + asset.album.path + asset.filename + "' is a copy of " +
@ -517,7 +517,8 @@ function scanDir(parent, path) {
filename: file.replace(rawExtension, ".jpg"), /* We will be converting from NEF/ORF => JPG */
name: file.replace(/.[^.]*$/, ""),
stats: {
mtime: stats.mtime
mtime: stats.mtime,
ctime: stats.ctime
},
album: album
});
@ -573,7 +574,7 @@ function findOrCreateDBAlbum(transaction, album) {
}
function findOrUpdateDBAsset(transaction, asset) {
let query = "SELECT id,scanned,modified FROM photos WHERE albumId=:albumId AND filename=:filename";
let query = "SELECT id,DATETIME(scanned) AS scanned,DATETIME(modified) AS modified FROM photos WHERE albumId=:albumId AND filename=:filename";
if (!asset.album || !asset.album.id) {
let error = "Asset being processed without an album";
console.error(error);
@ -595,8 +596,8 @@ function findOrUpdateDBAsset(transaction, asset) {
});
} else {
asset.id = results[0].id;
asset.scanned = results[0].scanned;
asset.modified = results[0].modified;
asset.scanned = new Date(results[0].scanned);
asset.modified = new Date(results[0].modified);
}
}).then(function() {
return asset;
@ -625,9 +626,7 @@ function computeHash(filepath) {
});
}
module.exports = {
scan: function (db) {
photoDB = db;
function doScan() {
/* 1. Scan for all assets which will be managed by the system. readdir
* 2. Check if entry in DB. Check mod-time in DB vs. stats from #1
* - For albums
@ -647,7 +646,16 @@ module.exports = {
let now = Date.now();
let needsProcessing = [];
lastScan = new Date();
return photoDB.sequelize.query("SELECT max(scanned) AS scanned FROM photos", {
type: photoDB.sequelize.QueryTypes.SELECT
}).then(function(results) {
if (results[0].scanned == null) {
lastScan = new Date("1800-01-01");
} else {
lastScan = new Date(results[0].scanned);
}
console.log("Updating any asset newer than " + moment(lastScan).format());
}).then(function() {
return scanDir(null, picturesPath).spread(function(albums, assets) {
console.log("Found " + assets.length + " assets in " + albums.length + " albums after " +
((Date.now() - now) / 1000) + "s");
@ -676,15 +684,32 @@ module.exports = {
let processed = 0, start = Date.now(), last = 0, updateScanned = [], newEntries = 0;
return photoDB.sequelize.transaction(function(transaction) {
return Promise.map(assets, function(asset) {
return new Promise(function(resolve, reject) {
/* If both mtime and ctime of the asset are older than the lastScan,
* skip this asset. */
if (lastScan != null && asset.stats.mtime < lastScan && asset.stats.ctime < lastScan) {
updateScanned.push(asset.id);
return resolve(asset);
}
return findOrUpdateDBAsset(transaction, asset).then(function(asset) {
if (!asset.scanned) {
newEntries++;
}
console.log(typeof asset.scanned);
if (!asset.scanned || asset.scanned < asset.stats.mtime || !asset.modified) {
console.log("Process asset: " + asset.scanned);
needsProcessing.push(asset);
} else {
console.log("Update scanned tag: " + asset.scanned);
updateScanned.push(asset.id);
}
return asset;
}).then(function(asset) {
return resolve(asset);
}).catch(function(error) {
return reject(error);
});
}).then(function(asset) {
processed++;
@ -727,5 +752,13 @@ module.exports = {
}).then(function() {
console.log("Total time to initialize DB and all scans: " + ((Date.now() - initialized) / 1000) + "s");
});
});
}
module.exports = {
scan: function (db) {
photoDB = db;
return doScan();
}
};