From a391848ec96f5544d1a3d3f0ce09c6057d1223b5 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sun, 7 Oct 2018 15:44:33 -0700 Subject: [PATCH] Partial size tracking Signed-off-by: James Ketrenos --- server/scanner.js | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/server/scanner.js b/server/scanner.js index 4938a55..0eabebc 100755 --- a/server/scanner.js +++ b/server/scanner.js @@ -575,31 +575,42 @@ function findOrCreateDBAlbum(transaction, album) { } function findOrUpdateDBAsset(transaction, asset) { - 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); throw error; } + asset.albumId = asset.album.id; - return photoDB.sequelize.query(query, { + + return photoDB.sequelize.query( + "SELECT id,DATETIME(scanned) AS scanned,size,DATETIME(modified) AS modified " + + "FROM photos " + + "WHERE albumId=:albumId AND filename=:filename", { replacements: asset, type: photoDB.sequelize.QueryTypes.SELECT }).then(function(results) { if (results.length == 0) { return photoDB.sequelize.query("INSERT INTO photos " + - "(albumId,filename,name,size) " + - "VALUES(:albumId,:filename,:name,:size)", { + "(albumId,filename,name,size) VALUES(:albumId,:filename,:name,:size)", { replacements: asset, transaction: transaction }).spread(function(results, metadata) { asset.id = metadata.lastID; }); - } else { - asset.id = results[0].id; - asset.scanned = new Date(results[0].scanned); - asset.modified = new Date(results[0].modified); + } + + asset.id = results[0].id; + asset.scanned = new Date(results[0].scanned); + asset.modified = new Date(results[0].modified); + + /* If the size on disk changed, update the size entry in the DB. This shouldn't happen in + * production unless someone modifies the file, then re-stamps the modified time */ + if (asset.size != results[0].size) { + return photoDB.sequelize.query("UPDATE photos SET size=:size WHERE id=:id", { + replacements: asset, + transaction: transaction + }); } }).then(function() { return asset; @@ -681,11 +692,12 @@ function doScan() { 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 it - * Can only do this after a full scan has occurred */ + return Promise.resolve(asset).then(function(asset) { + /* If both mtime and ctime of the asset are older than the lastScan, skip it + * + * Can only do this after a full scan has occurred */ if (lastScan != null && asset.stats.mtime < lastScan && asset.stats.ctime < lastScan) { - return resolve(asset); + return asset; } return findOrUpdateDBAsset(transaction, asset).then(function(asset) { @@ -699,9 +711,7 @@ function doScan() { } return asset; }).then(function(asset) { - return resolve(asset); - }).catch(function(error) { - return reject(error); + return asset; }); }).then(function(asset) { processed++;