Added some DB creation, scanning, and fetching
Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
parent
1e24e5a934
commit
2c427707fa
@ -87,8 +87,8 @@
|
||||
<div>[[path]]</div>
|
||||
</app-header>
|
||||
<div class="thumbnails layout horizontal wrap">
|
||||
<template is="dom-repeat" items="[[photos.files]]">
|
||||
<div style$="background-image:url([[item.filepath]])" on-tap="_imageTap" info="[[item]]"></div>
|
||||
<template is="dom-repeat" items="[[photos]]">
|
||||
<div style$="background-image:url([[item.path]]/[[item.filename]])" on-tap="_imageTap" info="[[item]]"></div>
|
||||
</template>
|
||||
</div>
|
||||
<div class="folders layout horizontal wrap">
|
||||
@ -148,26 +148,24 @@
|
||||
|
||||
var base = document.querySelector("base");
|
||||
if (base) {
|
||||
base = new URL(base.href).pathname;
|
||||
var parts = new RegExp("http.*" + base.replace(/\//, "\\/") + "(.*)").exec(window.location.href);
|
||||
if (parts && parts.length == 2) {
|
||||
this.base = "./" + parts[1];
|
||||
} else {
|
||||
this.base = "";
|
||||
}
|
||||
this.base = new URL(base.href).pathname;
|
||||
} else {
|
||||
this.base = "";
|
||||
}
|
||||
|
||||
photos.forEach(function(photo) {
|
||||
photo.path = this.base + photo.path;
|
||||
}.bind(this));
|
||||
|
||||
function findPath(path, item) {
|
||||
if (path.indexOf(item.filepath) != 0) {
|
||||
if (path.indexOf(item.path) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (path == item.filepath || path == item.filepath + "/") {
|
||||
if (path == item.path || path == item.path + "/") {
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
for (var i = 0; i < item.paths.length; i++) {
|
||||
var tmp = findPath(path, item.paths[i]);
|
||||
if (tmp) {
|
||||
|
@ -6,8 +6,8 @@ const express = require("express"),
|
||||
morgan = require("morgan"),
|
||||
bodyParser = require("body-parser"),
|
||||
config = require("config"),
|
||||
scanner = require("./scanner"),
|
||||
db = require("./db");
|
||||
db = require("./db"),
|
||||
scanner = require("./scanner");
|
||||
|
||||
require("./console-line.js"); /* Monkey-patch console.log with line numbers */
|
||||
|
||||
@ -32,7 +32,7 @@ app.set("trust proxy", true);
|
||||
/* Handle static files first so excessive logging doesn't occur */
|
||||
app.use(basePath, express.static("frontend", { index: false }));
|
||||
|
||||
app.use(basePath, express.static(".", { index: false }));
|
||||
app.use(basePath, express.static(picturesPath, { index: false }));
|
||||
|
||||
app.use(morgan("common"));
|
||||
|
||||
@ -75,9 +75,9 @@ app.set("port", serverConfig.port);
|
||||
|
||||
const server = require("http").createServer(app);
|
||||
|
||||
Promise.all([
|
||||
db, scanner.then(function(files) { app.set("files", files); })
|
||||
]).then(function() {
|
||||
db.then(function(photoDB) {
|
||||
return scanner.scan(photoDB);
|
||||
}).then(function() {
|
||||
console.log("Done scanning. Opening server.");
|
||||
server.listen(serverConfig.port);
|
||||
}).catch(function(error) {
|
||||
|
@ -20,44 +20,31 @@ const fs = require('fs'),
|
||||
|
||||
function init() {
|
||||
const db = {
|
||||
sequelize: new Sequelize(config.get("db.host"), config.get("db.options"))
|
||||
sequelize: new Sequelize(config.get("db.host"), config.get("db.options")),
|
||||
Sequelize: Sequelize
|
||||
};
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
console.log("DB initialization beginning. DB access will block.");
|
||||
let models = [];
|
||||
console.log("DB initialization beginning. DB access will block.");
|
||||
|
||||
/* Load models */
|
||||
fs.readdirSync(__dirname).forEach(function (file) {
|
||||
if (file == "." || file == ".." || file == "index.js") {
|
||||
return;
|
||||
}
|
||||
let model = db.sequelize.import(path.join(__dirname, file));
|
||||
db[model.name] = model;
|
||||
models.push(model);
|
||||
return db.sequelize.authenticate().then(function () {
|
||||
const Photo = db.sequelize.define('photo', {
|
||||
path: Sequelize.STRING,
|
||||
filename: Sequelize.STRING,
|
||||
added: Sequelize.DATE
|
||||
});
|
||||
|
||||
/* After all the models are loaded, associate any that need it */
|
||||
models.forEach(function (model) {
|
||||
if (model.associate) {
|
||||
model.associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
return db.sequelize.authenticate().then(function () {
|
||||
console.log("Connection established successfully with DB.");
|
||||
return db.sequelize.sync({
|
||||
force: false
|
||||
}).then(function () {
|
||||
console.log("DB relationships successfully mapped. DB access unblocked.");
|
||||
return resolve(db);
|
||||
});
|
||||
}).catch(function (error) {
|
||||
console.log("ERROR: Failed to authenticate with DB");
|
||||
console.log("ERROR: " + JSON.stringify(config.get("db"), null, 2));
|
||||
console.log(error);
|
||||
return reject(error);
|
||||
console.log("Connection established successfully with DB.");
|
||||
return db.sequelize.sync({
|
||||
force: false
|
||||
}).then(function () {
|
||||
console.log("DB relationships successfully mapped. DB access unblocked.");
|
||||
return db;
|
||||
});
|
||||
}).catch(function (error) {
|
||||
console.log("ERROR: Failed to authenticate with DB");
|
||||
console.log("ERROR: " + JSON.stringify(config.get("db"), null, 2));
|
||||
console.log(error);
|
||||
return reject(error);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -5,10 +5,33 @@ const express = require("express"),
|
||||
url = require("url"),
|
||||
config = require("config");
|
||||
|
||||
let photoDB;
|
||||
|
||||
require("../db").then(function(db) {
|
||||
photoDB = db;
|
||||
});
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* Each photos has:
|
||||
|
||||
* locations
|
||||
* people
|
||||
* date
|
||||
* tags
|
||||
* photo info
|
||||
|
||||
*/
|
||||
|
||||
router.get("/", function(req, res/*, next*/) {
|
||||
return res.status(200).json(req.app.get("files"));
|
||||
return photoDB.sequelize.query("SELECT path,filename,added FROM photos WHERE path LIKE :path", {
|
||||
replacements: {
|
||||
path: req.url + "%"
|
||||
},
|
||||
type: photoDB.Sequelize.QueryTypes.SELECT
|
||||
}).then(function(photos) {
|
||||
return res.status(200).json(photos);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
@ -1,27 +1,24 @@
|
||||
"use strict";
|
||||
|
||||
const db = require("sequelize"),
|
||||
Promise = require("bluebird"),
|
||||
const Promise = require("bluebird"),
|
||||
fs = require("fs"),
|
||||
config = require("config");
|
||||
|
||||
let scanning = 0;
|
||||
|
||||
let photoDB = null;
|
||||
|
||||
const picturesPath = config.get("picturesPath");
|
||||
|
||||
function scanFile(path, stats) {
|
||||
function scanFile(path, file, stats) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
const meta = { filepath: path };
|
||||
console.log("Scanning file: " + path);
|
||||
meta.created = stats.ctime;
|
||||
return resolve(meta);
|
||||
console.log("Scanning file: " + path + "/" + file);
|
||||
return resolve(true);
|
||||
});
|
||||
}
|
||||
|
||||
function scanDir(path) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
const meta = { filepath: path, files: [], paths: [] };
|
||||
|
||||
console.log("Scanning path " + path);
|
||||
|
||||
fs.readdir(path, function(err, files) {
|
||||
@ -35,29 +32,44 @@ function scanDir(path) {
|
||||
return Promise.map(files, function(file) {
|
||||
let filepath = path + "/" + file;
|
||||
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
fs.stat(filepath, function(err, stats) {
|
||||
if (err) {
|
||||
console.warn("Could not stat " + filepath);
|
||||
return resolve(null);
|
||||
return resolve(false);
|
||||
}
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
return scanDir(filepath, stats).then(function(entry) {
|
||||
if (entry && (entry.files.length || entry.paths.length)) {
|
||||
meta.paths.push(entry);
|
||||
}
|
||||
return resolve(entry);
|
||||
return resolve(true);
|
||||
});
|
||||
}
|
||||
|
||||
/* stats.isFile() */
|
||||
return scanFile(filepath, stats).then(function(entry) {
|
||||
if (entry) {
|
||||
meta.files.push(entry);
|
||||
return scanFile(path, file, stats).then(function(entry) {
|
||||
if (!entry) {
|
||||
return resolve(false);
|
||||
}
|
||||
return resolve(entry);
|
||||
|
||||
const replacements = {
|
||||
path: path.slice(picturesPath.length),
|
||||
filename: file,
|
||||
added: new Date()
|
||||
};
|
||||
|
||||
return photoDB.sequelize.query("SELECT id FROM photos WHERE path=:path AND filename=:filename", {
|
||||
replacements: replacements,
|
||||
type: photoDB.sequelize.QueryTypes.SELECT
|
||||
}).then(function(photo) {
|
||||
if (photo.length == 0) {
|
||||
return photoDB.sequelize.query("INSERT INTO photos " +
|
||||
"SET path=:path,filename=:filename,added=DATE(:added)", {
|
||||
replacements: replacements
|
||||
});
|
||||
}
|
||||
}).then(function() {
|
||||
return resolve(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -70,7 +82,7 @@ function scanDir(path) {
|
||||
console.log("Scanning completed in " + Math.round(((endStamp - startStamp))) + "ms.");
|
||||
}
|
||||
}).then(function() {
|
||||
return resolve(meta);
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -78,4 +90,9 @@ function scanDir(path) {
|
||||
|
||||
const startStamp = Date.now();
|
||||
|
||||
module.exports = scanDir(picturesPath);
|
||||
module.exports = {
|
||||
scan: function (db) {
|
||||
photoDB = db;
|
||||
return scanDir(picturesPath);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user