From 5095d8355872935638efd81e91bda60868f0bddf Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 09:30:51 +0200 Subject: [PATCH 01/18] vhost start --- main.js | 8 ++++++++ server.js | 26 +++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 main.js diff --git a/main.js b/main.js new file mode 100644 index 0000000..20430b5 --- /dev/null +++ b/main.js @@ -0,0 +1,8 @@ +const express = require('express') +const vhost = require('vhost') + +express() + .use(vhost('qmining.frylabs.net', require('./server.js').app)) + .use(vhost('frylabs.net', require('./server.js').app)) +// .use(vhost('sync.mysite.com', require('/path/to/sync').app)) + .listen(8080) diff --git a/server.js b/server.js index 536957b..071fa78 100644 --- a/server.js +++ b/server.js @@ -27,8 +27,8 @@ const bodyParser = require('body-parser') const busboy = require('connect-busboy') const fs = require('fs') const app = express() -const http = require('http') -const https = require('https') +// const http = require('http') +// const https = require('https') const logger = require('./logger.js') const utils = require('./utils.js') @@ -316,16 +316,20 @@ app.post('*', function (req, res) { var msg = '' stat.Load() -const httpServer = http.createServer(app) -httpServer.listen(port) +// const httpServer = http.createServer(app) +// httpServer.listen(port) + +exports.app = app + msg += 'Server listening on port ' + port + ' (http)' -if (startHTTPS && certsLoaded) { - const httpsServer = https.createServer(certs, app) - httpsServer.listen(httpsPort) - msg += ', and ' + httpsPort + ' (https)...' -} else { - logger.Log('Cert files does not exists, starting http only!', logger.GetColor('redbg')) -} +// if (startHTTPS && certsLoaded) { +// const httpsServer = https.createServer(certs, app) +// httpsServer.listen(httpsPort) +// msg += ', and ' + httpsPort + ' (https)...' +// } else { +// logger.Log('Cert files does not exists, starting http only!', logger.GetColor('redbg')) +// } + logger.Log(msg, logger.GetColor('yellow')) logger.Log('Node version: ' + process.version) From 7a40666a8f0c9ae06855402e8dc87aaf3098a868 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 09:57:51 +0200 Subject: [PATCH 02/18] qmining and main module on subdomains --- main.js | 175 +++++++++++++++++++++++++-- package.json | 11 ++ qmining.js | 265 ++++++++++++++++++++++++++++++++++++++++ server.js | 336 ++------------------------------------------------- 4 files changed, 454 insertions(+), 333 deletions(-) create mode 100644 package.json create mode 100644 qmining.js diff --git a/main.js b/main.js index 20430b5..c4ec696 100644 --- a/main.js +++ b/main.js @@ -1,8 +1,169 @@ -const express = require('express') -const vhost = require('vhost') +/* ---------------------------------------------------------------------------- -express() - .use(vhost('qmining.frylabs.net', require('./server.js').app)) - .use(vhost('frylabs.net', require('./server.js').app)) -// .use(vhost('sync.mysite.com', require('/path/to/sync').app)) - .listen(8080) + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +const startHTTPS = true +const siteUrl = 'https://qmining.tk' // http(s)//asd.basd +const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' + +const express = require('express') +const bodyParser = require('body-parser') +const busboy = require('connect-busboy') +const fs = require('fs') +const app = express() +// const http = require('http') +// const https = require('https') + +const logger = require('./logger.js') +const utils = require('./utils.js') +const actions = require('./actions.js') +const stat = require('./stat.js') + +const listedFiles = './public/files' +const recivedFiles = 'public/recivedfiles' +const uloadFiles = 'public/f' +const staticFile = 'public/data/static' +const dataFile = 'public/data.json' +const msgFile = 'stats/msgs' + +// https://certbot.eff.org/ +const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' +const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' +const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' + +var certsLoaded = false +if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( + chainFile)) { + try { + const key = fs.readFileSync(privkeyFile, 'utf8') + const cert = fs.readFileSync(fullchainFile, 'utf8') + const ca = fs.readFileSync(chainFile, 'utf8') + var certs = { + key: key, + cert: cert, + ca: ca + } + certsLoaded = true + } catch (e) { + logger.Log('Error loading cert files!', logger.GetColor('redbg')) + } +} + +app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) +app.use(express.static('public')) +app.use(busboy({ + limits: { + fileSize: 10000 * 1024 * 1024 + } +})) +app.use(bodyParser.json()) +app.use(bodyParser.urlencoded({ + limit: '5mb', + extended: true +})) +app.use(bodyParser.json({ + limit: '5mb' +})) + +// -------------------------------------------------------------- + +app.get('/', function (req, res) { + // req.hostname + + //res.render() + res.end('henlo') +}) + +// ----------------------------------------------------------------------------------------------- + +app.get('/stuff*', function (req, res) { + let curr = listedFiles + '/' + req.url.substring('/stuff/'.length, req.url.length).split('?')[0] + let relPath = curr.substring('./public/files'.length, curr.length) + + if (relPath[relPath.length - 1] !== '/') { relPath += '/' } + + let t = relPath.split('/') + let prevDir = '' + for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } + + // curr = curr.replace(/\//g, "/"); + // relPath = relPath.replace(/\//g, "/"); + + logger.LogReq(req) + + if (fs.lstatSync(curr).isDirectory()) { + if (curr[curr.length - 1] !== '/') { curr += '/' } + + let f = [] + + fs.readdirSync(curr).forEach((item) => { + if (item[0] !== '.') { + let res = { name: item } + let stats = fs.statSync(curr + '/' + item) + + let fileSizeInBytes = stats['size'] + res.size = Math.round(fileSizeInBytes / 1000000) + + res.path = relPath + if (res.path[res.path.length - 1] !== '/') { res.path += '/' } + res.path += item + + res.mtime = stats['mtime'].toLocaleString() + + f.push(res) + } + }) + + res.render('folders', { + folders: f, + dirname: relPath, + prevDir + }) + } else { + let fileStream = fs.createReadStream(curr) + fileStream.pipe(res) + } +}) + +// ----------------------------------------------------------------------------------------------- + +app.get('*', function (req, res) { + res.render('404') + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); +}) + +app.post('*', function (req, res) { + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); +}) + +var msg = '' +stat.Load() + +exports.app = app + +logger.Log('Main module started', logger.GetColor('yellow')) diff --git a/package.json b/package.json new file mode 100644 index 0000000..3471a4b --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "node-ejs", + "main": "server.js", + "dependencies": { + "connect-busboy": "0.0.2", + "ejs": "^1.0.0", + "express": "^4.6.1", + "express-ejs-layouts": "^1.1.0", + "vhost": "^3.0.2" + } +} diff --git a/qmining.js b/qmining.js new file mode 100644 index 0000000..a2bbf7c --- /dev/null +++ b/qmining.js @@ -0,0 +1,265 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +const startHTTPS = true +const siteUrl = 'https://qmining.tk' // http(s)//asd.basd +const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' + +const express = require('express') +const bodyParser = require('body-parser') +const busboy = require('connect-busboy') +const fs = require('fs') +const app = express() +// const http = require('http') +// const https = require('https') + +const logger = require('./logger.js') +const utils = require('./utils.js') +const actions = require('./actions.js') +const stat = require('./stat.js') + +const listedFiles = './public/files' +const recivedFiles = 'public/recivedfiles' +const uloadFiles = 'public/f' +const staticFile = 'public/data/static' +const dataFile = 'public/data.json' +const msgFile = 'stats/msgs' + +// https://certbot.eff.org/ +const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' +const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' +const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' + +var certsLoaded = false +if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( + chainFile)) { + try { + const key = fs.readFileSync(privkeyFile, 'utf8') + const cert = fs.readFileSync(fullchainFile, 'utf8') + const ca = fs.readFileSync(chainFile, 'utf8') + var certs = { + key: key, + cert: cert, + ca: ca + } + certsLoaded = true + } catch (e) { + logger.Log('Error loading cert files!', logger.GetColor('redbg')) + } +} + +app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) +app.use(express.static('public')) +app.use(busboy({ + limits: { + fileSize: 10000 * 1024 * 1024 + } +})) +app.use(bodyParser.json()) +app.use(bodyParser.urlencoded({ + limit: '5mb', + extended: true +})) +app.use(bodyParser.json({ + limit: '5mb' +})) + +// -------------------------------------------------------------- + +app.get('/', function (req, res) { + // req.hostname + + res.render('main', { + siteurl: siteUrl, + qa: actions.ProcessQA() + }) + res.end() +}) + +app.get('/sio', function (req, res) { + res.render('uload') + res.end() +}) + +app.get('/manual', function (req, res) { + res.render('man') + res.end() + logger.LogReq(req) +}) + +app.get('/public', function (req, res) { + var response = '@Plz update :)' + response += utils.ReadFile(staticFile) + res.write(response) + res.end() +}) + +app.get('/static', function (req, res) { + var response = '@Plz update :)' + response += utils.ReadFile(staticFile) + res.write(response) + res.end() +}) + +app.get('/legacy', function (req, res) { + var f = utils.ReadFile(dataFile) + var d = actions.LoadJSON(f) + let qcount = 0 + for (let i = 0; i < d.length; i++) { qcount += d.Subjects[i].length } + let scount = d.length + + res.render('alldata', { + data: d, + scount: scount, + qcount: qcount, + siteurl: siteUrl + }) + + logger.LogReq(req) +}) + +app.post('/postfeedback', function (req, res) { + res.redirect('back') + logger.Log('New feedback message', logger.GetColor('bluebg'), true) + utils.AppendToFile('\n\n' + logger.GetDateString() + ': ' + req.body.message_field, msgFile) +}) + +app.get('/postfeedback', function (req, res) { + // TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already + // redirects + res.render('main', { + sdata: utils.ReadFile(staticFile) + }) +}) + +app.post('/isAdding', function (req, res) { + res.end('OK') + logger.LogReq(req) + actions.ProcessIncomingRequest(req.body.datatoadd) + utils.WriteBackup() +}) + +app.get('/lred', function (req, res) { + res.redirect('/legacy') + res.end() + logger.LogReq(req) +}) + +app.get('/menuClick', function (req, res) { + res.redirect('/') + res.end() + logger.LogReq(req) +}) + +app.get('/irc', function (req, res) { + res.redirect(ircURL) + res.end() + logger.LogReq(req) +}) + +// all questions readable +app.get('/allqr', function (req, res) { + var f = utils.ReadFile(dataFile) + var d = actions.LoadJSON(f) + + res.render('allqr', { + d: d.toString().split('\n') + }) + logger.LogReq(req) +}) + +app.get('/greasy', function (req, res) { + res.redirect('https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help') + res.end() + logger.LogReq(req) +}) + +app.get('/scriptgit', function (req, res) { + res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript') + res.end() + logger.LogReq(req) +}) + +app.get('/servergit', function (req, res) { + res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server') + res.end() + logger.LogReq(req) +}) + +function UploadFile (req, res, path, next) { + var fstream + req.pipe(req.busboy) + req.busboy.on('file', function (fieldname, file, filename) { + logger.Log('Uploading: ' + filename, logger.GetColor('blue')) + + utils.CreatePath(path, true) + let d = new Date() + let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename + + fstream = fs.createWriteStream(path + '/' + fn) + file.pipe(fstream) + fstream.on('close', function () { + logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) + next(fn) + }) + fstream.on('error', function (err) { + console.log(err) + res.end('something bad happened :s') + }) + }) +} + +app.route('/fosuploader').post(function (req, res, next) { + UploadFile(req, res, uloadFiles, (fn) => { + res.redirect('/f/' + fn) + }) +}) + +app.route('/badtestsender').post(function (req, res, next) { + UploadFile(req, res, recivedFiles, (fn) => { + res.render('uploaded') + }) + logger.LogReq(req) +}) + +app.get('*', function (req, res) { + res.render('404') + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); +}) + +app.post('*', function (req, res) { + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); +}) + +var msg = '' +stat.Load() + +exports.app = app + +logger.Log('Qmining module started', logger.GetColor('yellow')) diff --git a/server.js b/server.js index 071fa78..77a2846 100644 --- a/server.js +++ b/server.js @@ -1,335 +1,19 @@ -/* ---------------------------------------------------------------------------- - - Question Server - GitLab: - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - - ------------------------------------------------------------------------- */ - -const startHTTPS = true -const siteUrl = 'https://qmining.tk' // http(s)//asd.basd -const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' - const express = require('express') -const bodyParser = require('body-parser') -const busboy = require('connect-busboy') -const fs = require('fs') -const app = express() -// const http = require('http') -// const https = require('https') - +const vhost = require('vhost') const logger = require('./logger.js') -const utils = require('./utils.js') -const actions = require('./actions.js') -const stat = require('./stat.js') -const listedFiles = './public/files' -const recivedFiles = 'public/recivedfiles' -const uloadFiles = 'public/f' -const staticFile = 'public/data/static' -const dataFile = 'public/data.json' -const msgFile = 'stats/msgs' - -// https://certbot.eff.org/ -const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' -const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' -const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' - -var certsLoaded = false -if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( - chainFile)) { - try { - const key = fs.readFileSync(privkeyFile, 'utf8') - const cert = fs.readFileSync(fullchainFile, 'utf8') - const ca = fs.readFileSync(chainFile, 'utf8') - var certs = { - key: key, - cert: cert, - ca: ca - } - certsLoaded = true - } catch (e) { - logger.Log('Error loading cert files!', logger.GetColor('redbg')) - } -} +const qmining = require('./qmining.js').app +const main = require('./main.js').app const port = 8080 -const httpsPort = 8443 -app.set('view engine', 'ejs') -app.use(function (req, res, next) { - res.on('finish', function () { - logger.LogReq(req, true, res.statusCode) - if (res.statusCode !== 404) { stat.LogStat(req.url) } - }) - next() -}) -app.use(express.static('public')) -app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } -})) -app.use(bodyParser.json()) -app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true -})) -app.use(bodyParser.json({ - limit: '5mb' -})) +express() + .use(vhost('qmining.frylabs.net', qmining)) + .use(vhost('frylabs.net', main)) + .use(vhost('qmining.tk', qmining)) + .listen(port) -// -------------------------------------------------------------- -app.get('/', function (req, res) { - // req.hostname - - res.render('main', { - siteurl: siteUrl, - qa: actions.ProcessQA() - }) - res.end() -}) - -app.get('/sio', function (req, res) { - res.render('uload') - res.end() -}) - -app.get('/manual', function (req, res) { - res.render('man') - res.end() - logger.LogReq(req) -}) - -app.get('/public', function (req, res) { - var response = '@Plz update :)' - response += utils.ReadFile(staticFile) - res.write(response) - res.end() -}) - -app.get('/static', function (req, res) { - var response = '@Plz update :)' - response += utils.ReadFile(staticFile) - res.write(response) - res.end() -}) - -app.get('/legacy', function (req, res) { - var f = utils.ReadFile(dataFile) - var d = actions.LoadJSON(f) - let qcount = 0 - for (let i = 0; i < d.length; i++) { qcount += d.Subjects[i].length } - let scount = d.length - - res.render('alldata', { - data: d, - scount: scount, - qcount: qcount, - siteurl: siteUrl - }) - - logger.LogReq(req) -}) - -app.post('/postfeedback', function (req, res) { - res.redirect('back') - logger.Log('New feedback message', logger.GetColor('bluebg'), true) - utils.AppendToFile('\n\n' + logger.GetDateString() + ': ' + req.body.message_field, msgFile) -}) - -app.get('/postfeedback', function (req, res) { - // TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already - // redirects - res.render('main', { - sdata: utils.ReadFile(staticFile) - }) -}) - -app.post('/isAdding', function (req, res) { - res.end('OK') - logger.LogReq(req) - actions.ProcessIncomingRequest(req.body.datatoadd) - utils.WriteBackup() -}) - -app.get('/lred', function (req, res) { - res.redirect('/legacy') - res.end() - logger.LogReq(req) -}) - -app.get('/menuClick', function (req, res) { - res.redirect('/') - res.end() - logger.LogReq(req) -}) - -app.get('/irc', function (req, res) { - res.redirect(ircURL) - res.end() - logger.LogReq(req) -}) - -// all questions readable -app.get('/allqr', function (req, res) { - var f = utils.ReadFile(dataFile) - var d = actions.LoadJSON(f) - - res.render('allqr', { - d: d.toString().split('\n') - }) - logger.LogReq(req) -}) - -app.get('/greasy', function (req, res) { - res.redirect('https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help') - res.end() - logger.LogReq(req) -}) - -app.get('/scriptgit', function (req, res) { - res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript') - res.end() - logger.LogReq(req) -}) - -app.get('/servergit', function (req, res) { - res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server') - res.end() - logger.LogReq(req) -}) - -function UploadFile (req, res, path, next) { - var fstream - req.pipe(req.busboy) - req.busboy.on('file', function (fieldname, file, filename) { - logger.Log('Uploading: ' + filename, logger.GetColor('blue')) - - utils.CreatePath(path, true) - let d = new Date() - let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename - - fstream = fs.createWriteStream(path + '/' + fn) - file.pipe(fstream) - fstream.on('close', function () { - logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) - next(fn) - }) - fstream.on('error', function (err) { - console.log(err) - res.end('something bad happened :s') - }) - }) -} - -app.route('/fosuploader').post(function (req, res, next) { - UploadFile(req, res, uloadFiles, (fn) => { - res.redirect('/f/' + fn) - }) -}) - -app.route('/badtestsender').post(function (req, res, next) { - UploadFile(req, res, recivedFiles, (fn) => { - res.render('uploaded') - }) - logger.LogReq(req) -}) - -// ----------------------------------------------------------------------------------------------- - -app.get('/stuff*', function (req, res) { - let curr = listedFiles + '/' + req.url.substring('/stuff/'.length, req.url.length).split('?')[0] - let relPath = curr.substring('./public/files'.length, curr.length) - - if (relPath[relPath.length - 1] !== '/') { relPath += '/' } - - let t = relPath.split('/') - let prevDir = '' - for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } - - // curr = curr.replace(/\//g, "/"); - // relPath = relPath.replace(/\//g, "/"); - - logger.LogReq(req) - - if (fs.lstatSync(curr).isDirectory()) { - if (curr[curr.length - 1] !== '/') { curr += '/' } - - let f = [] - - fs.readdirSync(curr).forEach((item) => { - if (item[0] !== '.') { - let res = { name: item } - let stats = fs.statSync(curr + '/' + item) - - let fileSizeInBytes = stats['size'] - res.size = Math.round(fileSizeInBytes / 1000000) - - res.path = relPath - if (res.path[res.path.length - 1] !== '/') { res.path += '/' } - res.path += item - - res.mtime = stats['mtime'].toLocaleString() - - f.push(res) - } - }) - - res.render('folders', { - folders: f, - dirname: relPath, - prevDir - }) - } else { - let fileStream = fs.createReadStream(curr) - fileStream.pipe(res) - } -}) - -// ----------------------------------------------------------------------------------------------- - -app.get('*', function (req, res) { - res.render('404') - res.status(404) - // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); -}) - -app.post('*', function (req, res) { - res.status(404) - // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); -}) - -var msg = '' -stat.Load() - -// const httpServer = http.createServer(app) -// httpServer.listen(port) - -exports.app = app - -msg += 'Server listening on port ' + port + ' (http)' - -// if (startHTTPS && certsLoaded) { -// const httpsServer = https.createServer(certs, app) -// httpsServer.listen(httpsPort) -// msg += ', and ' + httpsPort + ' (https)...' -// } else { -// logger.Log('Cert files does not exists, starting http only!', logger.GetColor('redbg')) -// } - -logger.Log(msg, logger.GetColor('yellow')) logger.Log('Node version: ' + process.version) +logger.Log('Listening on port: ' + port) + From 0bf83b721b15e5206d884ae318c8ee73727747f2 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:04:26 +0200 Subject: [PATCH 03/18] Logging in main, old code removal --- main.js | 53 +++-------------------------------------------- qmining.js | 45 +--------------------------------------- server.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 96 deletions(-) diff --git a/main.js b/main.js index c4ec696..918a065 100644 --- a/main.js +++ b/main.js @@ -18,61 +18,19 @@ ------------------------------------------------------------------------- */ -const startHTTPS = true -const siteUrl = 'https://qmining.tk' // http(s)//asd.basd -const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' - const express = require('express') const bodyParser = require('body-parser') const busboy = require('connect-busboy') const fs = require('fs') const app = express() -// const http = require('http') -// const https = require('https') const logger = require('./logger.js') -const utils = require('./utils.js') -const actions = require('./actions.js') -const stat = require('./stat.js') +// const utils = require('./utils.js') +// const actions = require('./actions.js') const listedFiles = './public/files' -const recivedFiles = 'public/recivedfiles' -const uloadFiles = 'public/f' -const staticFile = 'public/data/static' -const dataFile = 'public/data.json' -const msgFile = 'stats/msgs' - -// https://certbot.eff.org/ -const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' -const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' -const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' - -var certsLoaded = false -if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( - chainFile)) { - try { - const key = fs.readFileSync(privkeyFile, 'utf8') - const cert = fs.readFileSync(fullchainFile, 'utf8') - const ca = fs.readFileSync(chainFile, 'utf8') - var certs = { - key: key, - cert: cert, - ca: ca - } - certsLoaded = true - } catch (e) { - logger.Log('Error loading cert files!', logger.GetColor('redbg')) - } -} app.set('view engine', 'ejs') -app.use(function (req, res, next) { - res.on('finish', function () { - logger.LogReq(req, true, res.statusCode) - if (res.statusCode !== 404) { stat.LogStat(req.url) } - }) - next() -}) app.use(express.static('public')) app.use(busboy({ limits: { @@ -91,9 +49,7 @@ app.use(bodyParser.json({ // -------------------------------------------------------------- app.get('/', function (req, res) { - // req.hostname - - //res.render() + // res.render() res.end('henlo') }) @@ -161,9 +117,6 @@ app.post('*', function (req, res) { // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); }) -var msg = '' -stat.Load() - exports.app = app logger.Log('Main module started', logger.GetColor('yellow')) diff --git a/qmining.js b/qmining.js index a2bbf7c..c40b19d 100644 --- a/qmining.js +++ b/qmining.js @@ -18,9 +18,7 @@ ------------------------------------------------------------------------- */ -const startHTTPS = true -const siteUrl = 'https://qmining.tk' // http(s)//asd.basd -const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' +const siteUrl = 'https://frylabs.net' // http(s)//asd.basd const express = require('express') const bodyParser = require('body-parser') @@ -33,46 +31,14 @@ const app = express() const logger = require('./logger.js') const utils = require('./utils.js') const actions = require('./actions.js') -const stat = require('./stat.js') -const listedFiles = './public/files' const recivedFiles = 'public/recivedfiles' const uloadFiles = 'public/f' const staticFile = 'public/data/static' const dataFile = 'public/data.json' const msgFile = 'stats/msgs' -// https://certbot.eff.org/ -const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' -const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' -const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' - -var certsLoaded = false -if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( - chainFile)) { - try { - const key = fs.readFileSync(privkeyFile, 'utf8') - const cert = fs.readFileSync(fullchainFile, 'utf8') - const ca = fs.readFileSync(chainFile, 'utf8') - var certs = { - key: key, - cert: cert, - ca: ca - } - certsLoaded = true - } catch (e) { - logger.Log('Error loading cert files!', logger.GetColor('redbg')) - } -} - app.set('view engine', 'ejs') -app.use(function (req, res, next) { - res.on('finish', function () { - logger.LogReq(req, true, res.statusCode) - if (res.statusCode !== 404) { stat.LogStat(req.url) } - }) - next() -}) app.use(express.static('public')) app.use(busboy({ limits: { @@ -175,12 +141,6 @@ app.get('/menuClick', function (req, res) { logger.LogReq(req) }) -app.get('/irc', function (req, res) { - res.redirect(ircURL) - res.end() - logger.LogReq(req) -}) - // all questions readable app.get('/allqr', function (req, res) { var f = utils.ReadFile(dataFile) @@ -257,9 +217,6 @@ app.post('*', function (req, res) { // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); }) -var msg = '' -stat.Load() - exports.app = app logger.Log('Qmining module started', logger.GetColor('yellow')) diff --git a/server.js b/server.js index 77a2846..0ced543 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + const express = require('express') const vhost = require('vhost') const logger = require('./logger.js') @@ -5,15 +25,52 @@ const logger = require('./logger.js') const qmining = require('./qmining.js').app const main = require('./main.js').app +const stat = require('./stat.js') +stat.Load() + const port = 8080 +function AddLogger (app) { + app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() + }) +} + +AddLogger(qmining) +AddLogger(main) + +// // https://certbot.eff.org/ +// const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' +// const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' +// const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem' +// +// var certsLoaded = false +// if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( +// chainFile)) { +// try { +// const key = fs.readFileSync(privkeyFile, 'utf8') +// const cert = fs.readFileSync(fullchainFile, 'utf8') +// const ca = fs.readFileSync(chainFile, 'utf8') +// var certs = { +// key: key, +// cert: cert, +// ca: ca +// } +// certsLoaded = true +// } catch (e) { +// logger.Log('Error loading cert files!', logger.GetColor('redbg')) +// } +// } + express() .use(vhost('qmining.frylabs.net', qmining)) .use(vhost('frylabs.net', main)) .use(vhost('qmining.tk', qmining)) .listen(port) - logger.Log('Node version: ' + process.version) logger.Log('Listening on port: ' + port) - From 3ab3cfea3b48cde46bdffa428d27a0e8ef2a21a4 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:09:34 +0200 Subject: [PATCH 04/18] Removed logging in main --- main.js | 9 +++++++++ qmining.js | 9 +++++++++ server.js | 16 ---------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/main.js b/main.js index 918a065..a2af133 100644 --- a/main.js +++ b/main.js @@ -27,10 +27,19 @@ const app = express() const logger = require('./logger.js') // const utils = require('./utils.js') // const actions = require('./actions.js') +const stat = require('./stat.js') +stat.Load() const listedFiles = './public/files' app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) app.use(express.static('public')) app.use(busboy({ limits: { diff --git a/qmining.js b/qmining.js index c40b19d..142fbb8 100644 --- a/qmining.js +++ b/qmining.js @@ -31,6 +31,8 @@ const app = express() const logger = require('./logger.js') const utils = require('./utils.js') const actions = require('./actions.js') +const stat = require('./stat.js') +stat.Load() const recivedFiles = 'public/recivedfiles' const uloadFiles = 'public/f' @@ -39,6 +41,13 @@ const dataFile = 'public/data.json' const msgFile = 'stats/msgs' app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) app.use(express.static('public')) app.use(busboy({ limits: { diff --git a/server.js b/server.js index 0ced543..ad75b29 100644 --- a/server.js +++ b/server.js @@ -25,24 +25,8 @@ const logger = require('./logger.js') const qmining = require('./qmining.js').app const main = require('./main.js').app -const stat = require('./stat.js') -stat.Load() - const port = 8080 -function AddLogger (app) { - app.use(function (req, res, next) { - res.on('finish', function () { - logger.LogReq(req, true, res.statusCode) - if (res.statusCode !== 404) { stat.LogStat(req.url) } - }) - next() - }) -} - -AddLogger(qmining) -AddLogger(main) - // // https://certbot.eff.org/ // const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem' // const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem' From 17a303c3ca718e90c5f723d2c620f8ad496253e7 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:15:08 +0200 Subject: [PATCH 05/18] Sio module --- qmining.js | 5 --- server.js | 2 + sio.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 sio.js diff --git a/qmining.js b/qmining.js index 142fbb8..136e66d 100644 --- a/qmining.js +++ b/qmining.js @@ -75,11 +75,6 @@ app.get('/', function (req, res) { res.end() }) -app.get('/sio', function (req, res) { - res.render('uload') - res.end() -}) - app.get('/manual', function (req, res) { res.render('man') res.end() diff --git a/server.js b/server.js index ad75b29..69f0f36 100644 --- a/server.js +++ b/server.js @@ -24,6 +24,7 @@ const logger = require('./logger.js') const qmining = require('./qmining.js').app const main = require('./main.js').app +const sio = require('./sio.js').app const port = 8080 @@ -52,6 +53,7 @@ const port = 8080 express() .use(vhost('qmining.frylabs.net', qmining)) + .use(vhost('sio.frylabs.net', sio)) .use(vhost('frylabs.net', main)) .use(vhost('qmining.tk', qmining)) .listen(port) diff --git a/sio.js b/sio.js new file mode 100644 index 0000000..2bbb7de --- /dev/null +++ b/sio.js @@ -0,0 +1,107 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +const express = require('express') +const bodyParser = require('body-parser') +const busboy = require('connect-busboy') +const fs = require('fs') +const app = express() +// const http = require('http') +// const https = require('https') + +const logger = require('./logger.js') +const utils = require('./utils.js') +const stat = require('./stat.js') +stat.Load() + +const uloadFiles = 'public/f' + +app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) +app.use(express.static('public')) +app.use(busboy({ + limits: { + fileSize: 10000 * 1024 * 1024 + } +})) +app.use(bodyParser.json()) +app.use(bodyParser.urlencoded({ + limit: '5mb', + extended: true +})) +app.use(bodyParser.json({ + limit: '5mb' +})) + +// -------------------------------------------------------------- + +app.get('/', function (req, res) { + res.render('uload') + res.end() +}) + +function UploadFile (req, res, path, next) { + var fstream + req.pipe(req.busboy) + req.busboy.on('file', function (fieldname, file, filename) { + logger.Log('Uploading: ' + filename, logger.GetColor('blue')) + + utils.CreatePath(path, true) + let d = new Date() + let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename + + fstream = fs.createWriteStream(path + '/' + fn) + file.pipe(fstream) + fstream.on('close', function () { + logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) + next(fn) + }) + fstream.on('error', function (err) { + console.log(err) + res.end('something bad happened :s') + }) + }) +} + +app.route('/fosuploader').post(function (req, res, next) { + UploadFile(req, res, uloadFiles, (fn) => { + res.redirect('/f/' + fn) + }) +}) +app.get('*', function (req, res) { + res.render('404') + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); +}) + +app.post('*', function (req, res) { + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); +}) + +exports.app = app + +logger.Log('Sio module started', logger.GetColor('yellow')) From ca05bd74d7d2e6c02427609f207f43d63baab9a8 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:15:43 +0200 Subject: [PATCH 06/18] Changed site url --- qmining.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qmining.js b/qmining.js index 142fbb8..e448a47 100644 --- a/qmining.js +++ b/qmining.js @@ -18,7 +18,7 @@ ------------------------------------------------------------------------- */ -const siteUrl = 'https://frylabs.net' // http(s)//asd.basd +const siteUrl = 'https://qmining.frylabs.net' // http(s)//asd.basd const express = require('express') const bodyParser = require('body-parser') From 0d8c1c154a6bb654f76941503aaa3d29e0c5e812 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:27:31 +0200 Subject: [PATCH 07/18] Moving stuff around --- main.js => modules/main.js | 10 +++++----- qmining.js => modules/qmining.js | 8 ++++---- sio.js => modules/sio.js | 8 ++++---- server.js | 8 ++++---- actions.js => utils/actions.js | 0 changedataversion.js => utils/changedataversion.js | 0 logger.js => utils/logger.js | 0 merger.js => utils/merger.js | 0 motd.js => utils/motd.js | 0 stat.js => utils/stat.js | 0 utils.js => utils/utils.js | 0 11 files changed, 17 insertions(+), 17 deletions(-) rename main.js => modules/main.js (94%) rename qmining.js => modules/qmining.js (97%) rename sio.js => modules/sio.js (94%) rename actions.js => utils/actions.js (100%) rename changedataversion.js => utils/changedataversion.js (100%) rename logger.js => utils/logger.js (100%) rename merger.js => utils/merger.js (100%) rename motd.js => utils/motd.js (100%) rename stat.js => utils/stat.js (100%) rename utils.js => utils/utils.js (100%) diff --git a/main.js b/modules/main.js similarity index 94% rename from main.js rename to modules/main.js index a2af133..613c657 100644 --- a/main.js +++ b/modules/main.js @@ -24,13 +24,13 @@ const busboy = require('connect-busboy') const fs = require('fs') const app = express() -const logger = require('./logger.js') -// const utils = require('./utils.js') -// const actions = require('./actions.js') -const stat = require('./stat.js') +const logger = require('../utils/logger.js') +// const utils = require('../utils/utils.js') +// const actions = require('../utils/actions.js') +const stat = require('../utils/stat.js') stat.Load() -const listedFiles = './public/files' +const listedFiles = '../public/files' app.set('view engine', 'ejs') app.use(function (req, res, next) { diff --git a/qmining.js b/modules/qmining.js similarity index 97% rename from qmining.js rename to modules/qmining.js index b83bc3f..8a1e2ad 100644 --- a/qmining.js +++ b/modules/qmining.js @@ -28,10 +28,10 @@ const app = express() // const http = require('http') // const https = require('https') -const logger = require('./logger.js') -const utils = require('./utils.js') -const actions = require('./actions.js') -const stat = require('./stat.js') +const logger = require('../utils/logger.js') +const utils = require('../utils/utils.js') +const actions = require('../utils/actions.js') +const stat = require('../utils/stat.js') stat.Load() const recivedFiles = 'public/recivedfiles' diff --git a/sio.js b/modules/sio.js similarity index 94% rename from sio.js rename to modules/sio.js index 2bbb7de..afc4c1a 100644 --- a/sio.js +++ b/modules/sio.js @@ -26,12 +26,12 @@ const app = express() // const http = require('http') // const https = require('https') -const logger = require('./logger.js') -const utils = require('./utils.js') -const stat = require('./stat.js') +const logger = require('../utils/logger.js') +const utils = require('../utils/utils.js') +const stat = require('../utils/stat.js') stat.Load() -const uloadFiles = 'public/f' +const uloadFiles = '../public/f' app.set('view engine', 'ejs') app.use(function (req, res, next) { diff --git a/server.js b/server.js index 69f0f36..79bf80f 100644 --- a/server.js +++ b/server.js @@ -20,11 +20,11 @@ const express = require('express') const vhost = require('vhost') -const logger = require('./logger.js') +const logger = require('./utils/logger.js') -const qmining = require('./qmining.js').app -const main = require('./main.js').app -const sio = require('./sio.js').app +const qmining = require('./modules/qmining.js').app +const main = require('./modules/main.js').app +const sio = require('./modules/sio.js').app const port = 8080 diff --git a/actions.js b/utils/actions.js similarity index 100% rename from actions.js rename to utils/actions.js diff --git a/changedataversion.js b/utils/changedataversion.js similarity index 100% rename from changedataversion.js rename to utils/changedataversion.js diff --git a/logger.js b/utils/logger.js similarity index 100% rename from logger.js rename to utils/logger.js diff --git a/merger.js b/utils/merger.js similarity index 100% rename from merger.js rename to utils/merger.js diff --git a/motd.js b/utils/motd.js similarity index 100% rename from motd.js rename to utils/motd.js diff --git a/stat.js b/utils/stat.js similarity index 100% rename from stat.js rename to utils/stat.js diff --git a/utils.js b/utils/utils.js similarity index 100% rename from utils.js rename to utils/utils.js From 8a92e5651fe6f3991f4b0d2c7fb476fcd51f7ebf Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:32:41 +0200 Subject: [PATCH 08/18] Actions.js refactoring --- utils/actions.js | 873 +++++++++++++++++++++++------------------------ 1 file changed, 418 insertions(+), 455 deletions(-) diff --git a/utils/actions.js b/utils/actions.js index 74b2f7f..cb9feb8 100644 --- a/utils/actions.js +++ b/utils/actions.js @@ -1,4 +1,4 @@ -/* ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- Question Server GitLab: @@ -18,516 +18,479 @@ Question Server ------------------------------------------------------------------------- */ module.exports = { - ProcessIncomingRequest: ProcessIncomingRequest, - CheckData: CheckData, - NLoad: NLoad, - LoadJSON: LoadJSON, - ProcessQA: ProcessQA -}; + ProcessIncomingRequest: ProcessIncomingRequest, + CheckData: CheckData, + NLoad: NLoad, + LoadJSON: LoadJSON, + ProcessQA: ProcessQA +} -var staticFile = "public/data/static"; -var manFile = "public/man.html"; -var dataFile = "public/data.json"; -const recDataFile = "stats/recdata"; -const versionFile = "public/version"; -const motdFile = "public/motd"; -const qaFile = "public/qa"; +const staticFile = '../public/data/static' +const dataFile = '../public/data.json' +const recDataFile = '../stats/recdata' +const versionFile = '../public/version' +const motdFile = '../public/motd' +const qaFile = '../public/qa' -var logger = require('./logger.js'); -var utils = require('./utils.js'); +var logger = require('../utils/logger.js') +var utils = require('../utils/utils.js') class Question { - constructor(q, a, i) { - this.Q = q; - this.A = a; - this.I = i; - } - toString() { - var r = "?" + this.Q + "\n!" + this.A; - if (this.I) - r += "\n>" + this.I; - return r; - } - HasQuestion() { - return this.Q != undefined; - } - HasAnswer() { - return this.A != undefined; - } - HasImage() { - return this.I != undefined; - } - IsComplete() { - return this.HasQuestion() && this.HasAnswer(); - } - Compare(q2, i) { - if (typeof q2 == 'string') { - var qmatchpercent = Question.CompareString(this.Q, q2); + constructor (q, a, i) { + this.Q = q + this.A = a + this.I = i + } + toString () { + var r = '?' + this.Q + '\n!' + this.A + if (this.I) { r += '\n>' + this.I } + return r + } + HasQuestion () { + return this.Q !== undefined + } + HasAnswer () { + return this.A !== undefined + } + HasImage () { + return this.I !== undefined + } + IsComplete () { + return this.HasQuestion() && this.HasAnswer() + } + Compare (q2, i) { + if (typeof q2 === 'string') { + var qmatchpercent = Question.CompareString(this.Q, q2) - if (i == undefined || i.length == 0) - return qmatchpercent; - else { - if (this.HasImage()) { - const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(" "), i.join(" ")) : - 0; - return (qmatchpercent + imatchpercent) / 2; - } else { - qmatchpercent -= 30; - if (qmatchpercent < 0) - return 0; - else - return qmatchpercent; - } - } - } else { - const qmatchpercent = Question.CompareString(this.Q, q2.Q); - const amatchpercent = Question.CompareString(this.A, q2.A); - if (this.I != undefined) { - const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(" "), q2.I.join( - " ")) : 0; - return (qmatchpercent + amatchpercent + imatchpercent) / 3; - } else { - return (qmatchpercent + amatchpercent) / 2; - } - } - } - static CompareString(s1, s2) { - //if (s1 == undefined || s2 == undefined) - // return 0; - s1 = SimplifyStringForComparison(s1).split(" "); - s2 = SimplifyStringForComparison(s2).split(" "); - var match = 0; - for (var i = 0; i < s1.length; i++) - if (s2.includes(s1[i])) - match++; - var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent - var lengthDifference = Math.abs(s2.length - s1.length); - percent -= lengthDifference * 3; - if (percent < 0) - percent = 0; - return percent; - } + if (i == undefined || i.length == 0) { return qmatchpercent } else { + if (this.HasImage()) { + const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(' '), i.join(' ')) + : 0 + return (qmatchpercent + imatchpercent) / 2 + } else { + qmatchpercent -= 30 + if (qmatchpercent < 0) { return 0 } else { return qmatchpercent } + } + } + } else { + const qmatchpercent = Question.CompareString(this.Q, q2.Q) + const amatchpercent = Question.CompareString(this.A, q2.A) + if (this.I !== undefined) { + const imatchpercent = this.I === undefined ? Question.CompareString(this.I.join(' '), q2.I.join( + ' ')) : 0 + return (qmatchpercent + amatchpercent + imatchpercent) / 3 + } else { + return (qmatchpercent + amatchpercent) / 2 + } + } + } + + // static CompareString (s1, s2) { + // // if (s1 == undefined || s2 == undefined) + // // return 0; + // s1 = SimplifyStringForComparison(s1).split(' ') + // s2 = SimplifyStringForComparison(s2).split(' ') + // var match = 0 + // for (var i = 0; i < s1.length; i++) { + // if (s2.includes(s1[i])) { match++ } + // } + // var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent + // var lengthDifference = Math.abs(s2.length - s1.length) + // percent -= lengthDifference * 3 + // if (percent < 0) { percent = 0 } + // return percent + // } } class Subject { - constructor(n) { - this.Name = n; - this.Questions = []; - } - get length() { - return this.Questions.length; - } - AddQuestion(q) { - this.Questions.push(q); - } - Search(q, img) { - var r = []; - for (var i = 0; i < this.length; i++) { - let percent = this.Questions[i].Compare(q, img); - if (percent > minMatchAmmount) - r.push({ - q: this.Questions[i], - match: percent - }); - } + constructor (n) { + this.Name = n + this.Questions = [] + } + get length () { + return this.Questions.length + } + AddQuestion (q) { + this.Questions.push(q) + } + Search (q, img) { + const minMatchAmmount = 90 + var r = [] + for (let i = 0; i < this.length; i++) { + let percent = this.Questions[i].Compare(q, img) + if (percent > minMatchAmmount) { + r.push({ + q: this.Questions[i], + match: percent + }) + } + } - for (var i = 0; i < r.length; i++) - for (var j = i; j < r.length; j++) - if (r[i].match < r[j].match) { - var tmp = r[i]; - r[i] = r[j]; - r[j] = tmp; - } + for (let i = 0; i < r.length; i++) { + for (var j = i; j < r.length; j++) { + if (r[i].match < r[j].match) { + var tmp = r[i] + r[i] = r[j] + r[j] = tmp + } + } + } - return r; - } - toString() { - var r = []; - for (var i = 0; i < this.Questions.length; i++) - r.push(this.Questions[i].toString()); - return "+" + this.Name + "\n" + r.join("\n"); - } + return r + } + toString () { + var r = [] + for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) } + return '+' + this.Name + '\n' + r.join('\n') + } } class QuestionDB { - constructor() { - this.Subjects = []; - } - get length() { - return this.Subjects.length; - } - get activeIndexes() { - var r = []; - for (var i = 0; i < this.length; i++) { - if (GM_getValue("Is" + i + "Active")) { - r.push(i); - } - } - return r; - } - GetIfActive(ind) { - return GM_getValue("Is" + ind + "Active"); - } - ChangeActive(i, value) { - GM_setValue("Is" + i + "Active", !!value); - } - AddQuestion(subj, q) { - var i = 0; - while (i < this.Subjects.length && this.Subjects[i].Name != subj) - i++; - if (i < this.Subjects.length) - this.Subjects[i].AddQuestion(q); - else { - const n = new Subject(subj); - n.AddQuestion(q); - this.Subjects.push(n); - } - } - Search(q, img) { - var r = []; - for (var i = 0; i < this.length; i++) - if (this.GetIfActive(i)) - r = r.concat(this.Subjects[i].Search(q, img)); + constructor () { + this.Subjects = [] + } + get length () { + return this.Subjects.length + } + // get activeIndexes () { + // var r = [] + // for (var i = 0; i < this.length; i++) { + // if (GM_getValue('Is' + i + 'Active')) { + // r.push(i) + // } + // } + // return r + // } + // GetIfActive (ind) { + // return GM_getValue('Is' + ind + 'Active') + // } + // ChangeActive (i, value) { + // GM_setValue('Is' + i + 'Active', !!value) + // } + AddQuestion (subj, q) { + var i = 0 + while (i < this.Subjects.length && this.Subjects[i].Name !== subj) { i++ } + if (i < this.Subjects.length) { this.Subjects[i].AddQuestion(q) } else { + const n = new Subject(subj) + n.AddQuestion(q) + this.Subjects.push(n) + } + } + Search (q, img) { + var r = [] + for (let i = 0; i < this.length; i++) { + if (this.GetIfActive(i)) { r = r.concat(this.Subjects[i].Search(q, img)) } + } - for (var i = 0; i < r.length; i++) - for (var j = i; j < r.length; j++) - if (r[i].match < r[j].match) { - var tmp = r[i]; - r[i] = r[j]; - r[j] = tmp; - } + for (let i = 0; i < r.length; i++) { + for (var j = i; j < r.length; j++) { + if (r[i].match < r[j].match) { + var tmp = r[i] + r[i] = r[j] + r[j] = tmp + } + } + } - return r; - } - AddSubject(subj) { - var i = 0; - while (i < this.length && subj.Name != this.Subjects[i].Name) - i++; + return r + } + AddSubject (subj) { + var i = 0 + while (i < this.length && subj.Name !== this.Subjects[i].Name) { i++ } - if (i < this.length) { - this.Subjects.concat(subj.Questions); - } else { - this.Subjects.push(subj); - } - } - toString() { - var r = []; - for (var i = 0; i < this.Subjects.length; i++) - r.push(this.Subjects[i].toString()); - return r.join("\n\n"); - } + if (i < this.length) { + this.Subjects.concat(subj.Questions) + } else { + this.Subjects.push(subj) + } + } + toString () { + var r = [] + for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) } + return r.join('\n\n') + } } +function Process (d, file) { + try { + logger.Log('File: ' + file) + if (d.data.split('\n').length > 1) { + var oldFile = utils.ReadFile(file) + var newFile = oldFile + '\n' + if (d.data[0] === '+') { newFile += d.data } else { newFile += '+' + d.data } -function Process(d, file) { - try { - logger.Log("File: " + file); - if (d.data.split("\n").length > 1) { - var oldFile = utils.ReadFile(file); - var newFile = oldFile + "\n"; - if (d.data[0] == '+') - newFile += d.data; - else - newFile += "+" + d.data; + var newRes = CheckData(newFile) + var oldRes = CheckData(oldFile) - var newRes = CheckData(newFile); - var oldRes = CheckData(oldFile); + if (oldRes.count > 0) { logger.Log('\t\told public result: ' + oldRes.count, logger.GetColor('blue')) } else { logger.Log('\t\told public NLOD error, ' + oldRes.log, logger.GetColor('redbg'), true) } - if (oldRes.count > 0) - logger.Log("\t\told public result: " + oldRes.count, logger.GetColor("blue")); - else - logger.Log("\t\told public NLOD error, " + oldRes.log, logger.GetColor("redbg"), true); + if (newRes.count > 0) { logger.Log('\t\tnew file result: ' + newRes.count, logger.GetColor('blue')) } else { logger.Log('\t\tnew file NLOD error, ' + newRes.log, logger.GetColor('redbg'), true) } - if (newRes.count > 0) - logger.Log("\t\tnew file result: " + newRes.count, logger.GetColor("blue")); - else - logger.Log("\t\tnew file NLOD error, " + newRes.log, logger.GetColor("redbg"), true); + utils.WriteFile(newFile, file) + logger.Log('\t\tNew data written to: ' + file) - utils.WriteFile(newFile, file); - logger.Log("\t\tNew data written to: " + file); - - return newRes.count - oldRes.count; - } else - logger.Log("\t\tNo new data"); - - - } catch (e) { - Beep(); - logger.Log("\tError at processing data! File: " + file, logger.GetColor("redbg")); - logger.Log(e.toString(), logger.GetColor("redbg")); - } - return -1; + return newRes.count - oldRes.count + } else { logger.Log('\t\tNo new data') } + } catch (e) { + logger.Log('\tError at processing data! File: ' + file, logger.GetColor('redbg')) + logger.Log(e.toString(), logger.GetColor('redbg')) + } + return -1 } -function ProcessIncomingRequest(data) { +function ProcessIncomingRequest (data) { + if (data === undefined) { + logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) + return + } - if (data == undefined) { - logger.Log("\tRecieved data is undefined!", logger.GetColor("redbg")); - return; - } + try { + let towrite = logger.GetDateString() + '\n' + towrite += '------------------------------------------------------------------------------\n' + towrite += data + towrite += '\n------------------------------------------------------------------------------\n' + utils.AppendToFile(towrite, recDataFile) + } catch (e) { + logger.log('Error writing recieved data.') + } - try { - let towrite = logger.GetDateString() + "\n"; - towrite += "------------------------------------------------------------------------------\n"; - towrite += data - towrite += "\n------------------------------------------------------------------------------\n"; - utils.AppendToFile(towrite, recDataFile); - } catch (e) { - logger.log("Error writing recieved data."); - } + try { + var d = JSON.parse(data) + var dfile = utils.ReadFile(dataFile) + data = LoadJSON(dfile) + var allQuestions = [] + for (let i = 0; i < d.allData.length; i++) { + allQuestions.push(new Question(d.allData[i].Q, d.allData[i].A, d.allData[i].I)) + } + var questions = [] + for (let i = 0; i < d.data.length; i++) { + let q = new Question(d.data[i].Q, d.data[i].A, d.data[i].I) + questions.push(q) + data.AddQuestion(d.subj, q) + } - try { - var d = JSON.parse(data); - var dfile = utils.ReadFile(dataFile); - var data = LoadJSON(dfile); - var allQuestions = []; - for ( var i = 0; i < d.allData.length; i++){ - allQuestions.push(new Question(d.allData[i].Q, d.allData[i].A, d.allData[i].I)); - } - var questions = []; - for ( var i = 0; i < d.data.length; i++){ - let q = new Question(d.data[i].Q, d.data[i].A, d.data[i].I); - questions.push(q); - data.AddQuestion(d.subj, q); - } - - logger.Log("\t" + d.subj); - var msg = "All / new count: " + allQuestions.length + " / " + questions.length; - if (d.version != undefined) - msg += ". Version: " + d.version; - var color = logger.GetColor("green"); + logger.Log('\t' + d.subj) + var msg = 'All / new count: ' + allQuestions.length + ' / ' + questions.length + if (d.version !== undefined) { msg += '. Version: ' + d.version } + var color = logger.GetColor('green') - try { - data.version = utils.ReadFile(versionFile); - data.motd = utils.ReadFile(motdFile); - } catch (e) { - Log("MOTD/Version writing/reading error!"); - } + try { + data.version = utils.ReadFile(versionFile) + data.motd = utils.ReadFile(motdFile) + } catch (e) { + logger.Log('MOTD/Version writing/reading error!') + } - if (data != undefined && d.data.length > 0){ - utils.WriteBackup(); - utils.WriteFile(JSON.stringify(data), dataFile); - msg += " - Data file written!"; - var color = logger.GetColor("blue"); - } - logger.Log("\t" + msg, color); + if (data !== undefined && d.data.length > 0) { + utils.WriteBackup() + utils.WriteFile(JSON.stringify(data), dataFile) + msg += ' - Data file written!' + color = logger.GetColor('blue') + } + logger.Log('\t' + msg, color) + } catch (e) { + logger.Log('Couldnt parse JSON data, trying old format...', logger.GetColor('redbg')) + d = SetupData(data) + var qcount = -1 + try { + var splitted = d.alldata.split('\n') + var count = 0 + for (var i = 0; i < splitted.length; i++) { + if (splitted[i][0] === '?') { count++ } + } + qcount = count + } catch (e) { console.log('Error :c'); console.log(e) } - } catch (e) { - logger.Log("Couldnt parse JSON data, trying old format...", logger.GetColor("redbg")); - var d = SetupData(data); - var qcount = -1; - try { - var splitted = d.alldata.split("\n"); - var count = 0; - for (var i = 0; i < splitted.length; i++) - if (splitted[i][0] == '?') - count ++; - qcount = count; - } catch (e) {console.log("Error :c"); console.log(e);} + logger.Log('\tProcessing data: ' + d.subj + ' (' + d.type + '), count: ' + qcount, logger.GetColor('green')) + if (d.subj === undefined) { + logger.Log(JSON.stringify(d), logger.GetColor('red')) + return + } - logger.Log("\tProcessing data: " + d.subj + " (" + d.type + "), count: " + qcount, logger.GetColor("green")); - if (d.subj == undefined){ - logger.Log(JSON.stringify(d), logger.GetColor("red")); - return; - } + var newStatItems = Process(d, staticFile) - var newItems = 0; - - var newStatItems = Process(d, staticFile); - - PrintNewCount(d, newStatItems, staticFile); - } + PrintNewCount(d, newStatItems, staticFile) + } } -function PrintNewCount(d, newItems, file) { - if (newItems > 0) { - var count = 0; - var splitted = d.alldata.split("\n"); - for (var i = 0; i < splitted.length; i++) - if (splitted[i].startsWith("?")) - count++; - logger.Log("\t" + file + " All / New: " + count + " / " + newItems, logger.GetColor("cyan")); - } +function PrintNewCount (d, newItems, file) { + if (newItems > 0) { + var count = 0 + var splitted = d.alldata.split('\n') + for (var i = 0; i < splitted.length; i++) { + if (splitted[i].startsWith('?')) { count++ } + } + logger.Log('\t' + file + ' All / New: ' + count + ' / ' + newItems, logger.GetColor('cyan')) + } } -function SetupData(data) { - var pdata = data.split("<#>"); - if (pdata.length <= 0){ - logger.Log("Data length is zero !", logger.GetColor("redbg")); - throw "No data recieved!"; - } +function SetupData (data) { + var pdata = data.split('<#>') + if (pdata.length <= 0) { + logger.Log('Data length is zero !', logger.GetColor('redbg')) + throw 'No data recieved!' + } - var d = {}; // parsed data - for (var i = 0; i < pdata.length; i++) { - var td = pdata[i].split("<=>"); - if (td.length == 2) - d[td[0]] = td[1]; - else { - logger.Log("Invalid parameter!", logger.GetColor("redbg")); - throw "Invalid parameter recieved!"; - } - } - return d; + var d = {} // parsed data + for (var i = 0; i < pdata.length; i++) { + var td = pdata[i].split('<=>') + if (td.length === 2) { d[td[0]] = td[1] } else { + logger.Log('Invalid parameter!', logger.GetColor('redbg')) + throw 'Invalid parameter recieved!' + } + } + return d } -function CheckData(data) { - try { - var presult = NLoad(data); - return presult; - } catch (e) { - logger.Log("Load error, " + e.toString(), logger.GetColor("redbg"), true); - return { - count: -1, - log: [e.toString()] - }; - } +function CheckData (data) { + try { + var presult = NLoad(data) + return presult + } catch (e) { + logger.Log('Load error, ' + e.toString(), logger.GetColor('redbg'), true) + return { + count: -1, + log: [e.toString()] + } + } } -function NLoad(resource) { - var resultLog = []; - var count = -1; - var allCount = 0; - if (resource == undefined) - throw "A megadott adat undefined!"; - resource = resource.split("\n"); // splitting by enters - if (resource.length == 1) - throw "A megadott adat nem sorokból áll!"; - var data = []; // initializing data declared at the begining +function NLoad (resource) { + var resultLog = [] + var allCount = 0 + if (resource === undefined) { throw 'A megadott adat undefined!' } + resource = resource.split('\n') // splitting by enters + if (resource.length === 1) { throw 'A megadott adat nem sorokból áll!' } + var data = [] // initializing data declared at the begining - function AddNewSubj(name) // adds a new subject to the data - { - data.push({ - "questions": [] - }); // ads aa new object, with an empty array in it - GetCurrSubj().name = name; // sets the name for it - GetCurrSubj().count = 0; // setting count to default - // setting if its active only if its undefined, otherwise previous user setting shouldt be overwritten - } + function AddNewSubj (name) { + data.push({ + 'questions': [] + }) // ads aa new object, with an empty array in it + GetCurrSubj().name = name // sets the name for it + GetCurrSubj().count = 0 // setting count to default + // setting if its active only if its undefined, otherwise previous user setting shouldt be overwritten + } - function AddItem() // adds an item to the last subjects questions - { - GetCurrSubj().count++; - allCount++; // incrementing all count - GetCurrSubj().questions.push({}); // adding a new empty object to the last item in the data - } + function AddItem () // adds an item to the last subjects questions + { + GetCurrSubj().count++ + allCount++ // incrementing all count + GetCurrSubj().questions.push({}) // adding a new empty object to the last item in the data + } - function GetLastItem() // returns the last item of the last item of data - { - var q = GetCurrSubj().questions.length; // questions length - return GetCurrSubj().questions[q - 1]; - } + function GetLastItem () // returns the last item of the last item of data + { + var q = GetCurrSubj().questions.length // questions length + return GetCurrSubj().questions[q - 1] + } - function GetCurrSubj() { - return data[data.length - 1]; - } + function GetCurrSubj () { + return data[data.length - 1] + } - // ? : question - // ! : answer - // > : image JSON data - // + : subject name + // ? : question + // ! : answer + // > : image JSON data + // + : subject name - // checking for name - for (var j = 0; j < resource.length; j++) // goes through resources - { - if (resource[j][0] == '+') // if there is a name identifier - { - break; // breaks, couse there will be a name - } - if (resource[j][0] == '?' || resource[j][0] == '!' || resource[j][0] == '>') // if it begins with another identifier: - { - AddNewSubj("NONAME"); // there is no name (for the first question at least), so setting it noname. - break; - } - // else it does nothing, continues to check - } - var jumped = 0; // the amount of lines it processed - for (var i = 0; i < resource.length; i += jumped) // gouing through the resource - { - jumped = 0; // resetting it to 0 - var currRawDataQ = resource[i]; // current question - var currRawDataA = resource[i + 1]; // current answer - var currRawDataI = resource[i + 2]; // current image - if (currRawDataQ != undefined && currRawDataQ[0] == '?' && currRawDataA != undefined && + // checking for name + for (var j = 0; j < resource.length; j++) // goes through resources + { + if (resource[j][0] == '+') // if there is a name identifier + { + break // breaks, couse there will be a name + } + if (resource[j][0] == '?' || resource[j][0] == '!' || resource[j][0] == '>') // if it begins with another identifier: + { + AddNewSubj('NONAME') // there is no name (for the first question at least), so setting it noname. + break + } + // else it does nothing, continues to check + } + var jumped = 0 // the amount of lines it processed + for (var i = 0; i < resource.length; i += jumped) // gouing through the resource + { + jumped = 0 // resetting it to 0 + var currRawDataQ = resource[i] // current question + var currRawDataA = resource[i + 1] // current answer + var currRawDataI = resource[i + 2] // current image + if (currRawDataQ != undefined && currRawDataQ[0] == '?' && currRawDataA != undefined && currRawDataA[0] == '!') // if the current line is ? and the next is ! its a data - { - AddItem(); - GetLastItem().q = currRawDataQ.substr(1); - GetLastItem().a = currRawDataA.substr(1); - jumped += 2; - if (currRawDataI != undefined && currRawDataI[0] == '>') { - GetLastItem().i = currRawDataI.substr(1); - jumped++; - } - } else if (currRawDataQ[0] == '+') // if its a new subject - { - AddNewSubj(currRawDataQ.substr(1)); - jumped++; - } else { - // this should be invalid question order - resultLog.push("Warning @ line " + i + ":" + currRawDataQ + " " + currRawDataA + " " + - currRawDataI); - jumped++; - } - } // end of parsing all data - return { - count: allCount, - log: resultLog - }; + { + AddItem() + GetLastItem().q = currRawDataQ.substr(1) + GetLastItem().a = currRawDataA.substr(1) + jumped += 2 + if (currRawDataI != undefined && currRawDataI[0] == '>') { + GetLastItem().i = currRawDataI.substr(1) + jumped++ + } + } else if (currRawDataQ[0] == '+') // if its a new subject + { + AddNewSubj(currRawDataQ.substr(1)) + jumped++ + } else { + // this should be invalid question order + resultLog.push('Warning @ line ' + i + ':' + currRawDataQ + ' ' + currRawDataA + ' ' + + currRawDataI) + jumped++ + } + } // end of parsing all data + return { + count: allCount, + log: resultLog + } } // loading stuff -function LoadJSON(resource) { - var count = -1; - try { - var d = JSON.parse(resource); - var r = new QuestionDB(); - var rt = []; - var allCount = -1; +function LoadJSON (resource) { + var count = -1 + try { + var d = JSON.parse(resource) + var r = new QuestionDB() + var rt = [] + var allCount = -1 - for (var i = 0; i < d.Subjects.length; i++) { - let s = new Subject(d.Subjects[i].Name); - var j = 0; - for (j = 0; j < d.Subjects[i].Questions.length; j++) { - var currQ = d.Subjects[i].Questions[j]; - s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I)); - } - rt.push({ - name: d.Subjects[i].Name, - count: j - }); - allCount += j; - r.AddSubject(s); - } - return r; - } catch (e) { - logger.Log("Error loading sutff", logger.GetColor("redbg"), true); - } + for (var i = 0; i < d.Subjects.length; i++) { + let s = new Subject(d.Subjects[i].Name) + var j = 0 + for (j = 0; j < d.Subjects[i].Questions.length; j++) { + var currQ = d.Subjects[i].Questions[j] + s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I)) + } + rt.push({ + name: d.Subjects[i].Name, + count: j + }) + allCount += j + r.AddSubject(s) + } + return r + } catch (e) { + logger.Log('Error loading sutff', logger.GetColor('redbg'), true) + } } -function ProcessQA() { - if (!utils.FileExists(qaFile)) - utils.WriteFile("", qaFile); +function ProcessQA () { + if (!utils.FileExists(qaFile)) { utils.WriteFile('', qaFile) } - let a = utils.ReadFile(qaFile).split("\n"); - let r = []; - let ind = 0; - for (let i = 0; i < a.length; i++) { - if ( a[i] == "#") - ind ++; - else { - if (r[ind] == undefined) - r[ind] = {}; + let a = utils.ReadFile(qaFile).split('\n') + let r = [] + let ind = 0 + for (let i = 0; i < a.length; i++) { + if (a[i] == '#') { ind++ } else { + if (r[ind] == undefined) { r[ind] = {} } - if (r[ind].q == undefined) { - r[ind].q = a[i]; - } else { - if (r[ind].a == undefined) - r[ind].a = []; + if (r[ind].q == undefined) { + r[ind].q = a[i] + } else { + if (r[ind].a == undefined) { r[ind].a = [] } - r[ind].a.push(a[i]); - } - } - } - - return r; + r[ind].a.push(a[i]) + } + } + } + + return r } From 9cacee321cb9302164eea0a7e57d7ffd43e95eef Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:33:38 +0200 Subject: [PATCH 09/18] logger.js refactoring --- utils/logger.js | 132 ++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 73 deletions(-) diff --git a/utils/logger.js b/utils/logger.js index bd848f0..b9ff95f 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -1,4 +1,4 @@ -/* ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- Question Server GitLab: @@ -19,86 +19,72 @@ ------------------------------------------------------------------------- */ module.exports = { - GetDateString: GetDateString, - Log: Log, - GetColor: GetColor, - LogReq: LogReq -}; - -const DELIM = "|"; - -var utils = require('./utils.js'); -const nlogFile = "stats/nlogs"; -const logFile = "/nlogs/nlogs"; -const locLogFile = "stats/logs"; -const allLogFile = "/nlogs/log"; - -function GetDateString() { - var m = new Date(); - return m.getFullYear() + "/" + - ("0" + (m.getMonth() + 1)).slice(-2) + "/" + - ("0" + m.getDate()).slice(-2) + " " + - ("0" + m.getHours()).slice(-2) + ":" + - ("0" + m.getMinutes()).slice(-2) + ":" + - ("0" + m.getSeconds()).slice(-2); + GetDateString: GetDateString, + Log: Log, + GetColor: GetColor, + LogReq: LogReq } -function Log(s, c, b) { - if (c != undefined) - console.log(c, GetDateString() + DELIM + s); - else - console.log(GetDateString() + DELIM + s); +const DELIM = '|' - if (b) - utils.Beep(); +var utils = require('../utils/utils.js') +const nlogFile = '../stats/nlogs' +const locLogFile = '../stats/logs' +const logFile = '/nlogs/nlogs' +const allLogFile = '/nlogs/log' - utils.AppendToFile(GetDateString() + DELIM + s, nlogFile); - utils.AppendToFile(GetDateString() + DELIM + s, logFile); +function GetDateString () { + var m = new Date() + return m.getFullYear() + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + + ('0' + m.getDate()).slice(-2) + ' ' + + ('0' + m.getHours()).slice(-2) + ':' + + ('0' + m.getMinutes()).slice(-2) + ':' + + ('0' + m.getSeconds()).slice(-2) } -function LogReq(req, toFile, sc) { - try { - var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress; - var logEntry = ip + DELIM + req.hostname + DELIM + req.headers['user-agent'] + - DELIM + req.method + DELIM; +function Log (s, c, b) { + if (c != undefined) { console.log(c, GetDateString() + DELIM + s) } else { console.log(GetDateString() + DELIM + s) } - logEntry += req.url; + if (b) { utils.Beep() } - if (sc != undefined && sc == 404) - logEntry += DELIM + sc; - var color = GetColor("green"); - - if (req.url.toLowerCase().includes("isadding")) - color = GetColor("yellow"); - if (!toFile) { - Log(logEntry, color); - } else { - var defLogs = GetDateString() + DELIM + logEntry; - var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req.body) + "\n"; - - utils.AppendToFile(defLogs, locLogFile); - utils.AppendToFile(defLogs, allLogFile); - } - - } catch (e) { - console.log(e); - Log("Error at logging lol", GetColor("redbg"), true); - } + utils.AppendToFile(GetDateString() + DELIM + s, nlogFile) + utils.AppendToFile(GetDateString() + DELIM + s, logFile) } -function GetColor(c) { - if (c == "redbg") - return '\x1b[41m%s\x1b[0m'; - if (c == "bluebg") - return '\x1b[44m%s\x1b[0m'; - if (c == "red") - return '\x1b[31m%s\x1b[0m'; - if (c == "green") - return '\x1b[32m%s\x1b[0m'; - if (c == "yellow") - return '\x1b[33m%s\x1b[0m'; - if (c == "blue") - return '\x1b[34m%s\x1b[0m'; - if (c == "cyan") - return '\x1b[36m%s\x1b[0m'; +function LogReq (req, toFile, sc) { + try { + var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress + var logEntry = ip + DELIM + req.hostname + DELIM + req.headers['user-agent'] + + DELIM + req.method + DELIM + + logEntry += req.url + + if (sc != undefined && sc == 404) { logEntry += DELIM + sc } + var color = GetColor('green') + + if (req.url.toLowerCase().includes('isadding')) { color = GetColor('yellow') } + if (!toFile) { + Log(logEntry, color) + } else { + var defLogs = GetDateString() + DELIM + logEntry + var extraLogs = '\n\t' + JSON.stringify(req.headers) + '\n\t' + JSON.stringify(req.body) + '\n' + + utils.AppendToFile(defLogs, locLogFile) + utils.AppendToFile(defLogs, allLogFile) + } + } catch (e) { + console.log(e) + Log('Error at logging lol', GetColor('redbg'), true) + } +} + +function GetColor (c) { + if (c == 'redbg') { return '\x1b[41m%s\x1b[0m' } + if (c == 'bluebg') { return '\x1b[44m%s\x1b[0m' } + if (c == 'red') { return '\x1b[31m%s\x1b[0m' } + if (c == 'green') { return '\x1b[32m%s\x1b[0m' } + if (c == 'yellow') { return '\x1b[33m%s\x1b[0m' } + if (c == 'blue') { return '\x1b[34m%s\x1b[0m' } + if (c == 'cyan') { return '\x1b[36m%s\x1b[0m' } } From 50a1dd91eb1de0c34bd18cf384db17a3adc811be Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:34:55 +0200 Subject: [PATCH 10/18] merger, changedataversion, motd refactor --- utils/changedataversion.js | 39 +-- utils/merger.js | 690 ++++++++++++++++++------------------- utils/motd.js | 33 +- 3 files changed, 364 insertions(+), 398 deletions(-) diff --git a/utils/changedataversion.js b/utils/changedataversion.js index 2b5052a..94582e2 100644 --- a/utils/changedataversion.js +++ b/utils/changedataversion.js @@ -1,32 +1,31 @@ -const utils = require('./utils.js'); -const dataFile = "public/data.json"; -const versionFile = "public/version"; +const utils = require('../utils/utils.js') +const dataFile = '../public/data.json' +const versionFile = '../public/version' -var p = GetParams(); +var p = GetParams() if (p.length <= 0) { - console.log("no params!"); - process.exit(0); + console.log('no params!') + process.exit(0) } +var param = p.join(' ') -var param = p.join(" "); +console.log('param: ' + param) -console.log("param: " + param); +var d = utils.ReadFile(dataFile) +var parsed = JSON.parse(d) -var d = utils.ReadFile(dataFile); -var parsed = JSON.parse(d); +console.log('Old version:') +console.log(parsed.version) -console.log("Old version:"); -console.log(parsed.version); +parsed.version = param -parsed.version = param; +console.log('New version:') +console.log(parsed.version) -console.log("New version:"); -console.log(parsed.version); +utils.WriteFile(JSON.stringify(parsed), dataFile) +utils.WriteFile(parsed.version, versionFile) -utils.WriteFile(JSON.stringify(parsed), dataFile); -utils.WriteFile(parsed.version, versionFile); - -function GetParams() { - return process.argv.splice(2); +function GetParams () { + return process.argv.splice(2) } diff --git a/utils/merger.js b/utils/merger.js index 2833705..b3b0e07 100644 --- a/utils/merger.js +++ b/utils/merger.js @@ -1,4 +1,4 @@ -/* ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- Question Server question file merger GitLab: @@ -22,280 +22,256 @@ // join json datas, or raw datas // or something else -const minMatchAmmount = 55; -const minResultMatchPercent = 99; -const lengthDiffMultiplier = 10; +const minMatchAmmount = 55 +const minResultMatchPercent = 99 +const lengthDiffMultiplier = 10 class Question { - constructor(q, a, i) { - this.Q = q; - this.A = a; - this.I = i; - } - toString() { - var r = "?" + this.Q + "\n!" + this.A; - if (this.I) - r += "\n>" + this.I; - return r; - } - HasQuestion() { - return this.Q != undefined; - } - HasAnswer() { - return this.A != undefined; - } - HasImage() { - return this.I != undefined; - } - IsComplete() { - return this.HasQuestion() && this.HasAnswer(); - } - // TODO: TEST DIS - Compare(q2, i) { - if (typeof q2 == 'string') { - var qmatchpercent = Question.CompareString(this.Q, q2); + constructor (q, a, i) { + this.Q = q + this.A = a + this.I = i + } + toString () { + var r = '?' + this.Q + '\n!' + this.A + if (this.I) { r += '\n>' + this.I } + return r + } + HasQuestion () { + return this.Q != undefined + } + HasAnswer () { + return this.A != undefined + } + HasImage () { + return this.I != undefined + } + IsComplete () { + return this.HasQuestion() && this.HasAnswer() + } + // TODO: TEST DIS + Compare (q2, i) { + if (typeof q2 === 'string') { + var qmatchpercent = Question.CompareString(this.Q, q2) - if (i == undefined || i.length == 0) - return qmatchpercent; - else { - if (this.HasImage()) { - const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(" "), i.join(" ")) : - 0; - return (qmatchpercent + imatchpercent) / 2; - } else { - qmatchpercent -= 30; - if (qmatchpercent < 0) - return 0; - else - return qmatchpercent; - } - } - } else { - const qmatchpercent = Question.CompareString(this.Q, q2.Q); - const amatchpercent = Question.CompareString(this.A, q2.A); - if (this.I != undefined) { - const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(" "), q2.I.join( - " ")) : 0; - return (qmatchpercent + amatchpercent + imatchpercent) / 3; - } else { - return (qmatchpercent + amatchpercent) / 2; - } - } - } - static CompareString(s1, s2) { - s1 = SimplifyStringForComparison(s1).split(" "); - s2 = SimplifyStringForComparison(s2).split(" "); - var match = 0; - for (var i = 0; i < s1.length; i++) - if (s2.includes(s1[i])) - match++; - var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent - var lengthDifference = Math.abs(s2.length - s1.length); - percent -= lengthDifference * lengthDiffMultiplier; - if (percent < 0) - percent = 0; - return percent; - } + if (i == undefined || i.length == 0) { return qmatchpercent } else { + if (this.HasImage()) { + const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(' '), i.join(' ')) + : 0 + return (qmatchpercent + imatchpercent) / 2 + } else { + qmatchpercent -= 30 + if (qmatchpercent < 0) { return 0 } else { return qmatchpercent } + } + } + } else { + const qmatchpercent = Question.CompareString(this.Q, q2.Q) + const amatchpercent = Question.CompareString(this.A, q2.A) + if (this.I != undefined) { + const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(' '), q2.I.join( + ' ')) : 0 + return (qmatchpercent + amatchpercent + imatchpercent) / 3 + } else { + return (qmatchpercent + amatchpercent) / 2 + } + } + } + static CompareString (s1, s2) { + s1 = SimplifyStringForComparison(s1).split(' ') + s2 = SimplifyStringForComparison(s2).split(' ') + var match = 0 + for (var i = 0; i < s1.length; i++) { + if (s2.includes(s1[i])) { match++ } + } + var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent + var lengthDifference = Math.abs(s2.length - s1.length) + percent -= lengthDifference * lengthDiffMultiplier + if (percent < 0) { percent = 0 } + return percent + } } class Subject { - constructor(n) { - this.Name = n; - this.Questions = []; - } - get length() { - return this.Questions.length; - } - AddQuestion(q) { - this.Questions.push(q); - } - toString() { - var r = []; - for (var i = 0; i < this.Questions.length; i++) - r.push(this.Questions[i].toString()); - return "+" + this.Name + "\n" + r.join("\n"); - } + constructor (n) { + this.Name = n + this.Questions = [] + } + get length () { + return this.Questions.length + } + AddQuestion (q) { + this.Questions.push(q) + } + toString () { + var r = [] + for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) } + return '+' + this.Name + '\n' + r.join('\n') + } } class QuestionDB { - constructor() { - this.Subjects = []; - } - get length() { - return this.Subjects.length; - } - AddQuestion(subj, q) { - var i = 0; - while (i < this.Subjects.length && this.Subjects[i].Name != subj) - i++; - if (i < this.Subjects.length) - this.Subjects[i].AddQuestion(q); - else { - const n = new Subject(subj); - n.AddQuestion(q); - this.Subjects.push(n); - } - } - AddSubject(subj) { - var i = 0; - while (i < this.length && subj.Name != this.Subjects[i].Name) - i++; + constructor () { + this.Subjects = [] + } + get length () { + return this.Subjects.length + } + AddQuestion (subj, q) { + var i = 0 + while (i < this.Subjects.length && this.Subjects[i].Name != subj) { i++ } + if (i < this.Subjects.length) { this.Subjects[i].AddQuestion(q) } else { + const n = new Subject(subj) + n.AddQuestion(q) + this.Subjects.push(n) + } + } + AddSubject (subj) { + var i = 0 + while (i < this.length && subj.Name != this.Subjects[i].Name) { i++ } - if (i < this.length) { - this.Subjects.concat(subj.Questions); - } else { - this.Subjects.push(subj); - } - } - toString() { - var r = []; - for (var i = 0; i < this.Subjects.length; i++) - r.push(this.Subjects[i].toString()); - return r.join("\n\n"); - } + if (i < this.length) { + this.Subjects.concat(subj.Questions) + } else { + this.Subjects.push(subj) + } + } + toString () { + var r = [] + for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) } + return r.join('\n\n') + } } -var utils = require('./utils.js'); -var actions = require('./actions.js'); +var utils = require('./utils.js') +var actions = require('./actions.js') -Main(); +Main() -function Main() { - console.clear(); - const params = GetParams(); - console.log(params); - var dbs = []; +function Main () { + console.clear() + const params = GetParams() + console.log(params) + var dbs = [] + for (var i = 0; i < params.length; i++) { + PrintLN() + console.log(params[i] + ': ') + try { + dbs.push(ParseJSONData(utils.ReadFile(params[i]))) + console.log('JSON data added') + } catch (e) { + console.log(e) + console.log('Trying with old format...') + dbs.push(ReadData(utils.ReadFile(params[i])).result) + } + } + PrintLN() - for (var i = 0; i < params.length; i++) { - PrintLN(); - console.log(params[i] + ": "); - try { - dbs.push(ParseJSONData(utils.ReadFile(params[i]))); - console.log("JSON data added"); - } catch (e) { - console.log(e); - console.log("Trying with old format..."); - dbs.push(ReadData(utils.ReadFile(params[i])).result); - } - } - PrintLN(); + dbs.forEach((item) => { + PrintDB(item) + }) - dbs.forEach((item) => { - PrintDB(item); - }); + var olds = [] + if (dbs.length == 1) { + for (let i = 0; i < dbs[0].length; i++) { olds.push(dbs[0].Subjects[i].length) } + } - var olds = []; - if (dbs.length == 1) { - for ( let i = 0; i < dbs[0].length; i++) - olds.push(dbs[0].Subjects[i].length); - } + console.log('Parsed data count: ' + dbs.length) + PrintLN() - console.log("Parsed data count: " + dbs.length); - PrintLN(); + console.log('Merging databases...') + var db = MergeDatabases(dbs) - console.log("Merging databases..."); - var db = MergeDatabases(dbs); + console.log('Removing duplicates...') + var r = RemoveDuplicates(db) - console.log("Removing duplicates..."); - var r = RemoveDuplicates(db); + console.log('RESULT:') + PrintDB(r, olds) - console.log("RESULT:"); - PrintDB(r, olds); - - utils.WriteFile(JSON.stringify(r), "newData"); - console.log("File written!"); + utils.WriteFile(JSON.stringify(r), 'newData') + console.log('File written!') } -function PrintLN() { - console.log("------------------------------------------------------"); +function PrintLN () { + console.log('------------------------------------------------------') } -function PrintDB(r, olds) { - console.log("Data subject count: " + r.length); - var maxLength = 0; - for (var i = 0; i < r.length; i++) { - if (maxLength < r.Subjects[i].Name.length) - maxLength = r.Subjects[i].Name.length; - } +function PrintDB (r, olds) { + console.log('Data subject count: ' + r.length) + var maxLength = 0 + for (var i = 0; i < r.length; i++) { + if (maxLength < r.Subjects[i].Name.length) { maxLength = r.Subjects[i].Name.length } + } - let qcount = 0; + let qcount = 0 - for (var i = 0; i < r.length; i++) { - let line = i; - if (line < 10) - line += ' '; + for (var i = 0; i < r.length; i++) { + let line = i + if (line < 10) { line += ' ' } - line += ": "; - var currLength = line.length + maxLength + 4; - line += r.Subjects[i].Name; + line += ': ' + var currLength = line.length + maxLength + 4 + line += r.Subjects[i].Name - while (line.length < currLength) { - if (i % 4 == 0) - line += "."; - else - line += " "; - } + while (line.length < currLength) { + if (i % 4 == 0) { line += '.' } else { line += ' ' } + } - if (olds && olds.length > 0) { - // TODO: check if correct row! should be now, but well... - if (olds[i] < 10) - line += " "; - if (olds[i] < 100) - line += " "; + if (olds && olds.length > 0) { + // TODO: check if correct row! should be now, but well... + if (olds[i] < 10) { line += ' ' } + if (olds[i] < 100) { line += ' ' } - line += olds[i]; - line += " -> "; - } + line += olds[i] + line += ' -> ' + } - if (r.Subjects[i].length < 10) - line += " "; - if (r.Subjects[i].length < 100) - line += " "; + if (r.Subjects[i].length < 10) { line += ' ' } + if (r.Subjects[i].length < 100) { line += ' ' } - line += r.Subjects[i].length; - qcount += r.Subjects[i].length; - - line += " db"; - - console.log(line); - } + line += r.Subjects[i].length + qcount += r.Subjects[i].length - console.log("Total questions: " + qcount); + line += ' db' - PrintLN(); + console.log(line) + } + + console.log('Total questions: ' + qcount) + + PrintLN() } -function GetParams() { - return process.argv.splice(2); +function GetParams () { + return process.argv.splice(2) } -function ParseJSONData(data) { - var d = JSON.parse(data); - var r = new QuestionDB(); - var rt = []; +function ParseJSONData (data) { + var d = JSON.parse(data) + var r = new QuestionDB() + var rt = [] - for (var i = 0; i < d.Subjects.length; i++) { - let s = new Subject(d.Subjects[i].Name); - var j = 0; - for (j = 0; j < d.Subjects[i].Questions.length; j++) { - var currQ = d.Subjects[i].Questions[j]; - s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I)); - } - rt.push({ - name: d.Subjects[i].Name, - count: j - }); - r.AddSubject(s); - } - return r; + for (var i = 0; i < d.Subjects.length; i++) { + let s = new Subject(d.Subjects[i].Name) + var j = 0 + for (j = 0; j < d.Subjects[i].Questions.length; j++) { + var currQ = d.Subjects[i].Questions[j] + s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I)) + } + rt.push({ + name: d.Subjects[i].Name, + count: j + }) + r.AddSubject(s) + } + return r } -function MergeDatabases(dbs) { - var db = new QuestionDB(); - for (var i = 0; i < dbs.length; i++) - for (var j = 0; j < dbs[i].length; j++) - db.AddSubject(dbs[i].Subjects[j]); - return db; +function MergeDatabases (dbs) { + var db = new QuestionDB() + for (var i = 0; i < dbs.length; i++) { + for (var j = 0; j < dbs[i].length; j++) { db.AddSubject(dbs[i].Subjects[j]) } + } + return db } /* @@ -303,150 +279,142 @@ function MergeDatabases(dbs) { * Parameter should be raw read file in string with "\n"-s * TODO: ??? -s are not listed as errors, tho works correctly * */ -function ReadData(data) { +function ReadData (data) { + const d = data.split('\n') + const r = new QuestionDB() + var logs = [] + var currSubj = '' // the current subjects name + var ExpectedIdentifier = ['+', '?'] + let currQuestion = new Question() - const d = data.split("\n"); - const r = new QuestionDB(); - var logs = []; - var currSubj = ""; // the current subjects name - var ExpectedIdentifier = ['+', '?']; - let currQuestion = new Question(); + var i = -1 + while (i < d.length) { + let currIdentifier + let skipped = 0 + do { + if (skipped >= 1) { logs.push(i + ': ' + d[i]) } + i++ + if (i >= d.length) { + if (currQuestion.IsComplete()) { r.AddQuestion(currSubj, currQuestion) } + return { + result: r, + logs: logs + } + } + currIdentifier = d[i][0] + skipped++ + } while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length) - var i = -1; - while (i < d.length) { - let currIdentifier; - let skipped = 0; - do { - if (skipped >= 1) - logs.push(i + ": " + d[i]); - i++; - if (i >= d.length) { - if (currQuestion.IsComplete()) - r.AddQuestion(currSubj, currQuestion); - return { - result: r, - logs: logs - }; - } - currIdentifier = d[i][0]; - skipped++; - } while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length); + let currData = d[i].substring(1).trim() - let currData = d[i].substring(1).trim(); + if (currIdentifier == '+') { + if (currQuestion.IsComplete()) { r.AddQuestion(currSubj, currQuestion) } + currQuestion = new Question() + currSubj = currData + ExpectedIdentifier = ['?'] + continue + } - if (currIdentifier == '+') { - if (currQuestion.IsComplete()) - r.AddQuestion(currSubj, currQuestion); - currQuestion = new Question(); - currSubj = currData; - ExpectedIdentifier = ['?']; - continue; - } + if (currIdentifier == '?') { + if (currQuestion.IsComplete()) { + r.AddQuestion(currSubj, currQuestion) + currQuestion = new Question() + } + // overwriting is allowed here, bcus: + // ?????!> + currQuestion.Q = currData + ExpectedIdentifier = ['!', '?'] + continue + } - if (currIdentifier == '?') { - if (currQuestion.IsComplete()) { - r.AddQuestion(currSubj, currQuestion); - currQuestion = new Question(); - } - // overwriting is allowed here, bcus: - // ?????!> - currQuestion.Q = currData; - ExpectedIdentifier = ['!', '?']; - continue; - } + if (currIdentifier == '!') { + // if dont have question continue + if (!currQuestion.HasQuestion()) { throw 'No question! (A)' } + // dont allow overwriting + // ?!!!! + if (!currQuestion.HasAnswer()) { + currData = currData.replace('A helyes válaszok: ', '') + currData = currData.replace('A helyes válasz: ', '') - if (currIdentifier == '!') { - // if dont have question continue - if (!currQuestion.HasQuestion()) - throw "No question! (A)"; - // dont allow overwriting - // ?!!!! - if (!currQuestion.HasAnswer()) { - currData = currData.replace("A helyes válaszok: ", ""); - currData = currData.replace("A helyes válasz: ", ""); + currQuestion.A = currData + } + ExpectedIdentifier = ['?', '>', '+'] + continue + } - currQuestion.A = currData; - } - ExpectedIdentifier = ['?', '>', '+']; - continue; - } + if (currIdentifier == '>') { + // if dont have question or answer continue + if (!currQuestion.HasQuestion()) { throw 'No question! (I)' } + if (!currQuestion.HasAnswer()) { throw 'No asnwer! (I)' } + // dont allow overwriting + // ?!>>> + if (!currQuestion.HasImage()) { + try { + currQuestion.I = JSON.parse(currData) + } catch (e) { + currQuestion.I = currData.split(',') + } + } + ExpectedIdentifier = ['?', '+'] + continue + } + } - if (currIdentifier == '>') { - // if dont have question or answer continue - if (!currQuestion.HasQuestion()) - throw "No question! (I)"; - if (!currQuestion.HasAnswer()) - throw "No asnwer! (I)"; - // dont allow overwriting - // ?!>>> - if (!currQuestion.HasImage()) { - try { - currQuestion.I = JSON.parse(currData); - } catch (e) { - currQuestion.I = currData.split(','); - } - } - ExpectedIdentifier = ['?', '+']; - continue; - } - } - - return { - result: r, - logs: logs - }; + return { + result: r, + logs: logs + } } -function RemoveDuplicates(dataObj) { - for (var i = 0; i < dataObj.length; i++) - RemoveDuplFromSubject(dataObj.Subjects[i]); - return dataObj; +function RemoveDuplicates (dataObj) { + for (var i = 0; i < dataObj.length; i++) { RemoveDuplFromSubject(dataObj.Subjects[i]) } + return dataObj } -function RemoveDuplFromSubject(subj) { - var cp = subj.Questions; - subj.Questions = []; - for (var i = 0; i < cp.length; i++) { - var j = 0; - // Only removes 100% match! - while (j < subj.length && cp[i].Compare(subj.Questions[j]) != 100) { - j++; - } - if (j < subj.length) { - //console.log("----------------------------------------------------------"); - //console.log(cp[i].toString()); - //console.log(" VS "); - //console.log(subj.Questions[j].toString()); - //console.log(cp[i].Compare(subj.Questions[j])); - //console.log(j); - //console.log("removed:"); - //console.log(subj.Questions.splice(j, 1).toString()); - //console.log("----------------------------------------------------------"); - } else { - subj.AddQuestion(cp[i]); - } - } +function RemoveDuplFromSubject (subj) { + var cp = subj.Questions + subj.Questions = [] + for (var i = 0; i < cp.length; i++) { + var j = 0 + // Only removes 100% match! + while (j < subj.length && cp[i].Compare(subj.Questions[j]) != 100) { + j++ + } + if (j < subj.length) { + // console.log("----------------------------------------------------------"); + // console.log(cp[i].toString()); + // console.log(" VS "); + // console.log(subj.Questions[j].toString()); + // console.log(cp[i].Compare(subj.Questions[j])); + // console.log(j); + // console.log("removed:"); + // console.log(subj.Questions.splice(j, 1).toString()); + // console.log("----------------------------------------------------------"); + } else { + subj.AddQuestion(cp[i]) + } + } } -function SimplifyStringForComparison(value) { - value = RemoveUnnecesarySpaces(value).toLowerCase(); - var removableChars = [",", ".", ":", "!"]; - for (var i = 0; i < removableChars.length; i++) { - var regex = new RegExp(removableChars[i], "g"); - value.replace(regex, ""); - } - return value; +function SimplifyStringForComparison (value) { + value = RemoveUnnecesarySpaces(value).toLowerCase() + var removableChars = [',', '.', ':', '!'] + for (var i = 0; i < removableChars.length; i++) { + var regex = new RegExp(removableChars[i], 'g') + value.replace(regex, '') + } + return value } -function RemoveUnnecesarySpaces(toremove) { - toremove = NormalizeSpaces(toremove); - while (toremove.includes(" ")) // while the text includes double spaces replaces all of them with a single one - { - toremove = toremove.replace(/ /g, " "); - } - return toremove.trim(); +function RemoveUnnecesarySpaces (toremove) { + toremove = NormalizeSpaces(toremove) + while (toremove.includes(' ')) // while the text includes double spaces replaces all of them with a single one + { + toremove = toremove.replace(/ {2}/g, ' ') + } + return toremove.trim() } -function NormalizeSpaces(input) { - return input.replace(/\s/g, ' '); +function NormalizeSpaces (input) { + return input.replace(/\s/g, ' ') } diff --git a/utils/motd.js b/utils/motd.js index 6a8bcc9..6f50a6a 100644 --- a/utils/motd.js +++ b/utils/motd.js @@ -1,24 +1,23 @@ -const utils = require('./utils.js'); -const dataFile = "public/data.json"; -const versionFile = "public/version"; -const motdFile = "public/motd"; +const utils = require('../utils/utils.js') +const dataFile = '../public/data.json' +const versionFile = '../public/version' +const motdFile = '../public/motd' -var p = GetParams(); +var p = GetParams() if (p.length <= 0) { - console.log("no params!"); - process.exit(0); + console.log('no params!') + process.exit(0) } +var param = p.join(' ') +console.log('param: ' + param) +var d = utils.ReadFile(dataFile) +var parsed = JSON.parse(d) +parsed.motd = param +utils.WriteFile(JSON.stringify(parsed), dataFile) -var param = p.join(" "); -console.log("param: " + param); -var d = utils.ReadFile(dataFile); -var parsed = JSON.parse(d); -parsed.motd = param; -utils.WriteFile(JSON.stringify(parsed), dataFile); +utils.WriteFile(parsed.motd, motdFile) -utils.WriteFile(parsed.motd, motdFile); - -function GetParams() { - return process.argv.splice(2); +function GetParams () { + return process.argv.splice(2) } From d4cd019f2931fd2110505c0dc3ae031425a37076 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:35:52 +0200 Subject: [PATCH 11/18] stats.js refactor --- utils/stat.js | 146 ++++++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 81 deletions(-) diff --git a/utils/stat.js b/utils/stat.js index 96bc53c..3d2d91c 100644 --- a/utils/stat.js +++ b/utils/stat.js @@ -1,4 +1,4 @@ -/* ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- Question Server GitLab: @@ -19,94 +19,78 @@ ------------------------------------------------------------------------- */ module.exports = { - LogStat: LogStat, - Load: Load -}; - -var utils = require('./utils.js'); -var logger = require('./logger.js'); - -const statFile = "stats/stats"; -const vStatFile = "stats/vstats"; -const writeInterval = 10; - -var data = {}; -var vData = {}; -var writes = 0; - -function Load() { - try { - var prevData = utils.ReadFile(statFile); - data = JSON.parse(prevData); - } catch (e) { - logger.Log("Error at loading logs! (@ first run its normal)", logger.GetColor("redbg")); - console.log(e); - } - - try { - var prevVData = utils.ReadFile(vStatFile); - vData = JSON.parse(prevVData); - } catch (e) { - logger.Log("Error at loading visit logs! (@ first run its normal)", logger.GetColor("redbg")); - console.log(e); - } + LogStat: LogStat, + Load: Load } -function LogStat(url) { - Inc(url); - AddVisitStat(url); - Save(); +var utils = require('../utils/utils.js') +var logger = require('../utils/logger.js') + +const statFile = '../stats/stats' +const vStatFile = '../stats/vstats' +const writeInterval = 10 + +var data = {} +var vData = {} +var writes = 0 + +function Load () { + try { + var prevData = utils.ReadFile(statFile) + data = JSON.parse(prevData) + } catch (e) { + logger.Log('Error at loading logs! (@ first run its normal)', logger.GetColor('redbg')) + console.log(e) + } + + try { + var prevVData = utils.ReadFile(vStatFile) + vData = JSON.parse(prevVData) + } catch (e) { + logger.Log('Error at loading visit logs! (@ first run its normal)', logger.GetColor('redbg')) + console.log(e) + } } -function Inc(value) { - if (value.startsWith("/?")) - value = "/"; - if (data[value] == undefined) - data[value] = 0; - data[value]++; +function LogStat (url) { + Inc(url) + AddVisitStat(url) + Save() } -function AddVisitStat(name) { - var m = new Date(); - const now = m.getFullYear() + "/" + - ("0" + (m.getMonth() + 1)).slice(-2) + "/" + - ("0" + m.getDate()).slice(-2); - if (vData[now] == undefined) - vData[now] = {}; - if (vData[now][name] == undefined) - vData[now][name] = 0; - vData[now][name]++; +function Inc (value) { + if (value.startsWith('/?')) { value = '/' } + if (data[value] == undefined) { data[value] = 0 } + data[value]++ } -function AddVisitStat(name) { - var m = new Date(); - const now = m.getFullYear() + "/" + - ("0" + (m.getMonth() + 1)).slice(-2) + "/" + - ("0" + m.getDate()).slice(-2); - if (vData[now] == undefined) - vData[now] = {}; - if (vData[now][name] == undefined) - vData[now][name] = 0; - vData[now][name]++; +function AddVisitStat (name) { + var m = new Date() + const now = m.getFullYear() + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + + ('0' + m.getDate()).slice(-2) + if (vData[now] == undefined) { vData[now] = {} } + if (vData[now][name] == undefined) { vData[now][name] = 0 } + vData[now][name]++ } -function Save() { - writes++; - if (writes == writeInterval) { - try { - utils.WriteFile(JSON.stringify(data), statFile); - // logger.Log("Stats wrote."); - } catch (e) { - logger.Log("Error at writing logs!", logger.GetColor("redbg")); - console.log(e); - } - try { - utils.WriteFile(JSON.stringify(vData), vStatFile); - // logger.Log("Stats wrote."); - } catch (e) { - logger.Log("Error at writing visit logs!", logger.GetColor("redbg")); - console.log(e); - } - writes = 0; - } +function Save () { + writes++ + if (writes == writeInterval) { + try { + utils.WriteFile(JSON.stringify(data), statFile) + // logger.Log("Stats wrote."); + } catch (e) { + logger.Log('Error at writing logs!', logger.GetColor('redbg')) + console.log(e) + } + try { + utils.WriteFile(JSON.stringify(vData), vStatFile) + // logger.Log("Stats wrote."); + } catch (e) { + logger.Log('Error at writing visit logs!', logger.GetColor('redbg')) + console.log(e) + } + writes = 0 + } } From e5fd0aba13db7627fe67a6772685c45c2f4d1084 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:36:24 +0200 Subject: [PATCH 12/18] utils.js refactor --- utils/utils.js | 169 +++++++++++++++++++++++-------------------------- 1 file changed, 79 insertions(+), 90 deletions(-) diff --git a/utils/utils.js b/utils/utils.js index e3f736b..8d2d2d5 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -1,109 +1,98 @@ module.exports = { - ReadFile: ReadFile, - WriteFile: WriteFile, - writeFileAsync: WriteFileAsync, - AppendToFile: AppendToFile, - Beep: Beep, - WriteBackup: WriteBackup, - FileExists: FileExists, - CreatePath: CreatePath, - GetAllFilesFromFolder: GetAllFilesFromFolder -}; - -var fs = require('fs'); - -var logger = require('./logger.js'); - -const recievedFile = "stats/recieved"; -const manFile = "public/man.html"; -const logFile = "stats/logs"; -const dataFile = "public/data.json"; - -function ReadFile(name) { - if (!FileExists(name)) - throw "No such file: " + name; - return fs.readFileSync(name, "utf8"); + ReadFile: ReadFile, + WriteFile: WriteFile, + writeFileAsync: WriteFileAsync, + AppendToFile: AppendToFile, + Beep: Beep, + WriteBackup: WriteBackup, + FileExists: FileExists, + CreatePath: CreatePath, + GetAllFilesFromFolder: GetAllFilesFromFolder } -function FileExists(path) { - return fs.existsSync(path); +var fs = require('fs') + +var logger = require('../utils/logger.js') + +const recievedFile = '../stats/recieved' +const manFile = '../public/man.html' +const logFile = '../stats/logs' +const dataFile = '../public/data.json' + +function ReadFile (name) { + if (!FileExists(name)) { throw 'No such file: ' + name } + return fs.readFileSync(name, 'utf8') } -function CreatePath(path, onlyPath) { - if (FileExists(path)) - return; - - var p = path.split("/"); - var currDir = p[0]; - for (var i = 1; i < p.length; i++) { - if (currDir != "" && !fs.existsSync(currDir)) { - try { - fs.mkdirSync(currDir); - } catch (e) { console.log("Failed to make " + currDir + " directory... "); } - } - currDir += "/" + p[i]; - } - if (onlyPath == undefined || onlyPath == false) - fs.writeFileSync(path, ""); - else - fs.mkdirSync(path); - +function FileExists (path) { + return fs.existsSync(path) } -function WriteFile(content, path) { - CreatePath(path); - fs.writeFileSync(path, content); +function CreatePath (path, onlyPath) { + if (FileExists(path)) { return } + + var p = path.split('/') + var currDir = p[0] + for (var i = 1; i < p.length; i++) { + if (currDir != '' && !fs.existsSync(currDir)) { + try { + fs.mkdirSync(currDir) + } catch (e) { console.log('Failed to make ' + currDir + ' directory... ') } + } + currDir += '/' + p[i] + } + if (onlyPath == undefined || onlyPath == false) { fs.writeFileSync(path, '') } else { fs.mkdirSync(path) } } -function WriteFileAsync(content, path) { - CreatePath(path); - fs.writeFile(path, content, function(err) { - if (err) { - logger.Log("Error writing file: " + path + " (sync)", logger.GetColor("redbg")); - } - }); +function WriteFile (content, path) { + CreatePath(path) + fs.writeFileSync(path, content) } -function AppendToFile(data, file) { - CreatePath(file); - fs.appendFile(file, "\n" + data, function(err) { - if (err) - logger.Log("Error writing log file: " + file + " (sync)", logger.GetColor("redbg")); - }); +function WriteFileAsync (content, path) { + CreatePath(path) + fs.writeFile(path, content, function (err) { + if (err) { + logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg')) + } + }) } -function Beep() { - try { - process.stdout.write('\x07'); - } catch (e) { - console.log("error beepin"); - } +function AppendToFile (data, file) { + CreatePath(file) + fs.appendFile(file, '\n' + data, function (err) { + if (err) { logger.Log('Error writing log file: ' + file + ' (sync)', logger.GetColor('redbg')) } + }) } -function WriteBackup() { - try { - WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString()); - } catch (e) { - logger.Log("Error backing up data json file!", logger.GetColor("redbg")); - console.log(e); - } +function Beep () { + try { + process.stdout.write('\x07') + } catch (e) { + console.log('error beepin') + } } -function GetAllFilesFromFolder(dir) { - - var results = []; - - fs.readdirSync(dir).forEach(function(file) { - - file = dir + '/' + file; - var stat = fs.statSync(file); - - if (stat && stat.isDirectory()) { - results = results.concat(_getAllFilesFromFolder(file)); - } else results.push(file); - - }); - - return results; +function WriteBackup () { + try { + WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString()) + } catch (e) { + logger.Log('Error backing up data json file!', logger.GetColor('redbg')) + console.log(e) + } } +function GetAllFilesFromFolder (dir) { + var results = [] + + fs.readdirSync(dir).forEach(function (file) { + file = dir + '/' + file + var stat = fs.statSync(file) + + if (stat && stat.isDirectory()) { + results = results.concat(_getAllFilesFromFolder(file)) + } else results.push(file) + }) + + return results +} From da9c73b5ccc3ddf202a0cdbe88aff2ec3830d09f Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:38:25 +0200 Subject: [PATCH 13/18] stuff module --- modules/main.js | 56 --------------------- modules/stuff.js | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 56 deletions(-) create mode 100644 modules/stuff.js diff --git a/modules/main.js b/modules/main.js index 613c657..ff0aa85 100644 --- a/modules/main.js +++ b/modules/main.js @@ -21,7 +21,6 @@ const express = require('express') const bodyParser = require('body-parser') const busboy = require('connect-busboy') -const fs = require('fs') const app = express() const logger = require('../utils/logger.js') @@ -30,8 +29,6 @@ const logger = require('../utils/logger.js') const stat = require('../utils/stat.js') stat.Load() -const listedFiles = '../public/files' - app.set('view engine', 'ejs') app.use(function (req, res, next) { res.on('finish', function () { @@ -62,59 +59,6 @@ app.get('/', function (req, res) { res.end('henlo') }) -// ----------------------------------------------------------------------------------------------- - -app.get('/stuff*', function (req, res) { - let curr = listedFiles + '/' + req.url.substring('/stuff/'.length, req.url.length).split('?')[0] - let relPath = curr.substring('./public/files'.length, curr.length) - - if (relPath[relPath.length - 1] !== '/') { relPath += '/' } - - let t = relPath.split('/') - let prevDir = '' - for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } - - // curr = curr.replace(/\//g, "/"); - // relPath = relPath.replace(/\//g, "/"); - - logger.LogReq(req) - - if (fs.lstatSync(curr).isDirectory()) { - if (curr[curr.length - 1] !== '/') { curr += '/' } - - let f = [] - - fs.readdirSync(curr).forEach((item) => { - if (item[0] !== '.') { - let res = { name: item } - let stats = fs.statSync(curr + '/' + item) - - let fileSizeInBytes = stats['size'] - res.size = Math.round(fileSizeInBytes / 1000000) - - res.path = relPath - if (res.path[res.path.length - 1] !== '/') { res.path += '/' } - res.path += item - - res.mtime = stats['mtime'].toLocaleString() - - f.push(res) - } - }) - - res.render('folders', { - folders: f, - dirname: relPath, - prevDir - }) - } else { - let fileStream = fs.createReadStream(curr) - fileStream.pipe(res) - } -}) - -// ----------------------------------------------------------------------------------------------- - app.get('*', function (req, res) { res.render('404') res.status(404) diff --git a/modules/stuff.js b/modules/stuff.js new file mode 100644 index 0000000..e2ef269 --- /dev/null +++ b/modules/stuff.js @@ -0,0 +1,124 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +const express = require('express') +const bodyParser = require('body-parser') +const busboy = require('connect-busboy') +const fs = require('fs') +const app = express() + +const logger = require('../utils/logger.js') +// const utils = require('../utils/utils.js') +// const actions = require('../utils/actions.js') +const stat = require('../utils/stat.js') +stat.Load() + +const listedFiles = '../public/files' + +app.set('view engine', 'ejs') +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) +app.use(express.static('public')) +app.use(busboy({ + limits: { + fileSize: 10000 * 1024 * 1024 + } +})) +app.use(bodyParser.json()) +app.use(bodyParser.urlencoded({ + limit: '5mb', + extended: true +})) +app.use(bodyParser.json({ + limit: '5mb' +})) + +// -------------------------------------------------------------- + +app.get('/*', function (req, res) { + let curr = listedFiles + '/' + req.url.substring('/stuff/'.length, req.url.length).split('?')[0] + let relPath = curr.substring('./public/files'.length, curr.length) + + if (relPath[relPath.length - 1] !== '/') { relPath += '/' } + + let t = relPath.split('/') + let prevDir = '' + for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } + + // curr = curr.replace(/\//g, "/"); + // relPath = relPath.replace(/\//g, "/"); + + logger.LogReq(req) + + if (fs.lstatSync(curr).isDirectory()) { + if (curr[curr.length - 1] !== '/') { curr += '/' } + + let f = [] + + fs.readdirSync(curr).forEach((item) => { + if (item[0] !== '.') { + let res = { name: item } + let stats = fs.statSync(curr + '/' + item) + + let fileSizeInBytes = stats['size'] + res.size = Math.round(fileSizeInBytes / 1000000) + + res.path = relPath + if (res.path[res.path.length - 1] !== '/') { res.path += '/' } + res.path += item + + res.mtime = stats['mtime'].toLocaleString() + + f.push(res) + } + }) + + res.render('folders', { + folders: f, + dirname: relPath, + prevDir + }) + } else { + let fileStream = fs.createReadStream(curr) + fileStream.pipe(res) + } +}) + +// ----------------------------------------------------------------------------------------------- + +app.get('*', function (req, res) { + res.render('404') + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); +}) + +app.post('*', function (req, res) { + res.status(404) + // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); +}) + +exports.app = app + +logger.Log('Main module started', logger.GetColor('yellow')) From 0b5141e7d784d840b7eff3361cac3df90f585136 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:46:22 +0200 Subject: [PATCH 14/18] Moved views around a bit --- modules/main.js | 2 +- modules/qmining.js | 14 +++++++------- modules/sio.js | 4 ++-- modules/stuff.js | 4 ++-- views/{ => qmining}/alldata.ejs | 0 views/{ => qmining}/allqr.ejs | 0 views/{ => qmining}/aludni.ejs | 0 views/{ => qmining}/b.ejs | 0 views/{ => qmining}/main.ejs | 0 views/{ => qmining}/man.ejs | 0 views/{ => qmining}/qa.ejs | 0 views/{ => qmining}/uploaded.ejs | 0 views/{ => shared}/404.ejs | 0 views/{ => sio}/uload.ejs | 0 14 files changed, 12 insertions(+), 12 deletions(-) rename views/{ => qmining}/alldata.ejs (100%) rename views/{ => qmining}/allqr.ejs (100%) rename views/{ => qmining}/aludni.ejs (100%) rename views/{ => qmining}/b.ejs (100%) rename views/{ => qmining}/main.ejs (100%) rename views/{ => qmining}/man.ejs (100%) rename views/{ => qmining}/qa.ejs (100%) rename views/{ => qmining}/uploaded.ejs (100%) rename views/{ => shared}/404.ejs (100%) rename views/{ => sio}/uload.ejs (100%) diff --git a/modules/main.js b/modules/main.js index ff0aa85..831e0d5 100644 --- a/modules/main.js +++ b/modules/main.js @@ -60,7 +60,7 @@ app.get('/', function (req, res) { }) app.get('*', function (req, res) { - res.render('404') + res.render('shared/404') res.status(404) // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); }) diff --git a/modules/qmining.js b/modules/qmining.js index 8a1e2ad..1ed14ca 100644 --- a/modules/qmining.js +++ b/modules/qmining.js @@ -68,7 +68,7 @@ app.use(bodyParser.json({ app.get('/', function (req, res) { // req.hostname - res.render('main', { + res.render('qmining/main', { siteurl: siteUrl, qa: actions.ProcessQA() }) @@ -76,7 +76,7 @@ app.get('/', function (req, res) { }) app.get('/manual', function (req, res) { - res.render('man') + res.render('qmining/man') res.end() logger.LogReq(req) }) @@ -102,7 +102,7 @@ app.get('/legacy', function (req, res) { for (let i = 0; i < d.length; i++) { qcount += d.Subjects[i].length } let scount = d.length - res.render('alldata', { + res.render('qmining/alldata', { data: d, scount: scount, qcount: qcount, @@ -121,7 +121,7 @@ app.post('/postfeedback', function (req, res) { app.get('/postfeedback', function (req, res) { // TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already // redirects - res.render('main', { + res.render('qmining/main', { sdata: utils.ReadFile(staticFile) }) }) @@ -150,7 +150,7 @@ app.get('/allqr', function (req, res) { var f = utils.ReadFile(dataFile) var d = actions.LoadJSON(f) - res.render('allqr', { + res.render('qmining/allqr', { d: d.toString().split('\n') }) logger.LogReq(req) @@ -205,13 +205,13 @@ app.route('/fosuploader').post(function (req, res, next) { app.route('/badtestsender').post(function (req, res, next) { UploadFile(req, res, recivedFiles, (fn) => { - res.render('uploaded') + res.render('qmining/uploaded') }) logger.LogReq(req) }) app.get('*', function (req, res) { - res.render('404') + res.render('shared/404') res.status(404) // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); }) diff --git a/modules/sio.js b/modules/sio.js index afc4c1a..0f9c5a2 100644 --- a/modules/sio.js +++ b/modules/sio.js @@ -59,7 +59,7 @@ app.use(bodyParser.json({ // -------------------------------------------------------------- app.get('/', function (req, res) { - res.render('uload') + res.render('sio/uload') res.end() }) @@ -92,7 +92,7 @@ app.route('/fosuploader').post(function (req, res, next) { }) }) app.get('*', function (req, res) { - res.render('404') + res.render('shared/404') res.status(404) // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); }) diff --git a/modules/stuff.js b/modules/stuff.js index e2ef269..9005ff5 100644 --- a/modules/stuff.js +++ b/modules/stuff.js @@ -95,7 +95,7 @@ app.get('/*', function (req, res) { } }) - res.render('folders', { + res.render('stuff/folders', { folders: f, dirname: relPath, prevDir @@ -109,7 +109,7 @@ app.get('/*', function (req, res) { // ----------------------------------------------------------------------------------------------- app.get('*', function (req, res) { - res.render('404') + res.render('shared/404') res.status(404) // utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile); }) diff --git a/views/alldata.ejs b/views/qmining/alldata.ejs similarity index 100% rename from views/alldata.ejs rename to views/qmining/alldata.ejs diff --git a/views/allqr.ejs b/views/qmining/allqr.ejs similarity index 100% rename from views/allqr.ejs rename to views/qmining/allqr.ejs diff --git a/views/aludni.ejs b/views/qmining/aludni.ejs similarity index 100% rename from views/aludni.ejs rename to views/qmining/aludni.ejs diff --git a/views/b.ejs b/views/qmining/b.ejs similarity index 100% rename from views/b.ejs rename to views/qmining/b.ejs diff --git a/views/main.ejs b/views/qmining/main.ejs similarity index 100% rename from views/main.ejs rename to views/qmining/main.ejs diff --git a/views/man.ejs b/views/qmining/man.ejs similarity index 100% rename from views/man.ejs rename to views/qmining/man.ejs diff --git a/views/qa.ejs b/views/qmining/qa.ejs similarity index 100% rename from views/qa.ejs rename to views/qmining/qa.ejs diff --git a/views/uploaded.ejs b/views/qmining/uploaded.ejs similarity index 100% rename from views/uploaded.ejs rename to views/qmining/uploaded.ejs diff --git a/views/404.ejs b/views/shared/404.ejs similarity index 100% rename from views/404.ejs rename to views/shared/404.ejs diff --git a/views/uload.ejs b/views/sio/uload.ejs similarity index 100% rename from views/uload.ejs rename to views/sio/uload.ejs From 2816347cbde2f4e0b88af47c9fc37cc93fc4645f Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 10:47:33 +0200 Subject: [PATCH 15/18] Added floders.ejs --- views/folders.ejs | 96 +++++++++++++++++++++++++++++++++++++++++ views/stuff/folders.ejs | 96 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 views/folders.ejs create mode 100644 views/stuff/folders.ejs diff --git a/views/folders.ejs b/views/folders.ejs new file mode 100644 index 0000000..ea6b70f --- /dev/null +++ b/views/folders.ejs @@ -0,0 +1,96 @@ + + + + + + <%=dirname%> + + + +
+

