From d0ffda22b64a6585d2167fdaa07b8982dd530fc0 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 8 Oct 2018 10:56:14 -0700 Subject: [PATCH] Added placeholder for utils/check --- server/app.js | 25 +++++++++----- server/routes/users.js | 12 +++---- util/check.js | 78 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 util/check.js diff --git a/server/app.js b/server/app.js index 997f025..1c91650 100755 --- a/server/app.js +++ b/server/app.js @@ -22,10 +22,6 @@ require("./console-line.js"); /* Monkey-patch console.log with line numbers */ const picturesPath = config.get("picturesPath").replace(/\/$/, ""), serverConfig = config.get("server"); -config.get("admin.mail"); -config.get("smtp.host"); -config.get("smtp.sender"); - let basePath = config.get("basePath"); let photoDB = null, userDB = null; @@ -102,11 +98,17 @@ app.use(session({ const index = require("./routes/index"); -const transporter = require("nodemailer").createTransport({ - host: config.get("smtp.host"), - pool: true, - port: config.has("smtp.port") ? config.get("smtp.port") : 25 -}); +if (config.has("admin.mail") && + config.has("smtp.host") && + config.has("smtp.sender")) { + app.set("transporter", require("nodemailer").createTransport({ + host: config.get("smtp.host"), + pool: true, + port: config.has("smtp.port") ? config.get("smtp.port") : 25 + })); +} else { + console.log("SMTP disabled. To enable SMTP, configure admin.mail, smtp.host, and smtp.sender"); +} const templates = { "html": [ @@ -176,6 +178,11 @@ app.use(basePath, function(req, res, next) { throw "DB mis-match between authentications and users table"; } + if (!app.get("transporter")) { + console.log("Not sending VERIFIED email; SMTP not configured."); + return; + } + let user = results[0], envelope = { to: config.get("admin.mail"), diff --git a/server/routes/users.js b/server/routes/users.js index 5257699..d7e17e6 100755 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -11,12 +11,6 @@ const router = express.Router(); let userDB; -let transporter = createTransport({ - host: config.get("smtp.host"), - pool: true, - port: config.has("smtp.port") ? config.get("smtp.port") : 25 -}); - let ldap; if (config.has("ldap.url")) { ldap = new LdapAuth(config.get("ldap")); @@ -145,6 +139,12 @@ router.post("/create", function(req, res) { throw error; }); }).then(function() { + const transporter = app.get("transporter"); + if (!transporter) { + console.log("Not sending VERIFY email; SMTP not configured."); + return; + } + let data = { username: name, mail: mail, diff --git a/util/check.js b/util/check.js new file mode 100644 index 0000000..f204eae --- /dev/null +++ b/util/check.js @@ -0,0 +1,78 @@ +"use strict"; + +const fs = require("fs"), + Promise = require("bluebird"), + config = require("config"); + +if (process.argv.length <= 2) { + console.log("usage: node check file|id [file|id] ... "); + process.exit(-1); +} +const items = process.argv.splice(2); + +const picturesPath = config.get("picturesPath").replace(/\/$/, ""); + +for (let i = 0; i < items.length; i++) { + if (parseInt(items[i]) == items[i]) { + items[i] = { + type: "id", + value: items[i] + }; + } else { + items[i] = { + type: "file", + value: items[i] + } + } +} + +const exists = function(path) { + return stat(path).then(function() { + return true; + }).catch(function() { + return false; + }); +} + +const stat = function (_path) { + if (_path.indexOf(picturesPath.replace(/\/$/, "")) == 0) { + _path = _path.substring(picturesPath.length); + } + + let path = picturesPath + _path; + + return new Promise(function (resolve, reject) { + fs.stat(path, function (error, stats) { + if (error) { + return reject(error); + } + return resolve(stats); + }); + }); +} + +require("../server/db/photos").then(function(db) { + const photoDB = db; + return Promise.mapSeries(items, function(item) { + switch (item.type) { + case "file": + return exists(item.value).then(function(exists) { + console.log("File '" + item.value + "' " + (exists ? "exists" : "does not exist.")); + }); + case "id": + return photoDB.sequelize.query("SELECT * FROM photos WHERE id=:id", { + replacements: { + id: item.value + }, + type: photoDB.Sequelize.QueryTypes.SELECT, + raw: true + }).then(function(photos) { + if (photos.length == 0) { + console.log("Item " + item.value + " does not exist."); + } else { + console.log("Item " + item.value + " exists:", photos[0]); + } + }); + } + }); +});