diff --git a/server/scanner.js b/server/scanner.js index 09bc3ff..cdf39f2 100755 --- a/server/scanner.js +++ b/server/scanner.js @@ -311,22 +311,36 @@ const processBlock = async (items) => { asset.hash = await computeHash( picturesPath + asset.album.path + asset.filename) - let results = await photoDB.sequelize.query( - "SELECT photohashes.*,photos.filename,albums.path FROM photohashes " + - "LEFT JOIN photos ON (photos.id=photohashes.photoId) " + - "LEFT JOIN albums ON (albums.id=photos.albumId) " + - "WHERE hash=:hash OR photoId=:id", { - replacements: asset, - type: photoDB.sequelize.QueryTypes.SELECT - }); - - let query; + /* Writes to DB for new assets hasn't happened yet, so the DB + * won't have new duplicates */ + let duplicate = insertHash.find(entry => entry.hash === asset.hash) + || updateHash.find(entry => entry.hash === asset.hash); - if (results.length == 0) { - insertHash.push(asset); - } else if (results[0].hash != asset.hash) { - updateHash.push(asset); - } else if (results[0].photoId != asset.id) { + + let results = []; + + /* In addition to checking new entries, check the DB for old + * duplicates */ + if (!duplicate) { + results = await photoDB.sequelize.query( + "SELECT photohashes.*,photos.filename,albums.path FROM photohashes " + + "LEFT JOIN photos ON (photos.id=photohashes.photoId) " + + "LEFT JOIN albums ON (albums.id=photos.albumId) " + + "WHERE hash=:hash OR photoId=:id", { + replacements: asset, + type: photoDB.sequelize.QueryTypes.SELECT + }); + + if (results.length == 0) { + insertHash.push(asset); + } else if (results[0].hash != asset.hash) { + updateHash.push(asset); + } else if (results[0].photoId != asset.id) { + duplicate = true; + } + } + + if (duplicate) { setStatus("Duplicate asset: " + "'" + asset.album.path + asset.filename + "' is a copy of " + "'" + results[0].path + results[0].filename + "'"); @@ -337,13 +351,6 @@ const processBlock = async (items) => { return; /* Done processing this asset (DUPLICATE) */ } - /* Update PHOTOHASH table if necessary */ - if (query) { - await photoDB.sequelize.query(query, { - replacements: asset, - }); - } - /* Additional processing is only skipped if the asset was a * DUPLICATE above (the empty "return;") */