add comment cleanup and add globals

This commit is contained in:
Alf Hammerseth
2019-11-16 21:13:45 +01:00
parent 54b745ec68
commit 33e6220e43
31 changed files with 4480 additions and 2552 deletions

View File

@@ -1,8 +1,11 @@
var express = require("express");
var app = express();
const path = require("path");
const publicPath = path.join(__dirname + "", "../public");
import {
publicPath,
pathThumbnails
} from "../settings/globals";
var exphbs = require("express-handlebars");
var hbs = exphbs.create({
defaultLayout: publicPath + "/layouts/admin/main",
@@ -11,12 +14,10 @@ var hbs = exphbs.create({
});
var passport = require("passport");
var mpromise = require("mpromise");
var LocalStrategy = require("passport-local").Strategy;
var mongoose = require("mongoose");
var mongo_db_cred = require(pathThumbnails + "/config/mongo_config.js");
var mongojs = require("mongojs");
var db = mongojs(mongo_db_cred.config);
var token_db = mongojs("tokens");
var bodyParser = require("body-parser");
var session = require("express-session");
@@ -30,7 +31,9 @@ mongoose.connect(url);
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
app.use(compression({ filter: shouldCompress }));
app.use(compression({
filter: shouldCompress
}));
function shouldCompress(req, res) {
if (req.headers["x-no-compression"]) {
@@ -44,7 +47,6 @@ function shouldCompress(req, res) {
app.set("trust proxy", "127.0.0.1");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var referrerPolicy = require("referrer-policy");
var helmet = require("helmet");
var featurePolicy = require("feature-policy");
@@ -52,13 +54,11 @@ app.use(
featurePolicy({
features: {
fullscreen: ["*"],
//vibrate: ["'none'"],
payment: ["'none'"],
microphone: ["'none'"],
camera: ["'none'"],
speaker: ["*"],
syncXhr: ["'self'"]
//notifications: ["'self'"]
}
})
);
@@ -67,7 +67,9 @@ app.use(
frameguard: false
})
);
app.use(referrerPolicy({ policy: "origin-when-cross-origin" }));
app.use(referrerPolicy({
policy: "origin-when-cross-origin"
}));
app.enable("view cache");
app.set("views", publicPath);
app.use(bodyParser.json()); // to support JSON-encoded bodies
@@ -92,43 +94,47 @@ app.use(
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
//app.use('/assets', express.static(publicPath + '/assets'));
passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(
"local-signup",
new LocalStrategy(
{
new LocalStrategy({
// by default, local strategy uses username and password, we will override with username
usernameField: "username",
passwordField: "password",
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) {
function (req, username, password, done) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
process.nextTick(function () {
// find a user whose username is the same as the forms username
// we are checking to see if the user trying to login already exists
var token = req.body.token;
token_db
.collection("tokens")
.find({ token: token }, function(err, docs) {
.find({
token: token
}, function (err, docs) {
if (docs.length == 1) {
token_db
.collection("tokens")
.remove({ token: token }, function(err, docs) {
User.findOne({ username: username }, function(err, user) {
.remove({
token: token
}, function (err, docs) {
User.findOne({
username: username
}, function (err, user) {
// if there are any errors, return the error
if (err) return done(err);
@@ -145,7 +151,7 @@ passport.use(
newUser.password = newUser.generateHash(password);
// save the user
newUser.save(function(err) {
newUser.save(function (err) {
if (err) throw err;
return done(null, newUser);
});
@@ -163,19 +169,20 @@ passport.use(
passport.use(
"local-login",
new LocalStrategy(
{
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: "username",
passwordField: "password",
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) {
function (req, username, password, done) {
// callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ username: username }, function(err, user) {
User.findOne({
username: username
}, function (err, user) {
// if there are any errors, return the error before anything else
if (err) return done(err);
@@ -211,7 +218,7 @@ app.post(
})
);
app.use("/login", isLoggedInTryingToLogIn, function(req, res) {
app.use("/login", isLoggedInTryingToLogIn, function (req, res) {
var data = {
where_get: "not_authenticated"
};
@@ -219,7 +226,7 @@ app.use("/login", isLoggedInTryingToLogIn, function(req, res) {
res.render("layouts/admin/not_authenticated", data);
});
app.use("/signup", isLoggedInTryingToLogIn, function(req, res) {
app.use("/signup", isLoggedInTryingToLogIn, function (req, res) {
var data = {
where_get: "not_authenticated"
};
@@ -229,12 +236,12 @@ app.use("/signup", isLoggedInTryingToLogIn, function(req, res) {
app.use("/", api);
app.use("/logout", function(req, res) {
app.use("/logout", function (req, res) {
req.logout();
res.redirect("/login");
});
app.use("/assets/admin/authenticated", function(req, res, next) {
app.use("/assets/admin/authenticated", function (req, res, next) {
if (!req.isAuthenticated()) {
res.sendStatus(403);
return;
@@ -244,7 +251,7 @@ app.use("/assets/admin/authenticated", function(req, res, next) {
app.use("/assets", express.static(publicPath + "/assets"));
app.use("/", isLoggedIn, function(req, res) {
app.use("/", isLoggedIn, function (req, res) {
var data = {
where_get: "authenticated",
year: new Date().getYear() + 1900
@@ -265,6 +272,4 @@ function isLoggedIn(req, res, next) {
res.redirect("/login");
}
//app.listen(default_port);
module.exports = app;
module.exports = app;

View File

@@ -1,4 +1,8 @@
VERSION = require(pathThumbnails + "/VERSION.js");
import {
publicPath,
pathThumbnails,
} from "../settings/globals";
var secure = false;
var path = require("path");
try {
@@ -7,18 +11,9 @@ try {
"cert_config.js"
));
var fs = require("fs");
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString();
var ca = fs.readFileSync(cert_config.ca).toString();
var credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
secure = true;
} catch (err) {}
var add = "";
var express = require("express");
var app = express();
var compression = require("compression");
@@ -31,21 +26,23 @@ var hbs = exphbs.create({
layoutsDir: publicPath + "/layouts/client",
partialsDir: publicPath + "/partials",
helpers: {
if_equal: function(a, b, opts) {
if_equal: function (a, b, opts) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
},
decodeString: function(s) {
decodeString: function (s) {
if (s == undefined) return s;
return Functions.decodeChannelName(s);
}
}
});
var uniqid = require("uniqid");
app.use(compression({ filter: shouldCompress }));
app.use(compression({
filter: shouldCompress
}));
function shouldCompress(req, res) {
if (req.headers["x-no-compression"]) {
@@ -68,17 +65,16 @@ var cookieParser = require("cookie-parser");
var referrerPolicy = require("referrer-policy");
var helmet = require("helmet");
var featurePolicy = require("feature-policy");
app.use(
featurePolicy({
features: {
fullscreen: ["*"],
//vibrate: ["'none'"],
payment: ["'none'"],
microphone: ["'none'"],
camera: ["'none'"],
speaker: ["*"],
syncXhr: ["'self'"]
//notifications: ["'self'"]
}
})
);
@@ -87,7 +83,9 @@ app.use(
frameguard: false
})
);
app.use(referrerPolicy({ policy: "origin-when-cross-origin" }));
app.use(referrerPolicy({
policy: "origin-when-cross-origin"
}));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
@@ -96,12 +94,9 @@ app.use(
})
);
app.use(cookieParser());
//app.set('json spaces', 2);
io = require("socket.io")({
pingTimeout: 25000
//path: '/zoff',
//"origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*, http://localhost:8080*")});
});
var socketIO = require(pathThumbnails + "/handlers/io.js");
@@ -116,12 +111,12 @@ var api = api_file.router;
api_file.sIO = app.socketIO;
var ico_router = require(pathThumbnails + "/routing/client/icons_routing.js");
app.get("/robots.txt", function(req, res) {
app.get("/robots.txt", function (req, res) {
res.type("text/plain");
res.send("User-agent: *\nAllow: /$\nDisallow: /");
});
app.use(function(req, res, next) {
app.use(function (req, res, next) {
var cookie = req.cookies._uI;
var skipElements = [
"/_embed",
@@ -145,12 +140,6 @@ app.use(function(req, res, next) {
next();
} else {
if (cookie === undefined) {
try {
//console.error((new Date), "originalUrl", req.originalUrl);
//console.error((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req.get('origin'), "couldn't fetch cookie for some reason, maybe no cookie exists?");
} catch (e) {
//console.error((new Date), "couldn't fetch origin");
}
var user_name = Functions.hash_pass(
Functions.rndName(uniqid.time(), 15)
);
@@ -158,15 +147,12 @@ app.use(function(req, res, next) {
maxAge: 365 * 10000 * 3600000,
httpOnly: true,
secure: secure
//sameSite: true,
});
} else {
//process.stderr.write((new Date), "couldn't fetch cookie for some reason, maybe no cookie exists?", req, "couldn't fetch cookie for some reason, maybe no cookie exists?");
res.cookie("_uI", cookie, {
maxAge: 365 * 10000 * 3600000,
httpOnly: true,
secure: secure
//sameSite: true,
});
}
res.header("Access-Control-Allow-Origin", "*");
@@ -179,7 +165,7 @@ app.use(function(req, res, next) {
}
});
app.use("/service-worker.js", function(req, res) {
app.use("/service-worker.js", function (req, res) {
res.sendFile(publicPath + "/service-worker.js");
});
@@ -187,21 +173,21 @@ app.use("/", ico_router);
app.use("/", api);
app.use("/", cors(), router);
app.use("/assets/js", function(req, res, next) {
app.use("/assets/js", function (req, res, next) {
res.sendStatus(403);
return;
});
app.use("/assets/admin", function(req, res, next) {
app.use("/assets/admin", function (req, res, next) {
res.sendStatus(403);
return;
});
app.use("/assets", express.static(publicPath + "/assets"));
app.use(function(req, res, next) {
app.use(function (req, res, next) {
res.status(404);
res.redirect("/404");
});
module.exports = app;
module.exports = app;

View File

@@ -1,74 +1,52 @@
var path = require('path');
var publicPath = path.join(__dirname, 'public');
var pathThumbnail = __dirname;
import {
pathThumbnail
} from "../settings/globals";
pathThumbnails = __dirname + "/../";
var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/;
try {
var keys = require(path.join(__dirname, '../config/api_key.js'));
var key = keys.youtube;
var soundcloudKey = keys.soundcloud;
} catch(e) {
console.log("Error - missing file");
console.log("Seems you forgot to create the file api_key.js in /server/config/. Have a look at api_key.example.js.");
process.exit(1);
}
var Search = require(pathThumbnail + '/../handlers/search.js');
var request = require('request');
var db = require(pathThumbnail + '/../handlers/db.js');
var currentList = 0;
var listNames = [];
db.getCollectionNames(function(e, d) {
for(var i = 0; i < d.length; i++) {
if(d[i].indexOf("_") < 0) {
if(d[i].length > 0) {
if(d[i].substring(0, 1) == "." || d[i].substring(d[i].length - 1) == ".") continue;
db.getCollectionNames(function (e, d) {
for (var i = 0; i < d.length; i++) {
if (d[i].indexOf("_") < 0) {
if (d[i].length > 0) {
if (d[i].substring(0, 1) == "." || d[i].substring(d[i].length - 1) == ".") continue;
}
listNames.push(d[i]);
}
}
console.log("Number of lists is " + listNames.length);
/*for(var i = 0; i < listNames.length; i++) {
getListItems(d[i]);
if(i > 1000) return;
}*/
recursivifyListLooping(listNames, 0);
});
function filterFunction(el) {
return el != null &&
el != "" &&
el != undefined &&
el.trim() != ''
}
function recursivifyListLooping(listNames, i) {
if(i > listNames.length) {
if (i > listNames.length) {
console.log("Done");
return;
}
console.log("List " + i + " of " + listNames.length);
getListItems(listNames, 0, function() {
getListItems(listNames, 0, function () {
console.log("done");
});
}
function getListItems(arr, i, callback) {
console.log("List " + i + " of " + listNames.length + " - " + arr[i]);
if(i >= arr.length) {
if(typeof(callback) == "function") callback();
if (i >= arr.length) {
if (typeof (callback) == "function") callback();
return;
}
try {
db.collection(arr[i]).find(function(e, d) {
if(d.length > 0) {
Search.get_genres_list_recursive(d, arr[i], function(){
db.collection(arr[i]).find(function (e, d) {
if (d.length > 0) {
Search.get_genres_list_recursive(d, arr[i], function () {
getListItems(arr, i + 1, callback);
});
} else {
getListItems(arr, i + 1, callback);
}
});
} catch(e) {
} catch (e) {
getListItems(arr, i + 1, callback);
}
}
}