Added scan API
Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
parent
085cc5a772
commit
44924b3dc7
@ -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) {
|
||||
|
49
server/routes/scan.js
Normal file
49
server/routes/scan.js
Normal 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;
|
||||
};
|
||||
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user