+ <%=dirname%> +

+
+ > Up one level +

+ + + <% for (var i = 0; i < folders.length; i++) { %> + + + + <% } %> +
+ +
+ + diff --git a/views/stuff/folders.ejs b/views/stuff/folders.ejs new file mode 100644 index 0000000..ea6b70f --- /dev/null +++ b/views/stuff/folders.ejs @@ -0,0 +1,96 @@ + + + + + + <%=dirname%> + + + +
+

+ <%=dirname%> +

+
+ > Up one level +

+ + + <% for (var i = 0; i < folders.length; i++) { %> + + + + <% } %> +
+ +
+ + From e0b9d4e09f78b77a5c91ddff3308786660a13963 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 12:30:02 +0200 Subject: [PATCH 16/18] File path fixes --- modules/sio.js | 2 +- modules/stuff.js | 4 ++-- server.js | 2 ++ utils/actions.js | 12 ++++++------ utils/logger.js | 4 ++-- utils/stat.js | 4 ++-- utils/utils.js | 8 ++++---- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/modules/sio.js b/modules/sio.js index 0f9c5a2..bd6fceb 100644 --- a/modules/sio.js +++ b/modules/sio.js @@ -31,7 +31,7 @@ const utils = require('../utils/utils.js') const stat = require('../utils/stat.js') stat.Load() -const uloadFiles = '../public/f' +const uloadFiles = './public/f' app.set('view engine', 'ejs') app.use(function (req, res, next) { diff --git a/modules/stuff.js b/modules/stuff.js index 9005ff5..1321594 100644 --- a/modules/stuff.js +++ b/modules/stuff.js @@ -30,7 +30,7 @@ const logger = require('../utils/logger.js') const stat = require('../utils/stat.js') stat.Load() -const listedFiles = '../public/files' +const listedFiles = './public/files' app.set('view engine', 'ejs') app.use(function (req, res, next) { @@ -121,4 +121,4 @@ app.post('*', function (req, res) { exports.app = app -logger.Log('Main module started', logger.GetColor('yellow')) +logger.Log('Stuff module started', logger.GetColor('yellow')) diff --git a/server.js b/server.js index 79bf80f..5b61971 100644 --- a/server.js +++ b/server.js @@ -25,6 +25,7 @@ const logger = require('./utils/logger.js') const qmining = require('./modules/qmining.js').app const main = require('./modules/main.js').app const sio = require('./modules/sio.js').app +const stuff = require('./modules/stuff.js').app const port = 8080 @@ -54,6 +55,7 @@ const port = 8080 express() .use(vhost('qmining.frylabs.net', qmining)) .use(vhost('sio.frylabs.net', sio)) + .use(vhost('stuff.frylabs.net', stuff)) .use(vhost('frylabs.net', main)) .use(vhost('qmining.tk', qmining)) .listen(port) diff --git a/utils/actions.js b/utils/actions.js index cb9feb8..e691c7c 100644 --- a/utils/actions.js +++ b/utils/actions.js @@ -25,12 +25,12 @@ module.exports = { ProcessQA: ProcessQA } -const staticFile = '../public/data/static' -const dataFile = '../public/data.json' -const recDataFile = '../stats/recdata' -const versionFile = '../public/version' -const motdFile = '../public/motd' -const qaFile = '../public/qa' +const staticFile = './public/data/static' +const dataFile = './public/data.json' +const recDataFile = './stats/recdata' +const versionFile = './public/version' +const motdFile = './public/motd' +const qaFile = './public/qa' var logger = require('../utils/logger.js') var utils = require('../utils/utils.js') diff --git a/utils/logger.js b/utils/logger.js index b9ff95f..e8f7bc2 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -28,8 +28,8 @@ module.exports = { const DELIM = '|' var utils = require('../utils/utils.js') -const nlogFile = '../stats/nlogs' -const locLogFile = '../stats/logs' +const nlogFile = './stats/nlogs' +const locLogFile = './stats/logs' const logFile = '/nlogs/nlogs' const allLogFile = '/nlogs/log' diff --git a/utils/stat.js b/utils/stat.js index 3d2d91c..2539ec7 100644 --- a/utils/stat.js +++ b/utils/stat.js @@ -26,8 +26,8 @@ module.exports = { var utils = require('../utils/utils.js') var logger = require('../utils/logger.js') -const statFile = '../stats/stats' -const vStatFile = '../stats/vstats' +const statFile = 'stats/stats' +const vStatFile = 'stats/vstats' const writeInterval = 10 var data = {} diff --git a/utils/utils.js b/utils/utils.js index 8d2d2d5..86f5e78 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -14,10 +14,10 @@ var fs = require('fs') var logger = require('../utils/logger.js') -const recievedFile = '../stats/recieved' -const manFile = '../public/man.html' -const logFile = '../stats/logs' -const dataFile = '../public/data.json' +const recievedFile = './stats/recieved' +const manFile = './public/man.html' +const logFile = './stats/logs' +const dataFile = './public/data.json' function ReadFile (name) { if (!FileExists(name)) { throw 'No such file: ' + name } From 4b6814c5fe2b823cbe1a4937b010a2bed65241f3 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 12:35:25 +0200 Subject: [PATCH 17/18] folders.js move --- views/folders.ejs | 96 ----------------------------------------------- 1 file changed, 96 deletions(-) delete mode 100644 views/folders.ejs diff --git a/views/folders.ejs b/views/folders.ejs deleted file mode 100644 index ea6b70f..0000000 --- a/views/folders.ejs +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - <%=dirname%> - - - -
-

- <%=dirname%> -

-
- > Up one level -

- - - <% for (var i = 0; i < folders.length; i++) { %> - - - - <% } %> -
- -
- - From f9c6ff3c49afc6aa8a01869e5a4fd553cfa3589a Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Wed, 14 Aug 2019 13:13:06 +0200 Subject: [PATCH 18/18] Readme update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c7de34..27ee895 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Install: Requires node.js. -`npm install express connect-busboy ejs express-layout querystring express` +`npm install` then