Continued re-implementing scanning to it can be used for periodic re-scan and update

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2018-09-23 18:07:20 -07:00
parent 7730ebb28b
commit f3af3a5131
3 changed files with 60 additions and 21 deletions

View File

@ -42,6 +42,7 @@
"iron-icons": "^2", "iron-icons": "^2",
"iron-resizable-behavior": "^2", "iron-resizable-behavior": "^2",
"iron-iconset": "^2", "iron-iconset": "^2",
"iron-overlay-behavior": "2" "iron-overlay-behavior": "2",
"paper-input": "^2"
} }
} }

View File

@ -18,7 +18,7 @@ const express = require("express"),
require("./console-line.js"); /* Monkey-patch console.log with line numbers */ require("./console-line.js"); /* Monkey-patch console.log with line numbers */
const picturesPath = config.get("picturesPath"), const picturesPath = config.get("picturesPath").replace(/\/$/, ""),
serverConfig = config.get("server"); serverConfig = config.get("server");
let basePath = config.get("basePath"); let basePath = config.get("basePath");

76
server/scanner.js Normal file → Executable file
View File

@ -9,7 +9,7 @@ let scanning = 0;
let photoDB = null; let photoDB = null;
const picturesPath = config.get("picturesPath"); const picturesPath = config.get("picturesPath").replace(/\/$/, "");
const processQueue = [], triedClean = []; const processQueue = [], triedClean = [];
@ -465,13 +465,12 @@ function triggerWatcher() {
function scanDir(parent, path) { function scanDir(parent, path) {
let re = new RegExp("\.((" + extensions.join(")|(") + "))$", "i"), let re = new RegExp("\.((" + extensions.join(")|(") + "))$", "i"),
album = { album = {
path: path.slice(picturesPath.length), path: path.slice(picturesPath.length) || "/",
name: path.replace(/.*\//, "").replace(/_/g, " "), name: path.replace(/.*\//, "").replace(/_/g, " "),
assets: [],
parent: parent, parent: parent,
allAssetCount: 0, allAssetCount: 0,
allAlbumCount: 0 allAlbumCount: 0
}; }, albums = [ album ], assets = [];
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
fs.readdir(path, function(error, files) { fs.readdir(path, function(error, files) {
@ -508,9 +507,11 @@ function scanDir(parent, path) {
return stat(filepath).then(function(stats) { return stat(filepath).then(function(stats) {
if (stats.isDirectory()) { if (stats.isDirectory()) {
return scanDir(album, filepath).then(function(child) { return scanDir(album, filepath).spread(function(_albums, _assets) {
album.allAssetCount += child.allAssetCount; album.allAssetCount += _assets.length;
album.allAlbumCount += child.allAlbumCount + 1; album.allAlbumCount += _albums.length + 1;
albums = albums.concat(_albums);
assets = assets.concat(_assets);
}).catch(function(error) { }).catch(function(error) {
console.warn("Could not scanDir " + filepath + ": " + error); console.warn("Could not scanDir " + filepath + ": " + error);
}); });
@ -521,8 +522,7 @@ function scanDir(parent, path) {
return; return;
} }
album.allAssetCount++; assets.push({
album.assets.push({
path: path.slice(picturesPath.length), path: path.slice(picturesPath.length),
filename: file.replace(rawExtension, ".jpg"), /* We will be converting from NEF/ORF => JPG */ filename: file.replace(rawExtension, ".jpg"), /* We will be converting from NEF/ORF => JPG */
stats: stats stats: stats
@ -530,27 +530,65 @@ function scanDir(parent, path) {
}); });
}); });
}).then(function() { }).then(function() {
return album; return [ albums, assets ];
}); });
} }
function findOrCreateDBAlbum(album) {
let query = "SELECT id FROM albums WHERE path=:path AND ";
if (!album.parent) {
query += "parentId IS NULL";
} else {
if (!album.parent.id) {
let error = "Albums in array in non ancestral order!";
console.error(error);
throw error;
}
album.parentId = album.parent.id;
query += "parentId=:parentId";
}
return photoDB.sequelize.query(query, {
replacements: album,
type: photoDB.sequelize.QueryTypes.SELECT
}).then(function(results) {
if (results.length == 0) {
return photoDB.sequelize.query("INSERT INTO albums (path,parentId,name) VALUES(:path,:parent,:name)", {
replacements: replacements
}).then(function(results) {
return results[1].lastID;
});
} else {
return results[0].id;
}
}).then(function(id) {
album.parentId = id;
return id;
});
}
module.exports = { module.exports = {
scan: function (db) { scan: function (db) {
photoDB = db; photoDB = db;
/* 1. Scan for all assets which will be managed by the system. readdir /* 1. Scan for all assets which will be managed by the system. readdir
- returns set of albums and photos. * 2. Check if entry in DB. Check mod-time in DB vs. stats from #1
* 2. Compute HASH of the file * 3. If not in DB, or mod-time changed, compute HASH of the file
* 3. Check for HASH in photohash -- skip? * 4. Check for HASH in photohash -- skip?
* 4. Check for and create thumbs/FILE thumbs/scaled/FILE * 5. Check for and create thumbs/FILE thumbs/scaled/FILE
* 5. If necessary, create JPG from RAW * 6. If necessary, create JPG from RAW
* 6. Check in DB and update last-scanned date * 7. Update last-scanned date in DB for entry
* 7. Look up all DB entries with last-scanned date < NOW -- purge? * 8. Look up all DB entries with last-scanned date < NOW -- purge from DB (they were
* removed on disk)? Also purge from the HASH table.
*/ */
let now = Date.now(); let now = Date.now();
return scanDir(null, picturesPath).then(function(albums) { return scanDir(null, picturesPath).spread(function(albums, assets) {
console.log("Found " + albums.allAssetCount + " assets in " + albums.allAlbumCount + " albums after " + console.log("Found " + assets.length + " assets in " + albums.length + " albums after " +
((Date.now() - now) / 1000) + "s"); ((Date.now() - now) / 1000) + "s");
return Promise.map(albums, function(album) {
return db.sequelize
}, {
concurrency: 5
});
/*triggerWatcher();*/ /*triggerWatcher();*/
}); });
} }