Added scan API

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2018-09-30 22:29:23 -07:00
parent 085cc5a772
commit 44924b3dc7
3 changed files with 63 additions and 2 deletions

View File

@ -113,6 +113,7 @@ app.use(morgan("common"));
app.use(basePath + "api/v1/photos", require("./routes/photos")); app.use(basePath + "api/v1/photos", require("./routes/photos"));
app.use(basePath + "api/v1/days", require("./routes/days")); app.use(basePath + "api/v1/days", require("./routes/days"));
app.use(basePath + "api/v1/albums", require("./routes/albums")); app.use(basePath + "api/v1/albums", require("./routes/albums"));
app.use(basePath + "api/v1/scan", require("./routes/scan")(scanner));
/* Declare the "catch all" index route last; the final route is a 404 dynamic router */ /* Declare the "catch all" index route last; the final route is a 404 dynamic router */
app.use(basePath + "/*", index); app.use(basePath + "/*", index);
@ -130,7 +131,8 @@ require("./db/photos").then(function(photoDB) {
return photoDB; return photoDB;
}).then(function(photoDB) { }).then(function(photoDB) {
console.log("Scanning."); console.log("Scanning.");
return scanner.scan(photoDB); scanner.init(photoDB);
return scanner.scan();
}).then(function() { }).then(function() {
console.log("Scanning completed."); console.log("Scanning completed.");
}).catch(function(error) { }).catch(function(error) {

49
server/routes/scan.js Normal file
View File

@ -0,0 +1,49 @@
"use strict";
const express = require("express"),
fs = require("fs"),
url = require("url"),
config = require("config"),
moment = require("moment");
let photoDB;
require("../db/photos").then(function(db) {
photoDB = db;
});
const router = express.Router();
let scanner = null;
router.get("/", function(req, res/*, next*/) {
console.log("Scanning.");
let responded = false;
if (!scanner) {
return res.status(500).send("Not yet initialized.");
}
scanner.scan().then(function(results) {
if (responded) {
return;
}
responded = true;
return res.status(200).send(results);
});
setTimeout(function() {
if (responded) {
return;
}
responded = true;
return res.status(200).send("scan initiated");
}, 50);
});
module.exports = function(_scanner) {
scanner = _scanner;
return router;
};

View File

@ -626,6 +626,7 @@ function computeHash(filepath) {
}); });
} }
let scanning = false;
function doScan() { function doScan() {
/* 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
* 2. Check if entry in DB. Check mod-time in DB vs. stats from #1 * 2. Check if entry in DB. Check mod-time in DB vs. stats from #1
@ -646,6 +647,10 @@ function doScan() {
let now = Date.now(); let now = Date.now();
let needsProcessing = []; let needsProcessing = [];
if (scanning) {
return Promise.resolve("scanning");
}
return photoDB.sequelize.query("SELECT max(scanned) AS scanned FROM photos", { return photoDB.sequelize.query("SELECT max(scanned) AS scanned FROM photos", {
type: photoDB.sequelize.QueryTypes.SELECT type: photoDB.sequelize.QueryTypes.SELECT
}).then(function(results) { }).then(function(results) {
@ -752,12 +757,17 @@ function doScan() {
}).then(function() { }).then(function() {
console.log("Total time to initialize DB and all scans: " + ((Date.now() - initialized) / 1000) + "s"); console.log("Total time to initialize DB and all scans: " + ((Date.now() - initialized) / 1000) + "s");
}); });
}).then(function() {
scanning = false;
return "scan complete";
}); });
} }
module.exports = { module.exports = {
scan: function (db) { init: function(db) {
photoDB = db; photoDB = db;
},
scan: function () {
return doScan(); return doScan();
} }
}; };