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>
|
<div>[[path]]</div>
|
||||||
</app-header>
|
</app-header>
|
||||||
<div class="thumbnails layout horizontal wrap">
|
<div class="thumbnails layout horizontal wrap">
|
||||||
<template is="dom-repeat" items="[[photos.files]]">
|
<template is="dom-repeat" items="[[photos]]">
|
||||||
<div style$="background-image:url([[item.filepath]])" on-tap="_imageTap" info="[[item]]"></div>
|
<div style$="background-image:url([[item.path]]/[[item.filename]])" on-tap="_imageTap" info="[[item]]"></div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
<div class="folders layout horizontal wrap">
|
<div class="folders layout horizontal wrap">
|
||||||
@ -148,23 +148,21 @@
|
|||||||
|
|
||||||
var base = document.querySelector("base");
|
var base = document.querySelector("base");
|
||||||
if (base) {
|
if (base) {
|
||||||
base = new URL(base.href).pathname;
|
this.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 = "";
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.base = "";
|
this.base = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
photos.forEach(function(photo) {
|
||||||
|
photo.path = this.base + photo.path;
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
function findPath(path, item) {
|
function findPath(path, item) {
|
||||||
if (path.indexOf(item.filepath) != 0) {
|
if (path.indexOf(item.path) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path == item.filepath || path == item.filepath + "/") {
|
if (path == item.path || path == item.path + "/") {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ const express = require("express"),
|
|||||||
morgan = require("morgan"),
|
morgan = require("morgan"),
|
||||||
bodyParser = require("body-parser"),
|
bodyParser = require("body-parser"),
|
||||||
config = require("config"),
|
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 */
|
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 */
|
/* Handle static files first so excessive logging doesn't occur */
|
||||||
app.use(basePath, express.static("frontend", { index: false }));
|
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"));
|
app.use(morgan("common"));
|
||||||
|
|
||||||
@ -75,9 +75,9 @@ app.set("port", serverConfig.port);
|
|||||||
|
|
||||||
const server = require("http").createServer(app);
|
const server = require("http").createServer(app);
|
||||||
|
|
||||||
Promise.all([
|
db.then(function(photoDB) {
|
||||||
db, scanner.then(function(files) { app.set("files", files); })
|
return scanner.scan(photoDB);
|
||||||
]).then(function() {
|
}).then(function() {
|
||||||
console.log("Done scanning. Opening server.");
|
console.log("Done scanning. Opening server.");
|
||||||
server.listen(serverConfig.port);
|
server.listen(serverConfig.port);
|
||||||
}).catch(function(error) {
|
}).catch(function(error) {
|
||||||
|
@ -20,37 +20,25 @@ const fs = require('fs'),
|
|||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
const db = {
|
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.");
|
console.log("DB initialization beginning. DB access will block.");
|
||||||
let models = [];
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
});
|
|
||||||
|
|
||||||
/* 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 () {
|
return db.sequelize.authenticate().then(function () {
|
||||||
|
const Photo = db.sequelize.define('photo', {
|
||||||
|
path: Sequelize.STRING,
|
||||||
|
filename: Sequelize.STRING,
|
||||||
|
added: Sequelize.DATE
|
||||||
|
});
|
||||||
|
|
||||||
console.log("Connection established successfully with DB.");
|
console.log("Connection established successfully with DB.");
|
||||||
return db.sequelize.sync({
|
return db.sequelize.sync({
|
||||||
force: false
|
force: false
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
console.log("DB relationships successfully mapped. DB access unblocked.");
|
console.log("DB relationships successfully mapped. DB access unblocked.");
|
||||||
return resolve(db);
|
return db;
|
||||||
});
|
});
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
console.log("ERROR: Failed to authenticate with DB");
|
console.log("ERROR: Failed to authenticate with DB");
|
||||||
@ -58,7 +46,6 @@ function init() {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
return reject(error);
|
return reject(error);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = init();
|
module.exports = init();
|
||||||
|
@ -5,10 +5,33 @@ const express = require("express"),
|
|||||||
url = require("url"),
|
url = require("url"),
|
||||||
config = require("config");
|
config = require("config");
|
||||||
|
|
||||||
|
let photoDB;
|
||||||
|
|
||||||
|
require("../db").then(function(db) {
|
||||||
|
photoDB = db;
|
||||||
|
});
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
/* Each photos has:
|
||||||
|
|
||||||
|
* locations
|
||||||
|
* people
|
||||||
|
* date
|
||||||
|
* tags
|
||||||
|
* photo info
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
router.get("/", function(req, res/*, next*/) {
|
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;
|
module.exports = router;
|
||||||
|
@ -1,27 +1,24 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const db = require("sequelize"),
|
const Promise = require("bluebird"),
|
||||||
Promise = require("bluebird"),
|
|
||||||
fs = require("fs"),
|
fs = require("fs"),
|
||||||
config = require("config");
|
config = require("config");
|
||||||
|
|
||||||
let scanning = 0;
|
let scanning = 0;
|
||||||
|
|
||||||
|
let photoDB = null;
|
||||||
|
|
||||||
const picturesPath = config.get("picturesPath");
|
const picturesPath = config.get("picturesPath");
|
||||||
|
|
||||||
function scanFile(path, stats) {
|
function scanFile(path, file, stats) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
const meta = { filepath: path };
|
console.log("Scanning file: " + path + "/" + file);
|
||||||
console.log("Scanning file: " + path);
|
return resolve(true);
|
||||||
meta.created = stats.ctime;
|
|
||||||
return resolve(meta);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function scanDir(path) {
|
function scanDir(path) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
const meta = { filepath: path, files: [], paths: [] };
|
|
||||||
|
|
||||||
console.log("Scanning path " + path);
|
console.log("Scanning path " + path);
|
||||||
|
|
||||||
fs.readdir(path, function(err, files) {
|
fs.readdir(path, function(err, files) {
|
||||||
@ -35,29 +32,44 @@ function scanDir(path) {
|
|||||||
return Promise.map(files, function(file) {
|
return Promise.map(files, function(file) {
|
||||||
let filepath = path + "/" + file;
|
let filepath = path + "/" + file;
|
||||||
|
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
fs.stat(filepath, function(err, stats) {
|
fs.stat(filepath, function(err, stats) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.warn("Could not stat " + filepath);
|
console.warn("Could not stat " + filepath);
|
||||||
return resolve(null);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stats.isDirectory()) {
|
if (stats.isDirectory()) {
|
||||||
return scanDir(filepath, stats).then(function(entry) {
|
return scanDir(filepath, stats).then(function(entry) {
|
||||||
if (entry && (entry.files.length || entry.paths.length)) {
|
return resolve(true);
|
||||||
meta.paths.push(entry);
|
|
||||||
}
|
|
||||||
return resolve(entry);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stats.isFile() */
|
/* stats.isFile() */
|
||||||
return scanFile(filepath, stats).then(function(entry) {
|
return scanFile(path, file, stats).then(function(entry) {
|
||||||
if (entry) {
|
if (!entry) {
|
||||||
meta.files.push(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.");
|
console.log("Scanning completed in " + Math.round(((endStamp - startStamp))) + "ms.");
|
||||||
}
|
}
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
return resolve(meta);
|
return resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -78,4 +90,9 @@ function scanDir(path) {
|
|||||||
|
|
||||||
const startStamp = Date.now();
|
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