diff --git a/server/app.js b/server/app.js index 5788a2f..5fb7811 100755 --- a/server/app.js +++ b/server/app.js @@ -113,6 +113,7 @@ app.use(morgan("common")); app.use(basePath + "api/v1/photos", require("./routes/photos")); app.use(basePath + "api/v1/days", require("./routes/days")); 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 */ app.use(basePath + "/*", index); @@ -130,7 +131,8 @@ require("./db/photos").then(function(photoDB) { return photoDB; }).then(function(photoDB) { console.log("Scanning."); - return scanner.scan(photoDB); + scanner.init(photoDB); + return scanner.scan(); }).then(function() { console.log("Scanning completed."); }).catch(function(error) { diff --git a/server/routes/scan.js b/server/routes/scan.js new file mode 100644 index 0000000..6955e9a --- /dev/null +++ b/server/routes/scan.js @@ -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; +}; + \ No newline at end of file diff --git a/server/scanner.js b/server/scanner.js index 55a1853..678595b 100755 --- a/server/scanner.js +++ b/server/scanner.js @@ -626,6 +626,7 @@ function computeHash(filepath) { }); } +let scanning = false; 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 @@ -646,6 +647,10 @@ function doScan() { let now = Date.now(); let needsProcessing = []; + if (scanning) { + return Promise.resolve("scanning"); + } + return photoDB.sequelize.query("SELECT max(scanned) AS scanned FROM photos", { type: photoDB.sequelize.QueryTypes.SELECT }).then(function(results) { @@ -752,12 +757,17 @@ function doScan() { }).then(function() { console.log("Total time to initialize DB and all scans: " + ((Date.now() - initialized) / 1000) + "s"); }); + }).then(function() { + scanning = false; + return "scan complete"; }); } module.exports = { - scan: function (db) { + init: function(db) { photoDB = db; + }, + scan: function () { return doScan(); } };