Prettied all js in src/

This commit is contained in:
mrfry 2020-10-01 14:44:24 +02:00
parent 3f081d8dff
commit ee0f0a9f3b
17 changed files with 1012 additions and 688 deletions

View file

@ -1,68 +1,42 @@
{ {
"dataEditor": { "dataEditor": {
"path": "./modules/dataEditor/dataEditor.js", "path": "./modules/dataEditor/dataEditor.js",
"publicdirs": [ "publicdirs": ["qminingPublic/"],
"qminingPublic/"
],
"nextdir": "modules/dataEditor/public/", "nextdir": "modules/dataEditor/public/",
"name": "dataeditor", "name": "dataeditor",
"urls": [ "urls": ["dataeditor.frylabs.net"],
"dataeditor.frylabs.net"
],
"isNextJs": true "isNextJs": true
}, },
"qmining": { "qmining": {
"path": "./modules/qmining/qmining.js", "path": "./modules/qmining/qmining.js",
"publicdirs": [ "publicdirs": ["qminingPublic/"],
"qminingPublic/"
],
"nextdir": "modules/qmining/public/", "nextdir": "modules/qmining/public/",
"name": "qmining", "name": "qmining",
"urls": [ "urls": ["qmining.frylabs.net"],
"qmining.frylabs.net"
],
"isNextJs": true "isNextJs": true
}, },
"api": { "api": {
"path": "./modules/api/api.js", "path": "./modules/api/api.js",
"publicdirs": [ "publicdirs": ["qminingPublic/"],
"qminingPublic/"
],
"name": "api", "name": "api",
"urls": [ "urls": ["api.frylabs.net", "localhost"]
"api.frylabs.net",
"localhost"
]
}, },
"main": { "main": {
"path": "./modules/main/main.js", "path": "./modules/main/main.js",
"publicdirs": [ "publicdirs": ["public/"],
"public/"
],
"name": "main", "name": "main",
"urls": [ "urls": ["frylabs.net", "www.frylabs.net"]
"frylabs.net",
"www.frylabs.net"
]
}, },
"sio": { "sio": {
"path": "./modules/sio/sio.js", "path": "./modules/sio/sio.js",
"publicdirs": [ "publicdirs": ["sioPublic/"],
"sioPublic/"
],
"name": "sio", "name": "sio",
"urls": [ "urls": ["sio.frylabs.net"]
"sio.frylabs.net"
]
}, },
"stuff": { "stuff": {
"path": "./modules/stuff/stuff.js", "path": "./modules/stuff/stuff.js",
"publicdirs": [ "publicdirs": ["stuffPublic/"],
"stuffPublic/"
],
"name": "stuff", "name": "stuff",
"urls": [ "urls": ["stuff.frylabs.net"]
"stuff.frylabs.net"
]
} }
} }

View file

@ -54,7 +54,7 @@ let userDB
let url // eslint-disable-line let url // eslint-disable-line
let publicdirs = [] let publicdirs = []
function GetApp () { function GetApp() {
const p = publicdirs[0] const p = publicdirs[0]
if (!p) { if (!p) {
throw new Error(`No public dir! ( API )`) throw new Error(`No public dir! ( API )`)
@ -67,19 +67,21 @@ function GetApp () {
const motdFile = p + 'motd' const motdFile = p + 'motd'
const versionFile = p + 'version' const versionFile = p + 'version'
app.use(bodyParser.urlencoded({ app.use(
bodyParser.urlencoded({
limit: '10mb', limit: '10mb',
extended: true extended: true,
})) })
app.use(bodyParser.json({ )
limit: '10mb' app.use(
})) bodyParser.json({
limit: '10mb',
})
)
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
app.set('views', [ app.set('views', ['./modules/api/views', './sharedViews'])
'./modules/api/views', app.use(
'./sharedViews' auth({
])
app.use(auth({
userDB: userDB, userDB: userDB,
jsonResponse: true, jsonResponse: true,
exceptions: [ exceptions: [
@ -89,40 +91,43 @@ function GetApp () {
'/postfeedbackfile', '/postfeedbackfile',
'/postfeedback', '/postfeedback',
'/fosuploader', '/fosuploader',
'/badtestsender' '/badtestsender',
] ],
})) })
)
publicdirs.forEach((pdir) => { publicdirs.forEach((pdir) => {
logger.Log(`Using public dir: ${pdir}`) logger.Log(`Using public dir: ${pdir}`)
app.use(express.static(pdir)) app.use(express.static(pdir))
}) })
app.use(busboy({ app.use(
busboy({
limits: { limits: {
fileSize: 50000 * 1024 * 1024 fileSize: 50000 * 1024 * 1024,
} },
})) })
)
var data = actions.LoadJSON(dataFile) var data = actions.LoadJSON(dataFile)
var version = '' var version = ''
var motd = '' var motd = ''
var testUsers = [] var testUsers = []
function LoadVersion () { function LoadVersion() {
version = utils.ReadFile(versionFile) version = utils.ReadFile(versionFile)
} }
function LoadMOTD () { function LoadMOTD() {
motd = utils.ReadFile(motdFile) motd = utils.ReadFile(motdFile)
} }
function LoadTestUsers () { function LoadTestUsers() {
testUsers = utils.ReadJSON(testUsersFile) testUsers = utils.ReadJSON(testUsersFile)
if (testUsers) { if (testUsers) {
testUsers = testUsers.userIds testUsers = testUsers.userIds
} }
} }
function Load () { function Load() {
utils.WatchFile(motdFile, (newData) => { utils.WatchFile(motdFile, (newData) => {
logger.Log(`Motd changed: ${newData.replace(/\/n/g, '')}`) logger.Log(`Motd changed: ${newData.replace(/\/n/g, '')}`)
LoadMOTD() LoadMOTD()
@ -153,7 +158,7 @@ function GetApp () {
if (!key || !val) { if (!key || !val) {
res.render('votethank', { res.render('votethank', {
results: 'error', results: 'error',
msg: 'no key or val query param!' msg: 'no key or val query param!',
}) })
return return
} }
@ -162,17 +167,23 @@ function GetApp () {
if (utils.FileExists(quickVotes)) { if (utils.FileExists(quickVotes)) {
votes = utils.ReadJSON(quickVotes) votes = utils.ReadJSON(quickVotes)
} else { } else {
logger.Log(`No such vote "${key}", and quickVotes.json is missing ( #${user.id}: ${key}-${val} )`, logger.GetColor('blue')) logger.Log(
`No such vote "${key}", and quickVotes.json is missing ( #${user.id}: ${key}-${val} )`,
logger.GetColor('blue')
)
res.render('votethank', { res.render('votethank', {
result: 'no such pool' result: 'no such pool',
}) })
return return
} }
if (!votes.voteNames.includes(key)) { if (!votes.voteNames.includes(key)) {
logger.Log(`No such vote "${key}" ( #${user.id}: ${key}-${val} )`, logger.GetColor('blue')) logger.Log(
`No such vote "${key}" ( #${user.id}: ${key}-${val} )`,
logger.GetColor('blue')
)
res.render('votethank', { res.render('votethank', {
result: 'no such pool' result: 'no such pool',
}) })
return return
} }
@ -181,7 +192,7 @@ function GetApp () {
let voteData = { let voteData = {
votes: {}, votes: {},
users: [] users: [],
} }
if (utils.FileExists(voteFile)) { if (utils.FileExists(voteFile)) {
@ -198,16 +209,22 @@ function GetApp () {
} }
voteData.users.push(user.id) voteData.users.push(user.id)
logger.Log(`Vote from #${user.id}: ${key}: ${val}`, logger.GetColor('blue')) logger.Log(
`Vote from #${user.id}: ${key}: ${val}`,
logger.GetColor('blue')
)
res.render('votethank', { res.render('votethank', {
result: 'success', result: 'success',
msg: 'vote added' msg: 'vote added',
}) })
} else { } else {
logger.Log(`#${user.id} already voted for: ${key}: ${val}`, logger.GetColor('blue')) logger.Log(
`#${user.id} already voted for: ${key}: ${val}`,
logger.GetColor('blue')
)
res.render('votethank', { res.render('votethank', {
result: 'already voted', result: 'already voted',
msg: 'already voted' msg: 'already voted',
}) })
} }
@ -226,11 +243,11 @@ function GetApp () {
requestedPWS: user.pwRequestCount, requestedPWS: user.pwRequestCount,
maxPWCount: maxPWCount, maxPWCount: maxPWCount,
// daysAfterUserGetsPWs: daysAfterUserGetsPWs, // daysAfterUserGetsPWs: daysAfterUserGetsPWs,
addPWPerDay: addPWPerDay addPWPerDay: addPWPerDay,
}) })
}) })
app.post('/getpw', function (req, res) { app.post('/getpw', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
const requestingUser = req.session.user const requestingUser = req.session.user
@ -238,63 +255,83 @@ function GetApp () {
if (requestingUser.avaiblePWRequests <= 0) { if (requestingUser.avaiblePWRequests <= 0) {
res.json({ res.json({
result: 'error', result: 'error',
msg: 'Too many passwords requested or cant request password yet, try later' msg:
'Too many passwords requested or cant request password yet, try later',
}) })
logger.Log(`User #${requestingUser.id} requested too much passwords`, logger.GetColor('cyan')) logger.Log(
`User #${requestingUser.id} requested too much passwords`,
logger.GetColor('cyan')
)
return return
} }
dbtools.Update(userDB, 'users', { dbtools.Update(
userDB,
'users',
{
avaiblePWRequests: requestingUser.avaiblePWRequests - 1, avaiblePWRequests: requestingUser.avaiblePWRequests - 1,
pwRequestCount: requestingUser.pwRequestCount + 1 pwRequestCount: requestingUser.pwRequestCount + 1,
}, { },
id: requestingUser.id {
}) id: requestingUser.id,
}
)
const pw = uuidv4() const pw = uuidv4()
const insertRes = dbtools.Insert(userDB, 'users', { const insertRes = dbtools.Insert(userDB, 'users', {
pw: pw, pw: pw,
avaiblePWRequests: 0, avaiblePWRequests: 0,
created: utils.GetDateString() created: utils.GetDateString(),
}) })
logger.Log(`User #${requestingUser.id} created new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan')) logger.Log(
`User #${requestingUser.id} created new user #${insertRes.lastInsertRowid}`,
logger.GetColor('cyan')
)
res.json({ res.json({
result: 'success', result: 'success',
pw: pw, pw: pw,
requestedPWS: requestingUser.pwRequestCount + 1, requestedPWS: requestingUser.pwRequestCount + 1,
remaining: requestingUser.avaiblePWRequests - 1 remaining: requestingUser.avaiblePWRequests - 1,
}) })
}) })
app.post('/getveteranpw', function (req, res) { app.post('/getveteranpw', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
const tries = dbtools.Select(userDB, 'veteranPWRequests', { const tries = dbtools.Select(userDB, 'veteranPWRequests', {
ip: ip ip: ip,
})[0] })[0]
if (tries) { if (tries) {
if (tries.count > maxVeteranPwGetCount) { if (tries.count > maxVeteranPwGetCount) {
res.json({ res.json({
result: 'error', result: 'error',
msg: 'Too many tries from this IP' msg: 'Too many tries from this IP',
}) })
logger.Log(`Too many veteran PW requests from ${ip}!`, logger.GetColor('cyan')) logger.Log(
`Too many veteran PW requests from ${ip}!`,
logger.GetColor('cyan')
)
return return
} else { } else {
dbtools.Update(userDB, 'veteranPWRequests', { dbtools.Update(
userDB,
'veteranPWRequests',
{
count: tries.count + 1, count: tries.count + 1,
lastDate: utils.GetDateString() lastDate: utils.GetDateString(),
}, { },
id: tries.id {
}) id: tries.id,
}
)
} }
} else { } else {
dbtools.Insert(userDB, 'veteranPWRequests', { dbtools.Insert(userDB, 'veteranPWRequests', {
ip: ip, ip: ip,
lastDate: utils.GetDateString() lastDate: utils.GetDateString(),
}) })
} }
@ -303,41 +340,55 @@ function GetApp () {
if (!oldUserID) { if (!oldUserID) {
res.json({ res.json({
result: 'error', result: 'error',
msg: 'No Client ID recieved' msg: 'No Client ID recieved',
}) })
logger.Log(`No client ID recieved`, logger.GetColor('cyan')) logger.Log(`No client ID recieved`, logger.GetColor('cyan'))
return return
} }
const user = dbtools.Select(userDB, 'users', { const user = dbtools.Select(userDB, 'users', {
oldCID: oldUserID oldCID: oldUserID,
})[0] })[0]
if (user) { if (user) {
if (user.pwGotFromCID === 0) { if (user.pwGotFromCID === 0) {
logger.Log(`Sent password to veteran user #${user.id}`, logger.GetColor('cyan')) logger.Log(
dbtools.Update(userDB, 'users', { `Sent password to veteran user #${user.id}`,
pwGotFromCID: 1 logger.GetColor('cyan')
}, { )
id: user.id dbtools.Update(
}) userDB,
'users',
{
pwGotFromCID: 1,
},
{
id: user.id,
}
)
res.json({ res.json({
result: 'success', result: 'success',
pw: user.pw pw: user.pw,
}) })
} else { } else {
logger.Log(`Veteran user #${user.id} already requested password`, logger.GetColor('cyan')) logger.Log(
`Veteran user #${user.id} already requested password`,
logger.GetColor('cyan')
)
res.json({ res.json({
result: 'error', result: 'error',
msg: 'Password already requested' msg: 'Password already requested',
}) })
} }
} else { } else {
logger.Log(`Invalid password request with CID: ${oldUserID}`, logger.GetColor('cyan')) logger.Log(
`Invalid password request with CID: ${oldUserID}`,
logger.GetColor('cyan')
)
res.json({ res.json({
result: 'error', result: 'error',
msg: 'No such Client ID' msg: 'No such Client ID',
}) })
} }
}) })
@ -349,7 +400,7 @@ function GetApp () {
const isScript = req.body.script const isScript = req.body.script
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
const user = dbtools.Select(userDB, 'users', { const user = dbtools.Select(userDB, 'users', {
pw: pw pw: pw,
})[0] })[0]
if (user) { if (user) {
@ -358,59 +409,83 @@ function GetApp () {
// FIXME: Users now can only log in in one session, this might be too strict. // FIXME: Users now can only log in in one session, this might be too strict.
const existingSessions = dbtools.Select(userDB, 'sessions', { const existingSessions = dbtools.Select(userDB, 'sessions', {
userID: user.id, userID: user.id,
isScript: isScript ? 1 : 0 isScript: isScript ? 1 : 0,
}) })
if (existingSessions.length > 0) { if (existingSessions.length > 0) {
logger.Log(`Multiple ${isScript ? 'script' : 'website'} sessions ( ${existingSessions.length} ) for #${user.id}, deleting olds`, logger.GetColor('cyan')) logger.Log(
`Multiple ${isScript ? 'script' : 'website'} sessions ( ${
existingSessions.length
} ) for #${user.id}, deleting olds`,
logger.GetColor('cyan')
)
existingSessions.forEach((sess) => { existingSessions.forEach((sess) => {
dbtools.Delete(userDB, 'sessions', { dbtools.Delete(userDB, 'sessions', {
id: sess.id, id: sess.id,
isScript: isScript ? 1 : 0 isScript: isScript ? 1 : 0,
}) })
}) })
} }
dbtools.Update(userDB, 'users', { dbtools.Update(
userDB,
'users',
{
loginCount: user.loginCount + 1, loginCount: user.loginCount + 1,
lastIP: ip, lastIP: ip,
lastLogin: utils.GetDateString() lastLogin: utils.GetDateString(),
}, { },
id: user.id {
}) id: user.id,
}
)
dbtools.Insert(userDB, 'sessions', { dbtools.Insert(userDB, 'sessions', {
id: sessionID, id: sessionID,
ip: ip, ip: ip,
userID: user.id, userID: user.id,
isScript: isScript ? 1 : 0, isScript: isScript ? 1 : 0,
createDate: utils.GetDateString() createDate: utils.GetDateString(),
}) })
// https://www.npmjs.com/package/cookie // https://www.npmjs.com/package/cookie
// TODO: cookie age // TODO: cookie age
res.cookie('sessionID', sessionID, { res.cookie('sessionID', sessionID, {
domain: '.frylabs.net', // TODO: use url. url: "https://api.frylabs.net" domain: '.frylabs.net', // TODO: use url. url: "https://api.frylabs.net"
expires: new Date(new Date().getTime() + (10 * 365 * 24 * 60 * 60 * 1000)), expires: new Date(
new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000
),
sameSite: 'none', sameSite: 'none',
secure: true secure: true,
}) })
res.cookie('sessionID', sessionID, { res.cookie('sessionID', sessionID, {
expires: new Date(new Date().getTime() + (10 * 365 * 24 * 60 * 60 * 1000)), expires: new Date(
new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000
),
sameSite: 'none', sameSite: 'none',
secure: true secure: true,
}) })
res.json({ res.json({
result: 'success', result: 'success',
msg: 'you are now logged in' msg: 'you are now logged in',
}) })
logger.Log(`Successfull login to ${isScript ? 'script' : 'website'} with user ID: #${user.id}`, logger.GetColor('cyan')) logger.Log(
`Successfull login to ${
isScript ? 'script' : 'website'
} with user ID: #${user.id}`,
logger.GetColor('cyan')
)
} else { } else {
logger.Log(`Login attempt with invalid pw: ${pw} to ${isScript ? 'script' : 'website'}${cid ? ', CID:' + cid : ''}`, logger.GetColor('cyan')) logger.Log(
`Login attempt with invalid pw: ${pw} to ${
isScript ? 'script' : 'website'
}${cid ? ', CID:' + cid : ''}`,
logger.GetColor('cyan')
)
res.json({ res.json({
result: 'error', result: 'error',
msg: 'Invalid password' msg: 'Invalid password',
}) })
} }
}) })
@ -421,22 +496,22 @@ function GetApp () {
// removing session from db // removing session from db
dbtools.Delete(userDB, 'sessions', { dbtools.Delete(userDB, 'sessions', {
id: sessionID id: sessionID,
}) })
// TODO: remove old sessions every once in a while // TODO: remove old sessions every once in a while
res.clearCookie('sessionID').json({ res.clearCookie('sessionID').json({
result: 'success' result: 'success',
}) })
}) })
// -------------------------------------------------------------- // --------------------------------------------------------------
app.get('/', function (req, res) { app.get('/', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
res.redirect('https://www.youtube.com/watch?v=ieqGJgqiXFk') res.redirect('https://www.youtube.com/watch?v=ieqGJgqiXFk')
}) })
app.post('/postfeedbackfile', function (req, res) { app.post('/postfeedbackfile', function(req, res) {
UploadFile(req, res, uloadFiles, (fn) => { UploadFile(req, res, uloadFiles, (fn) => {
res.json({ success: true }) res.json({ success: true })
}) })
@ -445,43 +520,66 @@ function GetApp () {
logger.Log('New feedback file', logger.GetColor('bluebg'), true) logger.Log('New feedback file', logger.GetColor('bluebg'), true)
}) })
app.post('/postfeedback', function (req, res) { app.post('/postfeedback', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
if (req.body.fromLogin) { if (req.body.fromLogin) {
logger.Log('New feedback message from Login page', logger.GetColor('bluebg'), true) logger.Log(
'New feedback message from Login page',
logger.GetColor('bluebg'),
true
)
} else { } else {
logger.Log('New feedback message from feedback page', logger.GetColor('bluebg'), true) logger.Log(
'New feedback message from feedback page',
logger.GetColor('bluebg'),
true
)
} }
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
const user = req.session.user const user = req.session.user
utils.AppendToFile(utils.GetDateString() + ':\n' + JSON.stringify({ utils.AppendToFile(
utils.GetDateString() +
':\n' +
JSON.stringify({
...req.body, ...req.body,
userID: user ? user.id : 'no user', userID: user ? user.id : 'no user',
ip: ip ip: ip,
}), msgFile) }),
msgFile
)
res.json({ success: true }) res.json({ success: true })
}) })
function UploadFile (req, res, path, next) { function UploadFile(req, res, path, next) {
try { try {
var fstream var fstream
req.pipe(req.busboy) req.pipe(req.busboy)
req.busboy.on('file', function (fieldname, file, filename) { req.busboy.on('file', function(fieldname, file, filename) {
logger.Log('Uploading: ' + filename, logger.GetColor('blue')) logger.Log('Uploading: ' + filename, logger.GetColor('blue'))
utils.CreatePath(path, true) utils.CreatePath(path, true)
let d = new Date() let d = new Date()
let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename let fn =
d.getHours() +
'' +
d.getMinutes() +
'' +
d.getSeconds() +
'_' +
filename
fstream = fs.createWriteStream(path + '/' + fn) fstream = fs.createWriteStream(path + '/' + fn)
file.pipe(fstream) file.pipe(fstream)
fstream.on('close', function () { fstream.on('close', function() {
logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) logger.Log(
'Upload Finished of ' + path + '/' + fn,
logger.GetColor('blue')
)
next(fn) next(fn)
}) })
fstream.on('error', function (err) { fstream.on('error', function(err) {
console.log(err) console.log(err)
res.end('something bad happened :s') res.end('something bad happened :s')
}) })
@ -492,20 +590,20 @@ function GetApp () {
} }
} }
app.route('/fosuploader').post(function (req, res, next) { app.route('/fosuploader').post(function(req, res, next) {
UploadFile(req, res, uloadFiles, (fn) => { UploadFile(req, res, uloadFiles, (fn) => {
res.redirect('/f/' + fn) res.redirect('/f/' + fn)
}) })
}) })
app.route('/badtestsender').post(function (req, res, next) { app.route('/badtestsender').post(function(req, res, next) {
UploadFile(req, res, recivedFiles, (fn) => { UploadFile(req, res, recivedFiles, (fn) => {
res.redirect('back') res.redirect('back')
}) })
logger.LogReq(req) logger.LogReq(req)
}) })
app.get('/allqr.txt', function (req, res) { app.get('/allqr.txt', function(req, res) {
res.set('Content-Type', 'text/plain') res.set('Content-Type', 'text/plain')
res.send(data.toString()) res.send(data.toString())
res.end() res.end()
@ -528,7 +626,7 @@ function GetApp () {
const respStatuses = { const respStatuses = {
invalidPass: 'invalidPass', invalidPass: 'invalidPass',
ok: 'ok', ok: 'ok',
error: 'error' error: 'error',
} }
logger.LogReq(req) logger.LogReq(req)
@ -547,19 +645,53 @@ function GetApp () {
logger.Log(`PWD: ${password}`, logger.GetColor('bluebg')) logger.Log(`PWD: ${password}`, logger.GetColor('bluebg'))
// returning if user password is not ok // returning if user password is not ok
if (!user) { if (!user) {
logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red')) logger.Log(
utils.AppendToFile(utils.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) `Data upload: invalid password ${password}`,
logger.GetColor('red')
)
utils.AppendToFile(
utils.GetDateString() +
'\n' +
password +
'(FAILED PASSWORD)\n' +
JSON.stringify(editedQuestions) +
'\n\n',
dataEditsLog
)
res.json({ status: respStatuses.invalidPass }) res.json({ status: respStatuses.invalidPass })
return return
} }
logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg')) logger.Log(
logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg')) `Password accepted for ${user.name}`,
logger.GetColor('bluebg')
)
logger.Log(
`Old Subjects/Questions: ${initialCount.subjectCount} / ${
initialCount.questionCount
} | New: ${count.subjectCount} / ${
count.questionCount
} | Edited question count: ${Object.keys(editedQuestions).length}`,
logger.GetColor('bluebg')
)
// saving detailed editedCount // saving detailed editedCount
utils.AppendToFile(utils.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) utils.AppendToFile(
utils.GetDateString() +
'\n' +
JSON.stringify(user) +
'\n' +
JSON.stringify(editedQuestions) +
'\n\n',
dataEditsLog
)
// making backup // making backup
utils.CopyFile('./' + dataFile, `./qminingPublic/backs/data_before_${user.name}_${utils.GetDateString().replace(/ /g, '_')}`) // TODO: rewrite to dinamyc public!!! utils.CopyFile(
'./' + dataFile,
`./qminingPublic/backs/data_before_${
user.name
}_${utils.GetDateString().replace(/ /g, '_')}`
) // TODO: rewrite to dinamyc public!!!
logger.Log('Backup made') logger.Log('Backup made')
// writing data // writing data
utils.WriteFile(JSON.stringify(newData), dataFile) utils.WriteFile(JSON.stringify(newData), dataFile)
@ -571,7 +703,7 @@ function GetApp () {
res.json({ res.json({
status: respStatuses.ok, status: respStatuses.ok,
user: user.name user: user.name,
}) })
logger.Log('Data updating done!', logger.GetColor('bluebg')) logger.Log('Data updating done!', logger.GetColor('bluebg'))
} catch (e) { } catch (e) {
@ -581,7 +713,7 @@ function GetApp () {
} }
}) })
app.post('/isAdding', function (req, res) { app.post('/isAdding', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
const user = req.session.user const user = req.session.user
@ -590,27 +722,29 @@ function GetApp () {
// automatically saves to dataFile every n write // automatically saves to dataFile every n write
// FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future // FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future
actions.ProcessIncomingRequest( actions
.ProcessIncomingRequest(
req.body.datatoadd || req.body, req.body.datatoadd || req.body,
data, data,
{ motd, version }, { motd, version },
dryRun dryRun
).then((r) => { )
.then((r) => {
res.json({ res.json({
success: r !== -1, success: r !== -1,
newQuestions: r newQuestions: r,
}) })
}) })
}) })
app.get('/ask', function (req, res) { app.get('/ask', function(req, res) {
if (Object.keys(req.query).length === 0) { if (Object.keys(req.query).length === 0) {
logger.DebugLog(`No query params`, 'ask', 1) logger.DebugLog(`No query params`, 'ask', 1)
res.json({ res.json({
message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`, message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`,
result: [], result: [],
recievedData: JSON.stringify(req.query), recievedData: JSON.stringify(req.query),
success: false success: false,
}) })
} else { } else {
if (req.query.q && req.query.data) { if (req.query.q && req.query.data) {
@ -620,13 +754,16 @@ function GetApp () {
try { try {
recData = JSON.parse(req.query.data) recData = JSON.parse(req.query.data)
} catch (e) { } catch (e) {
logger.Log(`Unable to parse recieved question data! '${req.query.data}'`, logger.GetColor('redbg')) logger.Log(
`Unable to parse recieved question data! '${req.query.data}'`,
logger.GetColor('redbg')
)
} }
let r = data.Search(question, subj, recData) let r = data.Search(question, subj, recData)
res.json({ res.json({
result: r, result: r,
success: true success: true,
}) })
logger.DebugLog(`Question result length: ${r.length}`, 'ask', 1) logger.DebugLog(`Question result length: ${r.length}`, 'ask', 1)
logger.DebugLog(r, 'ask', 2) logger.DebugLog(r, 'ask', 2)
@ -636,35 +773,35 @@ function GetApp () {
message: `Invalid question :(`, message: `Invalid question :(`,
result: [], result: [],
recievedData: JSON.stringify(req.query), recievedData: JSON.stringify(req.query),
success: false success: false,
}) })
} }
} }
}) })
function getSimplreRes () { function getSimplreRes() {
return { return {
subjects: data.length, subjects: data.length,
questions: data.Subjects.reduce((acc, subj) => { questions: data.Subjects.reduce((acc, subj) => {
return acc + subj.length return acc + subj.length
}, 0) }, 0),
} }
} }
function getDetailedRes () { function getDetailedRes() {
return data.Subjects.map((subj) => { return data.Subjects.map((subj) => {
return { return {
name: subj.Name, name: subj.Name,
count: subj.length count: subj.length,
} }
}) })
} }
app.get('/datacount', function (req, res) { app.get('/datacount', function(req, res) {
logger.LogReq(req) logger.LogReq(req)
if (req.query.detailed === 'all') { if (req.query.detailed === 'all') {
res.json({ res.json({
detailed: getDetailedRes(), detailed: getDetailedRes(),
simple: getSimplreRes() simple: getSimplreRes(),
}) })
} else if (req.query.detailed) { } else if (req.query.detailed) {
res.json(getDetailedRes()) res.json(getDetailedRes())
@ -673,12 +810,12 @@ function GetApp () {
} }
}) })
app.get('/infos', function (req, res) { app.get('/infos', function(req, res) {
const user = req.session.user const user = req.session.user
let result = { let result = {
result: 'success', result: 'success',
uid: user.id uid: user.id,
} }
if (req.query.subjinfo) { if (req.query.subjinfo) {
result.subjinfo = getSimplreRes() result.subjinfo = getSimplreRes()
@ -694,30 +831,38 @@ function GetApp () {
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
app.get('*', function (req, res) { app.get('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
app.post('*', function (req, res) { app.post('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
function ExportDailyDataCount () { function ExportDailyDataCount() {
logger.Log('Saving daily data count ...') logger.Log('Saving daily data count ...')
utils.AppendToFile(JSON.stringify({ utils.AppendToFile(
JSON.stringify({
date: utils.GetDateString(), date: utils.GetDateString(),
subjectCount: data.Subjects.length, subjectCount: data.Subjects.length,
questionCOunt: data.Subjects.reduce((acc, subj) => { questionCOunt: data.Subjects.reduce((acc, subj) => {
return acc + subj.Questions.length return acc + subj.Questions.length
}, 0), }, 0),
userCount: dbtools.TableInfo(userDB, 'users').dataCount userCount: dbtools.TableInfo(userDB, 'users').dataCount,
}), dailyDataCountFile) }),
dailyDataCountFile
)
} }
function BackupDB () { function BackupDB() {
logger.Log('Backing up auth DB ...') logger.Log('Backing up auth DB ...')
utils.CreatePath(usersDbBackupPath, true) utils.CreatePath(usersDbBackupPath, true)
userDB.backup(`${usersDbBackupPath}/users.${utils.GetDateString().replace(/ /g, '_')}.db`) userDB
.backup(
`${usersDbBackupPath}/users.${utils
.GetDateString()
.replace(/ /g, '_')}.db`
)
.then(() => { .then(() => {
logger.Log('Auth DB backup complete!') logger.Log('Auth DB backup complete!')
}) })
@ -727,7 +872,7 @@ function GetApp () {
}) })
} }
function IncrementAvaiblePWs () { function IncrementAvaiblePWs() {
// FIXME: check this if this is legit and works // FIXME: check this if this is legit and works
logger.Log('Incrementing avaible PW-s ...') logger.Log('Incrementing avaible PW-s ...')
const users = dbtools.SelectAll(userDB, 'users') const users = dbtools.SelectAll(userDB, 'users')
@ -749,17 +894,27 @@ function GetApp () {
// } // }
if (dayDiff % addPWPerDay === 0) { if (dayDiff % addPWPerDay === 0) {
logger.Log(`Incrementing avaible PW-s for user #${u.id}: ${u.avaiblePWRequests} -> ${u.avaiblePWRequests + 1}`, logger.GetColor('cyan')) logger.Log(
dbtools.Update(userDB, 'users', { `Incrementing avaible PW-s for user #${u.id}: ${
avaiblePWRequests: u.avaiblePWRequests + 1 u.avaiblePWRequests
}, { } -> ${u.avaiblePWRequests + 1}`,
id: u.id logger.GetColor('cyan')
}) )
dbtools.Update(
userDB,
'users',
{
avaiblePWRequests: u.avaiblePWRequests + 1,
},
{
id: u.id,
}
)
} }
}) })
} }
function DailyAction () { function DailyAction() {
ExportDailyDataCount() ExportDailyDataCount()
BackupDB() BackupDB()
IncrementAvaiblePWs() IncrementAvaiblePWs()
@ -767,7 +922,7 @@ function GetApp () {
return { return {
dailyAction: DailyAction, dailyAction: DailyAction,
app: app app: app,
} }
} }

View file

@ -52,13 +52,9 @@
"sessions": { "sessions": {
"foreignKey": [ "foreignKey": [
{ {
"keysFrom": [ "keysFrom": ["userID"],
"userID"
],
"table": "users", "table": "users",
"keysTo": [ "keysTo": ["id"]
"id"
]
} }
], ],
"tableStruct": { "tableStruct": {
@ -91,13 +87,9 @@
"accesses": { "accesses": {
"foreignKey": [ "foreignKey": [
{ {
"keysFrom": [ "keysFrom": ["userID"],
"userID"
],
"table": "users", "table": "users",
"keysTo": [ "keysTo": ["id"]
"id"
]
} }
], ],
"tableStruct": { "tableStruct": {

View file

@ -31,48 +31,51 @@ const logger = require('../../utils/logger.js')
let publicdirs = [] let publicdirs = []
let url = '' // http(s)//asd.basd let url = '' // http(s)//asd.basd
function GetApp () { function GetApp() {
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
app.set('views', [ app.set('views', ['./modules/main/views', './sharedViews'])
'./modules/main/views',
'./sharedViews'
])
publicdirs.forEach((pdir) => { publicdirs.forEach((pdir) => {
logger.Log(`Using public dir: ${pdir}`) logger.Log(`Using public dir: ${pdir}`)
app.use(express.static(pdir)) app.use(express.static(pdir))
}) })
app.use(busboy({ app.use(
busboy({
limits: { limits: {
fileSize: 10000 * 1024 * 1024 fileSize: 10000 * 1024 * 1024,
} },
})) })
)
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ app.use(
bodyParser.urlencoded({
limit: '5mb', limit: '5mb',
extended: true extended: true,
})) })
app.use(bodyParser.json({ )
limit: '5mb' app.use(
})) bodyParser.json({
limit: '5mb',
})
)
// -------------------------------------------------------------- // --------------------------------------------------------------
app.get('/', function (req, res) { app.get('/', function(req, res) {
res.render('main', { res.render('main', {
siteurl: url siteurl: url,
}) })
}) })
app.get('*', function (req, res) { app.get('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
app.post('*', function (req, res) { app.post('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -41,20 +41,22 @@ try {
logger.Log('Couldnt read donate URL file!', logger.GetColor('red')) logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
} }
function GetApp () { function GetApp() {
app.use(bodyParser.urlencoded({ app.use(
bodyParser.urlencoded({
limit: '5mb', limit: '5mb',
extended: true extended: true,
})) })
app.use(bodyParser.json({ )
limit: '5mb' app.use(
})) bodyParser.json({
limit: '5mb',
})
)
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
app.set('views', [ app.set('views', ['./modules/qmining/views', './sharedViews'])
'./modules/qmining/views', app.use(
'./sharedViews' auth({
])
app.use(auth({
userDB: userDB, userDB: userDB,
jsonResponse: false, jsonResponse: false,
exceptions: [ exceptions: [
@ -66,92 +68,98 @@ function GetApp () {
'/getVeteranPw', '/getVeteranPw',
'/moodle-test-userscript/stable.user.js', '/moodle-test-userscript/stable.user.js',
'/donate', '/donate',
'/irc' '/irc',
] ],
})) })
)
publicdirs.forEach((pdir) => { publicdirs.forEach((pdir) => {
logger.Log(`Using public dir: ${pdir}`) logger.Log(`Using public dir: ${pdir}`)
app.use(express.static(pdir)) app.use(express.static(pdir))
}) })
app.use(express.static(nextdir)) app.use(express.static(nextdir))
app.use(busboy({ app.use(
busboy({
limits: { limits: {
fileSize: 10000 * 1024 * 1024 fileSize: 10000 * 1024 * 1024,
} },
})) })
)
// -------------------------------------------------------------- // --------------------------------------------------------------
// REDIRECTS // REDIRECTS
// -------------------------------------------------------------- // --------------------------------------------------------------
// to be backwards compatible // to be backwards compatible
app.get('/ask', function (req, res) { app.get('/ask', function(req, res) {
logger.DebugLog(`Qmining module ask redirect`, 'ask', 1) logger.DebugLog(`Qmining module ask redirect`, 'ask', 1)
res.redirect(`http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}`) res.redirect(
`http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}`
)
}) })
const simpleRedirects = [ const simpleRedirects = [
{ {
from: '/dataeditor', from: '/dataeditor',
to: 'https://dataeditor.frylabs.net' to: 'https://dataeditor.frylabs.net',
}, },
{ {
from: '/install', from: '/install',
to: 'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js' to: 'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js',
}, },
{ {
from: '/servergit', from: '/servergit',
to: 'https://gitlab.com/MrFry/mrfrys-node-server' to: 'https://gitlab.com/MrFry/mrfrys-node-server',
}, },
{ {
from: '/scriptgit', from: '/scriptgit',
to: 'https://gitlab.com/MrFry/moodle-test-userscript' to: 'https://gitlab.com/MrFry/moodle-test-userscript',
}, },
{ {
from: '/qminingSite', from: '/qminingSite',
to: 'https://gitlab.com/MrFry/qmining-page' to: 'https://gitlab.com/MrFry/qmining-page',
}, },
{ {
from: '/classesgit', from: '/classesgit',
to: 'https://gitlab.com/MrFry/question-classes' to: 'https://gitlab.com/MrFry/question-classes',
}, },
{ {
from: '/menuClick', from: '/menuClick',
to: '/' to: '/',
}, },
{ {
from: '/lred', from: '/lred',
to: '/allQuestions.html' to: '/allQuestions.html',
}, },
{ {
from: '/donate', from: '/donate',
to: donateURL to: donateURL,
}, },
{ // to be backwards compatible {
// to be backwards compatible
from: '/legacy', from: '/legacy',
to: '/allQuestions.html' to: '/allQuestions.html',
}, },
{ {
from: '/allqr', from: '/allqr',
to: 'http://api.frylabs.net/allqr.txt' to: 'http://api.frylabs.net/allqr.txt',
}, },
{ {
from: '/allqr.txt', from: '/allqr.txt',
to: 'http://api.frylabs.net/allqr.txt' to: 'http://api.frylabs.net/allqr.txt',
}, },
{ {
from: '/infos', from: '/infos',
to: 'http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true', to: 'http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true',
nolog: true nolog: true,
}, },
{ {
from: '/irc', from: '/irc',
to: 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' to: 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining',
} },
] ]
simpleRedirects.forEach((redirect) => { simpleRedirects.forEach((redirect) => {
app.get(redirect.from, function (req, res) { app.get(redirect.from, function(req, res) {
if (!redirect.nolog) { if (!redirect.nolog) {
logger.LogReq(req) logger.LogReq(req)
} }
@ -162,7 +170,7 @@ function GetApp () {
// -------------------------------------------------------------- // --------------------------------------------------------------
function AddHtmlRoutes (files) { function AddHtmlRoutes(files) {
const routes = files.reduce((acc, f) => { const routes = files.reduce((acc, f) => {
if (f.includes('html')) { if (f.includes('html')) {
acc.push(f.split('.')[0]) acc.push(f.split('.')[0])
@ -173,7 +181,7 @@ function GetApp () {
routes.forEach((route) => { routes.forEach((route) => {
logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1) logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1)
app.get(`/${route}`, function (req, res) { app.get(`/${route}`, function(req, res) {
logger.LogReq(req) logger.LogReq(req)
res.redirect(`${route}.html`) res.redirect(`${route}.html`)
}) })
@ -183,29 +191,29 @@ function GetApp () {
// -------------------------------------------------------------- // --------------------------------------------------------------
app.get('/', function (req, res) { app.get('/', function(req, res) {
res.end('hai') res.end('hai')
logger.LogReq(req) logger.LogReq(req)
}) })
app.get('/getVeteranPw', function (req, res) { app.get('/getVeteranPw', function(req, res) {
res.render('veteranPw', { res.render('veteranPw', {
cid: req.query.cid || '', cid: req.query.cid || '',
devel: process.env.NS_DEVEL devel: process.env.NS_DEVEL,
}) })
logger.LogReq(req) logger.LogReq(req)
}) })
app.get('*', function (req, res) { app.get('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
app.post('*', function (req, res) { app.post('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -32,7 +32,7 @@ const utils = require('../../utils/utils.js')
// stuff gotten from server.js // stuff gotten from server.js
let publicdirs = [] let publicdirs = []
function GetApp () { function GetApp() {
const p = publicdirs[0] const p = publicdirs[0]
if (!p) { if (!p) {
throw new Error(`No public dir! ( SIO )`) throw new Error(`No public dir! ( SIO )`)
@ -42,73 +42,86 @@ function GetApp () {
const uloadFiles = p + 'f' const uloadFiles = p + 'f'
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
app.set('views', [ app.set('views', ['./modules/sio/views', './sharedViews'])
'./modules/sio/views',
'./sharedViews'
])
publicdirs.forEach((pdir) => { publicdirs.forEach((pdir) => {
logger.Log(`Using public dir: ${pdir}`) logger.Log(`Using public dir: ${pdir}`)
app.use(express.static(pdir)) app.use(express.static(pdir))
}) })
app.use(busboy({ app.use(
busboy({
limits: { limits: {
fileSize: 10000 * 1024 * 1024 fileSize: 10000 * 1024 * 1024,
} },
})) })
)
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ app.use(
bodyParser.urlencoded({
limit: '5mb', limit: '5mb',
extended: true extended: true,
})) })
app.use(bodyParser.json({ )
limit: '5mb' app.use(
})) bodyParser.json({
limit: '5mb',
})
)
// -------------------------------------------------------------- // --------------------------------------------------------------
app.get('/', function (req, res) { app.get('/', function(req, res) {
res.render('uload') res.render('uload')
res.end() res.end()
}) })
function UploadFile (req, res, path, next) { function UploadFile(req, res, path, next) {
var fstream var fstream
req.pipe(req.busboy) req.pipe(req.busboy)
req.busboy.on('file', function (fieldname, file, filename) { req.busboy.on('file', function(fieldname, file, filename) {
logger.Log('Uploading: ' + filename, logger.GetColor('blue')) logger.Log('Uploading: ' + filename, logger.GetColor('blue'))
utils.CreatePath(path, true) utils.CreatePath(path, true)
let d = new Date() let d = new Date()
let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename let fn =
d.getHours() +
'' +
d.getMinutes() +
'' +
d.getSeconds() +
'_' +
filename
fstream = fs.createWriteStream(path + '/' + fn) fstream = fs.createWriteStream(path + '/' + fn)
file.pipe(fstream) file.pipe(fstream)
fstream.on('close', function () { fstream.on('close', function() {
logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) logger.Log(
'Upload Finished of ' + path + '/' + fn,
logger.GetColor('blue')
)
next(fn) next(fn)
}) })
fstream.on('error', function (err) { fstream.on('error', function(err) {
console.log(err) console.log(err)
res.end('something bad happened :s') res.end('something bad happened :s')
}) })
}) })
} }
app.route('/fosuploader').post(function (req, res, next) { app.route('/fosuploader').post(function(req, res, next) {
UploadFile(req, res, uloadFiles, (fn) => { UploadFile(req, res, uloadFiles, (fn) => {
res.redirect('/f/' + fn) res.redirect('/f/' + fn)
}) })
}) })
app.get('*', function (req, res) { app.get('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
app.post('*', function (req, res) { app.post('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -32,7 +32,7 @@ const logger = require('../../utils/logger.js')
let publicdirs = [] let publicdirs = []
let url = '' let url = ''
function GetApp () { function GetApp() {
const p = publicdirs[0] const p = publicdirs[0]
if (!p) { if (!p) {
throw new Error(`No public dir! ( Stuff )`) throw new Error(`No public dir! ( Stuff )`)
@ -42,33 +42,36 @@ function GetApp () {
const listedFiles = './' + p + 'files' const listedFiles = './' + p + 'files'
app.set('view engine', 'ejs') app.set('view engine', 'ejs')
app.set('views', [ app.set('views', ['./modules/stuff/views', './sharedViews'])
'./modules/stuff/views',
'./sharedViews'
])
publicdirs.forEach((pdir) => { publicdirs.forEach((pdir) => {
logger.Log(`Using public dir: ${pdir}`) logger.Log(`Using public dir: ${pdir}`)
app.use(express.static(pdir)) app.use(express.static(pdir))
}) })
app.use(busboy({ app.use(
busboy({
limits: { limits: {
fileSize: 10000 * 1024 * 1024 fileSize: 10000 * 1024 * 1024,
} },
})) })
)
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ app.use(
bodyParser.urlencoded({
limit: '5mb', limit: '5mb',
extended: true extended: true,
})) })
app.use(bodyParser.json({ )
limit: '5mb' app.use(
})) bodyParser.json({
limit: '5mb',
})
)
// -------------------------------------------------------------- // --------------------------------------------------------------
// app, '/*.mp4', 'video/mp4', 'stuff/video' // app, '/*.mp4', 'video/mp4', 'stuff/video'
function appGetFileType (app, wildcard, contentType, pageToRender) { function appGetFileType(app, wildcard, contentType, pageToRender) {
app.get(wildcard, function (req, res) { app.get(wildcard, function(req, res) {
let p = decodeURI(req.url) let p = decodeURI(req.url)
let fp = p let fp = p
if (p.includes('?')) { if (p.includes('?')) {
@ -80,7 +83,7 @@ function GetApp () {
if (!fs.existsSync(fpath)) { if (!fs.existsSync(fpath)) {
res.render('nofile', { res.render('nofile', {
missingFile: fpath, missingFile: fpath,
url url,
}) })
return return
} }
@ -91,23 +94,21 @@ function GetApp () {
if (range) { if (range) {
const parts = range.replace(/bytes=/, '').split('-') const parts = range.replace(/bytes=/, '').split('-')
const start = parseInt(parts[0], 10) const start = parseInt(parts[0], 10)
const end = parts[1] const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1
? parseInt(parts[1], 10) const chunksize = end - start + 1
: fileSize - 1
const chunksize = (end - start) + 1
const file = fs.createReadStream(fpath, { start, end }) const file = fs.createReadStream(fpath, { start, end })
const head = { const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`, 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes', 'Accept-Ranges': 'bytes',
'Content-Length': chunksize, 'Content-Length': chunksize,
'Content-Type': contentType 'Content-Type': contentType,
} }
res.writeHead(206, head) res.writeHead(206, head)
file.pipe(res) file.pipe(res)
} else { } else {
const head = { const head = {
'Content-Length': fileSize, 'Content-Length': fileSize,
'Content-Type': contentType 'Content-Type': contentType,
} }
res.writeHead(200, head) res.writeHead(200, head)
fs.createReadStream(fpath).pipe(res) fs.createReadStream(fpath).pipe(res)
@ -121,7 +122,7 @@ function GetApp () {
fname, fname,
url, url,
contentType, contentType,
albumArt: GetAlbumArt(p) albumArt: GetAlbumArt(p),
}) })
} }
}) })
@ -132,10 +133,10 @@ function GetApp () {
['/*.mkv', 'audio/x-matroska', 'video'], ['/*.mkv', 'audio/x-matroska', 'video'],
['/*.mp3', 'audio/mpeg', 'audio'], ['/*.mp3', 'audio/mpeg', 'audio'],
['/*.pdf', 'application/pdf'], ['/*.pdf', 'application/pdf'],
['/*.zip', 'application/zip'] ['/*.zip', 'application/zip'],
] ]
function GetAlbumArt (path) { function GetAlbumArt(path) {
let tmp = path.split('.') let tmp = path.split('.')
tmp.pop() tmp.pop()
tmp = tmp.join('.').split('/') tmp = tmp.join('.').split('/')
@ -148,16 +149,23 @@ function GetApp () {
appGetFileType(app, t[0], t[1], t[2]) appGetFileType(app, t[0], t[1], t[2])
}) })
app.get('/*', function (req, res) { app.get('/*', function(req, res) {
let parsedUrl = decodeURI(req.url) let parsedUrl = decodeURI(req.url)
let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] let curr =
listedFiles +
'/' +
parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0]
let relPath = curr.substring(listedFiles.length, curr.length) let relPath = curr.substring(listedFiles.length, curr.length)
if (relPath[relPath.length - 1] !== '/') { relPath += '/' } if (relPath[relPath.length - 1] !== '/') {
relPath += '/'
}
let t = relPath.split('/') let t = relPath.split('/')
let prevDir = '' let prevDir = ''
for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } for (let i = 0; i < t.length - 2; i++) {
prevDir += t[i] + '/'
}
// curr = curr.replace(/\//g, "/"); // curr = curr.replace(/\//g, "/");
// relPath = relPath.replace(/\//g, "/"); // relPath = relPath.replace(/\//g, "/");
@ -167,14 +175,18 @@ function GetApp () {
try { try {
const stat = fs.lstatSync(curr) const stat = fs.lstatSync(curr)
if (stat.isDirectory() || stat.isSymbolicLink()) { if (stat.isDirectory() || stat.isSymbolicLink()) {
if (curr[curr.length - 1] !== '/') { curr += '/' } if (curr[curr.length - 1] !== '/') {
curr += '/'
}
let f = [] let f = []
let files = fs.readdirSync(curr) let files = fs.readdirSync(curr)
files.sort(function (a, b) { files.sort(function(a, b) {
return fs.statSync(curr + b).mtime.getTime() - return (
fs.statSync(curr + b).mtime.getTime() -
fs.statSync(curr + a).mtime.getTime() fs.statSync(curr + a).mtime.getTime()
)
}) })
files.forEach((item) => { files.forEach((item) => {
@ -186,7 +198,9 @@ function GetApp () {
res.size = Math.round(fileSizeInBytes / 1000000) res.size = Math.round(fileSizeInBytes / 1000000)
res.path = relPath res.path = relPath
if (res.path[res.path.length - 1] !== '/') { res.path += '/' } if (res.path[res.path.length - 1] !== '/') {
res.path += '/'
}
res.path += item res.path += item
res.mtime = stat['mtime'].toLocaleString() res.mtime = stat['mtime'].toLocaleString()
@ -200,7 +214,7 @@ function GetApp () {
folders: f, folders: f,
dirname: relPath, dirname: relPath,
prevDir, prevDir,
url url,
}) })
} else { } else {
let fileStream = fs.createReadStream(curr) let fileStream = fs.createReadStream(curr)
@ -209,23 +223,23 @@ function GetApp () {
} catch (e) { } catch (e) {
res.render('nofile', { res.render('nofile', {
missingFile: curr, missingFile: curr,
url url,
}) })
} }
}) })
// ----------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------
app.get('*', function (req, res) { app.get('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
app.post('*', function (req, res) { app.post('*', function(req, res) {
res.status(404).render('404') res.status(404).render('404')
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -19,7 +19,7 @@ Question Server
module.exports = { module.exports = {
ProcessIncomingRequest: ProcessIncomingRequest, ProcessIncomingRequest: ProcessIncomingRequest,
LoadJSON: LoadJSON LoadJSON: LoadJSON,
} }
const dataFile = './qminingPublic/data.json' const dataFile = './qminingPublic/data.json'
@ -38,7 +38,7 @@ const minMatchAmmountToAdd = 90 // FIXME: test this value
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
var currWrites = 0 var currWrites = 0
function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { function ProcessIncomingRequest(recievedData, qdb, infos, dryRun) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.DebugLog('Processing incoming request', 'actions', 1) logger.DebugLog('Processing incoming request', 'actions', 1)
if (recievedData === undefined) { if (recievedData === undefined) {
@ -48,13 +48,15 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
try { try {
let towrite = logger.GetDateString() + '\n' let towrite = logger.GetDateString() + '\n'
towrite += '------------------------------------------------------------------------------\n' towrite +=
'------------------------------------------------------------------------------\n'
if (typeof recievedData === 'object') { if (typeof recievedData === 'object') {
towrite += JSON.stringify(recievedData) towrite += JSON.stringify(recievedData)
} else { } else {
towrite += recievedData towrite += recievedData
} }
towrite += '\n------------------------------------------------------------------------------\n' towrite +=
'\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, recDataFile) utils.AppendToFile(towrite, recDataFile)
logger.DebugLog('recDataFile written', 'actions', 1) logger.DebugLog('recDataFile written', 'actions', 1)
} catch (e) { } catch (e) {
@ -78,7 +80,11 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
logger.DebugLog('Question:', 'actions', 2) logger.DebugLog('Question:', 'actions', 2)
logger.DebugLog(question, 'actions', 2) logger.DebugLog(question, 'actions', 2)
let q = new classes.Question(question.Q, question.A, question.data) let q = new classes.Question(question.Q, question.A, question.data)
logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3) logger.DebugLog(
'Searching for question in subj ' + d.subj,
'actions',
3
)
logger.DebugLog(q, 'actions', 3) logger.DebugLog(q, 'actions', 3)
let sames = qdb.Search(q, d.subj) let sames = qdb.Search(q, d.subj)
@ -86,7 +92,9 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
logger.DebugLog('Length: ' + sames.length, 'actions', 2) logger.DebugLog('Length: ' + sames.length, 'actions', 2)
logger.DebugLog(sames, 'actions', 3) logger.DebugLog(sames, 'actions', 3)
// if it didnt find any question, or every found questions match is lower thatn 80 // if it didnt find any question, or every found questions match is lower thatn 80
let isNew = sames.length === 0 || sames.every(searchResItem => { let isNew =
sames.length === 0 ||
sames.every((searchResItem) => {
return searchResItem.match < minMatchAmmountToAdd return searchResItem.match < minMatchAmmountToAdd
}) })
logger.DebugLog('isNew: ' + isNew, 'actions', 2) logger.DebugLog('isNew: ' + isNew, 'actions', 2)
@ -102,7 +110,11 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )` msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )`
allQuestions.forEach((q) => { allQuestions.forEach((q) => {
const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj) const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj)
logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3) logger.DebugLog(
'Adding question with subjName: ' + sName + ' :',
'actions',
3
)
logger.DebugLog(q, 'actions', 3) logger.DebugLog(q, 'actions', 3)
qdb.AddQuestion(sName, q) qdb.AddQuestion(sName, q)
}) })
@ -134,7 +146,9 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
idStats.LogId(d.id, d.subj) idStats.LogId(d.id, d.subj)
} }
logger.Log(subjRow) logger.Log(subjRow)
if (d.version !== undefined) { msg += '. Version: ' + d.version } if (d.version !== undefined) {
msg += '. Version: ' + d.version
}
logger.Log('\t' + msg, color) logger.Log('\t' + msg, color)
logger.DebugLog('New Questions:', 'actions', 2) logger.DebugLog('New Questions:', 'actions', 2)
@ -151,7 +165,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
} }
// loading stuff // loading stuff
function LoadJSON (dataFile) { function LoadJSON(dataFile) {
try { try {
var d = JSON.parse(utils.ReadFile(dataFile)) var d = JSON.parse(utils.ReadFile(dataFile))
var r = new classes.QuestionDB() var r = new classes.QuestionDB()
@ -166,7 +180,7 @@ function LoadJSON (dataFile) {
} }
rt.push({ rt.push({
name: d.Subjects[i].Name, name: d.Subjects[i].Name,
count: j count: j,
}) })
r.AddSubject(s) r.AddSubject(s)
} }

View file

@ -26,6 +26,6 @@ console.log(parsed.version)
utils.WriteFile(JSON.stringify(parsed), dataFile) utils.WriteFile(JSON.stringify(parsed), dataFile)
utils.WriteFile(parsed.version, versionFile) utils.WriteFile(parsed.version, versionFile)
function GetParams () { function GetParams() {
return process.argv.splice(2) return process.argv.splice(2)
} }

View file

@ -1,10 +1,10 @@
var debugLogger = null var debugLogger = null
function initLogger (logger) { function initLogger(logger) {
debugLogger = logger debugLogger = logger
} }
function debugLog (msg, name, lvl) { function debugLog(msg, name, lvl) {
if (debugLogger) { if (debugLogger) {
debugLogger(msg, name, lvl) debugLogger(msg, name, lvl)
} }
@ -18,26 +18,21 @@ const commonUselessAnswerParts = [
'A helyes válasz: ', 'A helyes válasz: ',
'A helyes válasz:', 'A helyes válasz:',
'The correct answer is:', 'The correct answer is:',
'\'' "'",
] ]
const commonUselessStringParts = [ const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
',', const specialChars = ['&', '\\+']
'\\.',
':',
'!',
'\\+',
'\\s*\\.'
]
const specialChars = [ '&', '\\+' ]
const lengthDiffMultiplier = 10 /* Percent minus for length difference */ const lengthDiffMultiplier = 10 /* Percent minus for length difference */
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */ const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
const assert = (val) => { const assert = (val) => {
if (!val) { throw new Error('Assertion failed') } if (!val) {
throw new Error('Assertion failed')
}
} }
class StringUtils { class StringUtils {
GetSubjNameWithoutYear (subjName) { GetSubjNameWithoutYear(subjName) {
let t = subjName.split(' - ') let t = subjName.split(' - ')
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t[1] || subjName return t[1] || subjName
@ -46,7 +41,7 @@ class StringUtils {
} }
} }
RemoveStuff (value, removableStrings, toReplace) { RemoveStuff(value, removableStrings, toReplace) {
removableStrings.forEach((x) => { removableStrings.forEach((x) => {
var regex = new RegExp(x, 'g') var regex = new RegExp(x, 'g')
value = value.replace(regex, toReplace || '') value = value.replace(regex, toReplace || '')
@ -54,14 +49,14 @@ class StringUtils {
return value return value
} }
SimplifyQuery (q) { SimplifyQuery(q) {
assert(q) assert(q)
var result = q.replace(/\n/g, ' ').replace(/\s/g, ' ') var result = q.replace(/\n/g, ' ').replace(/\s/g, ' ')
return this.RemoveUnnecesarySpaces(result) return this.RemoveUnnecesarySpaces(result)
} }
ShortenString (toShorten, ammount) { ShortenString(toShorten, ammount) {
assert(toShorten) assert(toShorten)
var result = '' var result = ''
@ -73,7 +68,7 @@ class StringUtils {
return result return result
} }
ReplaceCharsWithSpace (val, char) { ReplaceCharsWithSpace(val, char) {
assert(val) assert(val)
assert(char) assert(char)
@ -86,7 +81,7 @@ class StringUtils {
} }
// removes whitespace from begining and and, and replaces multiple spaces with one space // removes whitespace from begining and and, and replaces multiple spaces with one space
RemoveUnnecesarySpaces (toremove) { RemoveUnnecesarySpaces(toremove) {
assert(toremove) assert(toremove)
toremove = this.NormalizeSpaces(toremove) toremove = this.NormalizeSpaces(toremove)
@ -97,34 +92,41 @@ class StringUtils {
} }
// simplifies a string for easier comparison // simplifies a string for easier comparison
SimplifyStringForComparison (value) { SimplifyStringForComparison(value) {
assert(value) assert(value)
value = this.RemoveUnnecesarySpaces(value).toLowerCase() value = this.RemoveUnnecesarySpaces(value).toLowerCase()
return this.RemoveStuff(value, commonUselessStringParts) return this.RemoveStuff(value, commonUselessStringParts)
} }
RemoveSpecialChars (value) { RemoveSpecialChars(value) {
assert(value) assert(value)
return this.RemoveStuff(value, specialChars, ' ') return this.RemoveStuff(value, specialChars, ' ')
} }
// if the value is empty, or whitespace // if the value is empty, or whitespace
EmptyOrWhiteSpace (value) { EmptyOrWhiteSpace(value) {
// replaces /n-s with "". then replaces spaces with "". if it equals "", then its empty, or only consists of white space // replaces /n-s with "". then replaces spaces with "". if it equals "", then its empty, or only consists of white space
if (value === undefined) { return true } if (value === undefined) {
return (value.replace(/\n/g, '').replace(/ /g, '').replace(/\s/g, ' ') === '') return true
}
return (
value
.replace(/\n/g, '')
.replace(/ /g, '')
.replace(/\s/g, ' ') === ''
)
} }
// damn nonbreaking space // damn nonbreaking space
NormalizeSpaces (input) { NormalizeSpaces(input) {
assert(input) assert(input)
return input.replace(/\s/g, ' ') return input.replace(/\s/g, ' ')
} }
CompareString (s1, s2) { CompareString(s1, s2) {
if (!s1 || !s2) { if (!s1 || !s2) {
if (!s1 && !s2) { if (!s1 && !s2) {
return 100 return 100
@ -137,24 +139,27 @@ class StringUtils {
s2 = this.SimplifyStringForComparison(s2).split(' ') s2 = this.SimplifyStringForComparison(s2).split(' ')
var match = 0 var match = 0
for (var i = 0; i < s1.length; i++) { for (var i = 0; i < s1.length; i++) {
if (s2.includes(s1[i])) { match++ } if (s2.includes(s1[i])) {
match++
}
} }
var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
var lengthDifference = Math.abs(s2.length - s1.length) var lengthDifference = Math.abs(s2.length - s1.length)
percent -= lengthDifference * lengthDiffMultiplier percent -= lengthDifference * lengthDiffMultiplier
if (percent < 0) { percent = 0 } if (percent < 0) {
percent = 0
}
return percent return percent
} }
AnswerPreProcessor (value) { AnswerPreProcessor(value) {
assert(value) assert(value)
return this.RemoveStuff( return this.RemoveStuff(value, commonUselessAnswerParts)
value, commonUselessAnswerParts)
} }
// 'a. pécsi sör' -> 'pécsi sör' // 'a. pécsi sör' -> 'pécsi sör'
RemoveAnswerLetters (value) { RemoveAnswerLetters(value) {
assert(value) assert(value)
let s = value.split('. ') let s = value.split('. ')
@ -166,8 +171,10 @@ class StringUtils {
} }
} }
SimplifyQA (value, mods) { SimplifyQA(value, mods) {
if (!value) { return } if (!value) {
return
}
const reducer = (res, fn) => { const reducer = (res, fn) => {
return fn(res) return fn(res)
@ -176,27 +183,23 @@ class StringUtils {
return mods.reduce(reducer, value) return mods.reduce(reducer, value)
} }
SimplifyAnswer (value) { SimplifyAnswer(value) {
return this.SimplifyQA( return this.SimplifyQA(value, [
value,
[
this.RemoveSpecialChars.bind(this), this.RemoveSpecialChars.bind(this),
this.RemoveUnnecesarySpaces.bind(this), this.RemoveUnnecesarySpaces.bind(this),
this.AnswerPreProcessor.bind(this), this.AnswerPreProcessor.bind(this),
this.RemoveAnswerLetters.bind(this) this.RemoveAnswerLetters.bind(this),
]) ])
} }
SimplifyQuestion (value) { SimplifyQuestion(value) {
return this.SimplifyQA( return this.SimplifyQA(value, [
value,
[
this.RemoveSpecialChars.bind(this), this.RemoveSpecialChars.bind(this),
this.RemoveUnnecesarySpaces.bind(this) this.RemoveUnnecesarySpaces.bind(this),
]) ])
} }
SimplifyStack (stack) { SimplifyStack(stack) {
return this.SimplifyQuery(stack) return this.SimplifyQuery(stack)
} }
} }
@ -204,13 +207,13 @@ class StringUtils {
const SUtils = new StringUtils() const SUtils = new StringUtils()
class Question { class Question {
constructor (q, a, data) { constructor(q, a, data) {
this.Q = SUtils.SimplifyQuestion(q) this.Q = SUtils.SimplifyQuestion(q)
this.A = SUtils.SimplifyAnswer(a) this.A = SUtils.SimplifyAnswer(a)
this.data = { ...data } this.data = { ...data }
} }
toString () { toString() {
if (this.data.type !== 'simple') { if (this.data.type !== 'simple') {
return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data) return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data)
} else { } else {
@ -218,28 +221,31 @@ class Question {
} }
} }
HasQuestion () { HasQuestion() {
return this.Q !== undefined return this.Q !== undefined
} }
HasAnswer () { HasAnswer() {
return this.A !== undefined return this.A !== undefined
} }
HasImage () { HasImage() {
return this.data.type === 'image' return this.data.type === 'image'
} }
IsComplete () { IsComplete() {
return this.HasQuestion() && this.HasAnswer() return this.HasQuestion() && this.HasAnswer()
} }
CompareImage (data2) { CompareImage(data2) {
return SUtils.CompareString(this.data.images.join(' '), data2.images.join(' ')) return SUtils.CompareString(
this.data.images.join(' '),
data2.images.join(' ')
)
} }
// returns -1 if botth is simple // returns -1 if botth is simple
CompareData (qObj) { CompareData(qObj) {
try { try {
if (qObj.data.type === this.data.type) { if (qObj.data.type === this.data.type) {
let dataType = qObj.data.type let dataType = qObj.data.type
@ -248,7 +254,11 @@ class Question {
} else if (dataType === 'image') { } else if (dataType === 'image') {
return this.CompareImage(qObj.data) return this.CompareImage(qObj.data)
} else { } else {
debugLog(`Unhandled data type ${dataType}`, 'Compare question data', 1) debugLog(
`Unhandled data type ${dataType}`,
'Compare question data',
1
)
debugLog(qObj, 'Compare question data', 2) debugLog(qObj, 'Compare question data', 2)
} }
} else { } else {
@ -262,22 +272,22 @@ class Question {
return 0 return 0
} }
CompareQuestion (qObj) { CompareQuestion(qObj) {
return SUtils.CompareString(this.Q, qObj.Q) return SUtils.CompareString(this.Q, qObj.Q)
} }
CompareAnswer (qObj) { CompareAnswer(qObj) {
return SUtils.CompareString(this.A, qObj.A) return SUtils.CompareString(this.A, qObj.A)
} }
Compare (q2, data) { Compare(q2, data) {
assert(q2) assert(q2)
let qObj let qObj
if (typeof q2 === 'string') { if (typeof q2 === 'string') {
qObj = { qObj = {
Q: q2, Q: q2,
data: data data: data,
} }
} else { } else {
qObj = q2 qObj = q2
@ -307,42 +317,42 @@ class Question {
qMatch: qMatch, qMatch: qMatch,
aMatch: aMatch, aMatch: aMatch,
dMatch: dMatch, dMatch: dMatch,
avg: avg avg: avg,
} }
} }
} }
class Subject { class Subject {
constructor (n) { constructor(n) {
assert(n) assert(n)
this.Name = n this.Name = n
this.Questions = [] this.Questions = []
} }
setIndex (i) { setIndex(i) {
this.index = i this.index = i
} }
getIndex () { getIndex() {
return this.index return this.index
} }
get length () { get length() {
return this.Questions.length return this.Questions.length
} }
AddQuestion (q) { AddQuestion(q) {
assert(q) assert(q)
this.Questions.push(q) this.Questions.push(q)
} }
getSubjNameWithoutYear () { getSubjNameWithoutYear() {
return SUtils.GetSubjNameWithoutYear(this.Name) return SUtils.GetSubjNameWithoutYear(this.Name)
} }
getYear () { getYear() {
let t = this.Name.split(' - ')[0] let t = this.Name.split(' - ')[0]
if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t return t
@ -351,7 +361,7 @@ class Subject {
} }
} }
Search (q, data) { Search(q, data) {
assert(q) assert(q)
var r = [] var r = []
@ -361,7 +371,7 @@ class Subject {
r.push({ r.push({
q: this.Questions[i], q: this.Questions[i],
match: percent.avg, match: percent.avg,
detailedMatch: percent detailedMatch: percent,
}) })
} }
} }
@ -379,30 +389,36 @@ class Subject {
return r return r
} }
toString () { toString() {
var r = [] var r = []
for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) } for (var i = 0; i < this.Questions.length; i++) {
r.push(this.Questions[i].toString())
}
return '+' + this.Name + '\n' + r.join('\n') return '+' + this.Name + '\n' + r.join('\n')
} }
} }
class QuestionDB { class QuestionDB {
constructor () { constructor() {
this.Subjects = [] this.Subjects = []
} }
get length () { get length() {
return this.Subjects.length return this.Subjects.length
} }
AddQuestion (subj, q) { AddQuestion(subj, q) {
debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1) debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
debugLog(q, 'qdb add', 3) debugLog(q, 'qdb add', 3)
assert(subj) assert(subj)
var i = 0 var i = 0
while (i < this.Subjects.length && while (
!subj.toLowerCase().includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())) { i < this.Subjects.length &&
!subj
.toLowerCase()
.includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())
) {
i++ i++
} }
@ -417,7 +433,7 @@ class QuestionDB {
} }
} }
SimplifyQuestion (q) { SimplifyQuestion(q) {
if (typeof q === 'string') { if (typeof q === 'string') {
return SUtils.SimplifyQuestion(q) return SUtils.SimplifyQuestion(q)
} else { } else {
@ -427,7 +443,7 @@ class QuestionDB {
} }
} }
Search (q, subjName, data) { Search(q, subjName, data) {
assert(q) assert(q)
debugLog('Searching for question', 'qdb search', 1) debugLog('Searching for question', 'qdb search', 1)
debugLog('Question:', 'qdb search', 2) debugLog('Question:', 'qdb search', 2)
@ -447,7 +463,11 @@ class QuestionDB {
var r = [] var r = []
this.Subjects.forEach((subj) => { this.Subjects.forEach((subj) => {
if (subjName.toLowerCase().includes(subj.getSubjNameWithoutYear().toLowerCase())) { if (
subjName
.toLowerCase()
.includes(subj.getSubjNameWithoutYear().toLowerCase())
) {
debugLog(`Searching in ${subj.Name} `, 2) debugLog(`Searching in ${subj.Name} `, 2)
r = r.concat(subj.Search(q, data)) r = r.concat(subj.Search(q, data))
} }
@ -455,12 +475,20 @@ class QuestionDB {
// FIXME: try to remove this? but this is also a good backup plan so idk // FIXME: try to remove this? but this is also a good backup plan so idk
if (r.length === 0) { if (r.length === 0) {
debugLog('Reqults length is zero when comparing names, trying all subjects', 'qdb search', 1) debugLog(
'Reqults length is zero when comparing names, trying all subjects',
'qdb search',
1
)
this.Subjects.forEach((subj) => { this.Subjects.forEach((subj) => {
r = r.concat(subj.Search(q, data)) r = r.concat(subj.Search(q, data))
}) })
if (r.length > 0) { if (r.length > 0) {
debugLog(`FIXME: '${subjName}' gave no result but '' did!`, 'qdb search', 1) debugLog(
`FIXME: '${subjName}' gave no result but '' did!`,
'qdb search',
1
)
console.error(`FIXME: '${subjName}' gave no result but '' did!`) console.error(`FIXME: '${subjName}' gave no result but '' did!`)
} }
} }
@ -479,11 +507,13 @@ class QuestionDB {
return r return r
} }
AddSubject (subj) { AddSubject(subj) {
assert(subj) assert(subj)
var i = 0 var i = 0
while (i < this.length && subj.Name !== this.Subjects[i].Name) { i++ } while (i < this.length && subj.Name !== this.Subjects[i].Name) {
i++
}
if (i < this.length) { if (i < this.length) {
this.Subjects.concat(subj.Questions) this.Subjects.concat(subj.Questions)
@ -492,9 +522,11 @@ class QuestionDB {
} }
} }
toString () { toString() {
var r = [] var r = []
for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) } for (var i = 0; i < this.Subjects.length; i++) {
r.push(this.Subjects[i].toString())
}
return r.join('\n\n') return r.join('\n\n')
} }
} }

View file

@ -12,7 +12,7 @@ CreateDB()
authDB.close() authDB.close()
function CreateDB () { function CreateDB() {
const dbStruct = utils.ReadJSON(dbStructPath) const dbStruct = utils.ReadJSON(dbStructPath)
// authDB = dbtools.GetDB(':memory:') // authDB = dbtools.GetDB(':memory:')
authDB = dbtools.GetDB(usersDBPath) authDB = dbtools.GetDB(usersDBPath)
@ -20,7 +20,12 @@ function CreateDB () {
Object.keys(dbStruct).forEach((tableName) => { Object.keys(dbStruct).forEach((tableName) => {
const tableData = dbStruct[tableName] const tableData = dbStruct[tableName]
dbtools.CreateTable(authDB, tableName, tableData.tableStruct, tableData.foreignKey) dbtools.CreateTable(
authDB,
tableName,
tableData.tableStruct,
tableData.foreignKey
)
}) })
try { try {
@ -28,14 +33,16 @@ function CreateDB () {
const uids = utils.ReadFile('./ids').split('\n') const uids = utils.ReadFile('./ids').split('\n')
uids.forEach((cid, i) => { uids.forEach((cid, i) => {
if (!cid) { return } if (!cid) {
return
}
logger.Log(`[ ${i} / ${uids.length} ]`) logger.Log(`[ ${i} / ${uids.length} ]`)
try { try {
dbtools.Insert(authDB, 'users', { dbtools.Insert(authDB, 'users', {
pw: uuidv4(), pw: uuidv4(),
oldCID: cid, oldCID: cid,
avaiblePWRequests: 4, avaiblePWRequests: 4,
created: utils.GetDateString() created: utils.GetDateString(),
}) })
} catch (e) { } catch (e) {
logger.Log('Error during inserting', logger.GetColor('redbg')) logger.Log('Error during inserting', logger.GetColor('redbg'))
@ -52,10 +59,13 @@ function CreateDB () {
Object.keys(dbStruct).forEach((key) => { Object.keys(dbStruct).forEach((key) => {
const path = `${dir}/${key}.json` const path = `${dir}/${key}.json`
logger.Log(`Writing ${path}...`) logger.Log(`Writing ${path}...`)
utils.WriteFile(JSON.stringify({ utils.WriteFile(
JSON.stringify({
tableInfo: dbtools.TableInfo(authDB, key), tableInfo: dbtools.TableInfo(authDB, key),
tableRows: dbtools.SelectAll(authDB, key) tableRows: dbtools.SelectAll(authDB, key),
}), path) }),
path
)
}) })
logger.Log('Done') logger.Log('Done')

View file

@ -11,7 +11,7 @@ module.exports = {
SelectAll, SelectAll,
Select, Select,
Insert, Insert,
CloseDB CloseDB,
} }
const Sqlite = require('better-sqlite3') const Sqlite = require('better-sqlite3')
@ -21,7 +21,7 @@ const utils = require('../utils/utils.js')
const debugLog = process.env.NS_SQL_DEBUG_LOG const debugLog = process.env.NS_SQL_DEBUG_LOG
// { asd: 'asd', basd: 4 } => asd = 'asd', basd = 4 // { asd: 'asd', basd: 4 } => asd = 'asd', basd = 4
function GetSqlQuerry (conditions, type) { function GetSqlQuerry(conditions, type) {
const res = Object.keys(conditions).reduce((acc, key) => { const res = Object.keys(conditions).reduce((acc, key) => {
const item = conditions[key] const item = conditions[key]
if (typeof item === 'string') { if (typeof item === 'string') {
@ -40,20 +40,20 @@ function GetSqlQuerry (conditions, type) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
function GetDB (path) { function GetDB(path) {
utils.CreatePath(path) utils.CreatePath(path)
const res = new Sqlite(path) const res = new Sqlite(path)
res.pragma('synchronous = OFF') res.pragma('synchronous = OFF')
return res return res
} }
function DebugLog (msg) { function DebugLog(msg) {
if (debugLog) { if (debugLog) {
logger.DebugLog(msg, 'sql', 0) logger.DebugLog(msg, 'sql', 0)
} }
} }
function AddColumn (db, table, col) { function AddColumn(db, table, col) {
try { try {
const colName = Object.keys(col)[0] const colName = Object.keys(col)[0]
const colType = col.type const colType = col.type
@ -67,9 +67,8 @@ function AddColumn (db, table, col) {
} }
} }
function TableInfo (db, table) { function TableInfo(db, table) {
try { try {
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -85,13 +84,16 @@ function TableInfo (db, table) {
return { return {
columns: infoRes, columns: infoRes,
dataCount: countRes[Object.keys(countRes)[0]] dataCount: countRes[Object.keys(countRes)[0]],
} }
} }
function Update (db, table, newData, conditions) { function Update(db, table, newData, conditions) {
try { try {
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}` const s = `UPDATE ${table} SET ${GetSqlQuerry(
newData,
'set'
)} WHERE ${GetSqlQuerry(conditions, 'where')}`
const stmt = PrepareStatement(db, s) const stmt = PrepareStatement(db, s)
return stmt.run() return stmt.run()
@ -100,7 +102,7 @@ function Update (db, table, newData, conditions) {
} }
} }
function Delete (db, table, conditions) { function Delete(db, table, conditions) {
try { try {
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}` const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}`
const stmt = PrepareStatement(db, s) const stmt = PrepareStatement(db, s)
@ -111,14 +113,15 @@ function Delete (db, table, conditions) {
} }
} }
function CreateTable (db, name, columns, foreignKeys) { function CreateTable(db, name, columns, foreignKeys) {
// CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount // CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount
// number, lastLogin text, lastAccess text // number, lastLogin text, lastAccess text
// //
// FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname) ) // FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname) )
try { try {
const cols = Object.keys(columns).reduce((acc, key) => { const cols = Object.keys(columns)
.reduce((acc, key) => {
const item = columns[key] const item = columns[key]
// FIXME: array, and push stuff, then join() // FIXME: array, and push stuff, then join()
const flags = [] const flags = []
@ -127,7 +130,7 @@ function CreateTable (db, name, columns, foreignKeys) {
notNull: 'NOT NULL', notNull: 'NOT NULL',
unique: 'UNIQUE', unique: 'UNIQUE',
autoIncrement: 'AUTOINCREMENT', autoIncrement: 'AUTOINCREMENT',
defaultZero: 'DEFAULT 0' defaultZero: 'DEFAULT 0',
} }
Object.keys(toCheck).forEach((key) => { Object.keys(toCheck).forEach((key) => {
if (item[key]) { if (item[key]) {
@ -137,13 +140,18 @@ function CreateTable (db, name, columns, foreignKeys) {
acc.push(`${key} ${item.type} ${flags.join(' ')}`) acc.push(`${key} ${item.type} ${flags.join(' ')}`)
return acc return acc
}, []).join(', ') }, [])
.join(', ')
let fKeys = [] let fKeys = []
if (foreignKeys) { if (foreignKeys) {
foreignKeys.forEach((f) => { foreignKeys.forEach((f) => {
const { keysFrom, table, keysTo } = f const { keysFrom, table, keysTo } = f
fKeys.push(`, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`) fKeys.push(
`, FOREIGN KEY(${keysFrom.join(
', '
)}) REFERENCES ${table}(${keysTo.join(', ')})`
)
}) })
} }
@ -156,7 +164,7 @@ function CreateTable (db, name, columns, foreignKeys) {
} }
} }
function SelectAll (db, from) { function SelectAll(db, from) {
try { try {
const s = `SELECT * from ${from}` const s = `SELECT * from ${from}`
@ -167,7 +175,7 @@ function SelectAll (db, from) {
} }
} }
function Select (db, from, conditions) { function Select(db, from, conditions) {
try { try {
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}` const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}`
@ -178,14 +186,17 @@ function Select (db, from, conditions) {
} }
} }
function Insert (db, table, data) { function Insert(db, table, data) {
try { try {
const cols = Object.keys(data).reduce((acc, key) => { const cols = Object.keys(data)
.reduce((acc, key) => {
acc.push(`${key}`) acc.push(`${key}`)
return acc return acc
}, []).join(', ') }, [])
.join(', ')
const values = Object.keys(data).reduce((acc, key) => { const values = Object.keys(data)
.reduce((acc, key) => {
const item = data[key] const item = data[key]
if (typeof item === 'string') { if (typeof item === 'string') {
acc.push(`'${item}'`) acc.push(`'${item}'`)
@ -193,7 +204,8 @@ function Insert (db, table, data) {
acc.push(`${item}`) acc.push(`${item}`)
} }
return acc return acc
}, []).join(', ') }, [])
.join(', ')
const s = `INSERT INTO ${table} (${cols}) VALUES (${values})` const s = `INSERT INTO ${table} (${cols}) VALUES (${values})`
const stmt = PrepareStatement(db, s) const stmt = PrepareStatement(db, s)
@ -204,7 +216,7 @@ function Insert (db, table, data) {
} }
} }
function CloseDB (db) { function CloseDB(db) {
db.close((err) => { db.close((err) => {
if (err) { if (err) {
return console.error(err.message) return console.error(err.message)
@ -215,9 +227,11 @@ function CloseDB (db) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
function PrepareStatement (db, s) { function PrepareStatement(db, s) {
if (!db) { if (!db) {
throw new Error('DB is undefined in prepare statement! DB action called with undefined db') throw new Error(
'DB is undefined in prepare statement! DB action called with undefined db'
)
} }
DebugLog(s) DebugLog(s)
return db.prepare(s) return db.prepare(s)

View file

@ -20,7 +20,7 @@
module.exports = { module.exports = {
LogId: LogId, LogId: LogId,
Load: Load Load: Load,
} }
const utils = require('../utils/utils.js') const utils = require('../utils/utils.js')
@ -34,12 +34,15 @@ let data = {}
let vData = {} let vData = {}
let writes = 0 let writes = 0
function Load () { function Load() {
try { try {
var prevData = utils.ReadFile(idStatFile) var prevData = utils.ReadFile(idStatFile)
data = JSON.parse(prevData) data = JSON.parse(prevData)
} catch (e) { } catch (e) {
logger.Log('Error at loading id logs! (@ first run its normal)', logger.GetColor('redbg')) logger.Log(
'Error at loading id logs! (@ first run its normal)',
logger.GetColor('redbg')
)
console.log(e) console.log(e)
} }
@ -47,50 +50,60 @@ function Load () {
var prevVData = utils.ReadFile(idVStatFile) var prevVData = utils.ReadFile(idVStatFile)
vData = JSON.parse(prevVData) vData = JSON.parse(prevVData)
} catch (e) { } catch (e) {
logger.Log('Error at loading id logs! (@ first run its normal)', logger.GetColor('redbg')) logger.Log(
'Error at loading id logs! (@ first run its normal)',
logger.GetColor('redbg')
)
console.log(e) console.log(e)
} }
} }
function LogId (id, subj) { function LogId(id, subj) {
Inc(id, subj) Inc(id, subj)
AddVisitStat(id, subj) AddVisitStat(id, subj)
Save() Save()
} }
function AddSubjToList (list, subj) { function AddSubjToList(list, subj) {
if (!list[subj]) { if (!list[subj]) {
list[subj] = 0 list[subj] = 0
} }
list[subj]++ list[subj]++
} }
function Inc (value, subj) { function Inc(value, subj) {
if (data[value] === undefined) { if (data[value] === undefined) {
data[value] = { data[value] = {
count: 0, count: 0,
subjs: {} subjs: {},
} }
} }
data[value].count++ data[value].count++
AddSubjToList(data[value].subjs, subj) AddSubjToList(data[value].subjs, subj)
} }
function AddVisitStat (name, subj) { function AddVisitStat(name, subj) {
var m = new Date() var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) const now =
if (vData[now] === undefined) { vData[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) { if (vData[now][name] === undefined) {
vData[now][name] = { vData[now][name] = {
count: 0, count: 0,
subjs: {} subjs: {},
} }
} }
vData[now][name].count++ vData[now][name].count++
AddSubjToList(vData[now][name].subjs, subj) AddSubjToList(vData[now][name].subjs, subj)
} }
function Save () { function Save() {
writes++ writes++
if (writes === writeInterval) { if (writes === writeInterval) {
try { try {

View file

@ -18,7 +18,8 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
const hr = '---------------------------------------------------------------------------------' const hr =
'---------------------------------------------------------------------------------'
module.exports = { module.exports = {
GetDateString: GetDateString, GetDateString: GetDateString,
@ -30,7 +31,7 @@ module.exports = {
Load: Load, Load: Load,
logHashed: logHashed, logHashed: logHashed,
hr: hr, hr: hr,
C: C C: C,
} }
const DELIM = C('green') + '|' + C() const DELIM = C('green') + '|' + C()
@ -45,14 +46,7 @@ const uStatsFile = 'stats/ustats'
const uvStatsFile = 'stats/uvstats' const uvStatsFile = 'stats/uvstats'
const nologFile = './nolog' const nologFile = './nolog'
const colors = [ const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan']
'green',
'red',
'yellow',
'blue',
'magenta',
'cyan'
]
const writeInterval = 10 const writeInterval = 10
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
@ -66,19 +60,21 @@ let writes = 0
let noLogips = [] let noLogips = []
function GetDateString () { function GetDateString() {
const m = new Date() const m = new Date()
const d = utils.GetDateString() const d = utils.GetDateString()
return GetRandomColor(m.getHours().toString()) + d + C() return GetRandomColor(m.getHours().toString()) + d + C()
} }
function DebugLog (msg, name, lvl) { function DebugLog(msg, name, lvl) {
if (lvl <= debugLevel) { if (lvl <= debugLevel) {
if (msg === 'hr') { if (msg === 'hr') {
msg = hr msg = hr
} }
let s = msg let s = msg
let header = `${C('red')}#DEBUG${lvl}#${C('yellow')}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}` let header = `${C('red')}#DEBUG${lvl}#${C(
'yellow'
)}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
if (typeof msg !== 'object') { if (typeof msg !== 'object') {
s = header + msg s = header + msg
} else { } else {
@ -89,7 +85,7 @@ function DebugLog (msg, name, lvl) {
} }
} }
function Log (s, c) { function Log(s, c) {
let log = s let log = s
if (typeof s !== 'object') { if (typeof s !== 'object') {
let dl = DELIM + C(c) let dl = DELIM + C(c)
@ -100,7 +96,7 @@ function Log (s, c) {
utils.AppendToFile(log, logFile) utils.AppendToFile(log, logFile)
} }
function LogReq (req, toFile, sc) { function LogReq(req, toFile, sc) {
try { try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
@ -122,12 +118,13 @@ function LogReq (req, toFile, sc) {
hostname = req.hostname.replace('www.', '').split('.')[0] hostname = req.hostname.replace('www.', '').split('.')[0]
} else { } else {
hostname = 'NOHOST' hostname = 'NOHOST'
Log('req.hostname is undefined! req.hostname: ' + req.hostname, GetColor('redbg')) Log(
'req.hostname is undefined! req.hostname: ' + req.hostname,
GetColor('redbg')
)
} }
logEntry += dl + logEntry +=
hostname + dl + dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl
req.headers['user-agent'] + dl +
req.method + dl
if (req.session && req.session.user) { if (req.session && req.session.user) {
logEntry += C('cyan') + req.session.user.id + C() + dl logEntry += C('cyan') + req.session.user.id + C() + dl
@ -139,7 +136,9 @@ function LogReq (req, toFile, sc) {
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
if (sc !== undefined) { logEntry += dl + sc } if (sc !== undefined) {
logEntry += dl + sc
}
logEntry += C() logEntry += C()
if (!toFile) { if (!toFile) {
@ -156,7 +155,7 @@ function LogReq (req, toFile, sc) {
} }
} }
function parseNoLogFile (newData) { function parseNoLogFile(newData) {
noLogips = newData.split('\n') noLogips = newData.split('\n')
if (noLogips[noLogips.length - 1] === '') { if (noLogips[noLogips.length - 1] === '') {
noLogips.pop() noLogips.pop()
@ -167,7 +166,7 @@ function parseNoLogFile (newData) {
Log('\tNo Log IP-s changed: ' + noLogips.join(', ')) Log('\tNo Log IP-s changed: ' + noLogips.join(', '))
} }
function setNoLogReadInterval () { function setNoLogReadInterval() {
utils.WatchFile(nologFile, (newData) => { utils.WatchFile(nologFile, (newData) => {
parseNoLogFile(newData) parseNoLogFile(newData)
}) })
@ -175,7 +174,7 @@ function setNoLogReadInterval () {
parseNoLogFile(utils.ReadFile(nologFile)) parseNoLogFile(utils.ReadFile(nologFile))
} }
function Load () { function Load() {
Log('Loading logger...') Log('Loading logger...')
try { try {
uData = JSON.parse(utils.ReadFile(uStatsFile)) uData = JSON.parse(utils.ReadFile(uStatsFile))
@ -203,13 +202,16 @@ function Load () {
var prevVData = utils.ReadFile(vStatFile) var prevVData = utils.ReadFile(vStatFile)
vData = JSON.parse(prevVData) vData = JSON.parse(prevVData)
} catch (e) { } catch (e) {
Log('Error at loading visit logs! (@ first run its normal)', GetColor('redbg')) Log(
'Error at loading visit logs! (@ first run its normal)',
GetColor('redbg')
)
console.log(e) console.log(e)
} }
setNoLogReadInterval() setNoLogReadInterval()
} }
function LogStat (url, ip, hostname, userId) { function LogStat(url, ip, hostname, userId) {
let nolog = noLogips.some((x) => { let nolog = noLogips.some((x) => {
return x.includes(ip) return x.includes(ip)
}) })
@ -225,9 +227,11 @@ function LogStat (url, ip, hostname, userId) {
Save() Save()
} }
function IncUserStat (userId) { function IncUserStat(userId) {
try { try {
if (uData[userId] === undefined) { uData[userId] = 0 } if (uData[userId] === undefined) {
uData[userId] = 0
}
uData[userId]++ uData[userId]++
} catch (e) { } catch (e) {
Log('Error at making user ID stats!', GetColor('redbg')) Log('Error at making user ID stats!', GetColor('redbg'))
@ -235,12 +239,21 @@ function IncUserStat (userId) {
} }
} }
function AddUserIdStat (userId) { function AddUserIdStat(userId) {
try { try {
var m = new Date() var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) const now =
if (uvData[now] === undefined) { uvData[now] = {} } m.getFullYear() +
if (uvData[now][userId] === undefined) { uvData[now][userId] = 0 } '/' +
('0' + (m.getMonth() + 1)).slice(-2) +
'/' +
('0' + m.getDate()).slice(-2)
if (uvData[now] === undefined) {
uvData[now] = {}
}
if (uvData[now][userId] === undefined) {
uvData[now][userId] = 0
}
uvData[now][userId]++ uvData[now][userId]++
} catch (e) { } catch (e) {
Log('Error at making user ID stats!', GetColor('redbg')) Log('Error at making user ID stats!', GetColor('redbg'))
@ -248,21 +261,34 @@ function AddUserIdStat (userId) {
} }
} }
function Inc (value) { function Inc(value) {
if (value.startsWith('/?')) { value = '/' } if (value.startsWith('/?')) {
if (data[value] === undefined) { data[value] = 0 } value = '/'
}
if (data[value] === undefined) {
data[value] = 0
}
data[value]++ data[value]++
} }
function AddVisitStat (name) { function AddVisitStat(name) {
var m = new Date() var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) const now =
if (vData[now] === undefined) { vData[now] = {} } m.getFullYear() +
if (vData[now][name] === undefined) { vData[now][name] = 0 } '/' +
('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]++ vData[now][name]++
} }
function Save () { function Save() {
writes++ writes++
if (writes === writeInterval) { if (writes === writeInterval) {
try { try {
@ -295,11 +321,11 @@ function Save () {
} }
} }
function logHashed (x) { function logHashed(x) {
return GetRandomColor(x.toString()) + x + C() return GetRandomColor(x.toString()) + x + C()
} }
function GetRandomColor (ip) { function GetRandomColor(ip) {
if (!ip) { if (!ip) {
return 'red' return 'red'
} }
@ -310,20 +336,38 @@ function GetRandomColor (ip) {
return C(colors[res % colors.length]) return C(colors[res % colors.length])
} }
function GetColor (c) { function GetColor(c) {
return c return c
} }
function C (c) { function C(c) {
if (c !== undefined) { c = c.toLowerCase() } if (c !== undefined) {
c = c.toLowerCase()
}
if (c === 'redbg') { return '\x1b[41m' } if (c === 'redbg') {
if (c === 'bluebg') { return '\x1b[44m' } return '\x1b[41m'
if (c === 'green') { return '\x1b[32m' } }
if (c === 'red') { return '\x1b[31m' } if (c === 'bluebg') {
if (c === 'yellow') { return '\x1b[33m' } return '\x1b[44m'
if (c === 'blue') { return '\x1b[34m' } }
if (c === 'magenta') { return '\x1b[35m' } if (c === 'green') {
if (c === 'cyan') { return '\x1b[36m' } return '\x1b[32m'
}
if (c === 'red') {
return '\x1b[31m'
}
if (c === 'yellow') {
return '\x1b[33m'
}
if (c === 'blue') {
return '\x1b[34m'
}
if (c === 'magenta') {
return '\x1b[35m'
}
if (c === 'cyan') {
return '\x1b[36m'
}
return '\x1b[0m' return '\x1b[0m'
} }

View file

@ -17,6 +17,6 @@ utils.WriteFile(JSON.stringify(parsed), dataFile)
utils.WriteFile(parsed.motd, motdFile) utils.WriteFile(parsed.motd, motdFile)
function GetParams () { function GetParams() {
return process.argv.splice(2) return process.argv.splice(2)
} }

View file

@ -30,7 +30,7 @@ const logPath = './mergeLogs/mergelog_' + GetDateString().replace(/ /g, '_')
Main() Main()
function Main () { function Main() {
const params = GetParams() const params = GetParams()
console.log(params) console.log(params)
if (params.length === 0) { if (params.length === 0) {
@ -59,7 +59,7 @@ function Main () {
console.log(C('green') + 'Done' + C()) console.log(C('green') + 'Done' + C())
} }
function LogStats (stats, oldData, newData) { function LogStats(stats, oldData, newData) {
const maxSubjNameLength = MaxLengthOf(stats, 'name') const maxSubjNameLength = MaxLengthOf(stats, 'name')
const maxPrevLength = MaxLengthOf(stats, 'prevQuestions') const maxPrevLength = MaxLengthOf(stats, 'prevQuestions')
const maxAddedLength = MaxLengthOf(stats, 'addedQuestions') const maxAddedLength = MaxLengthOf(stats, 'addedQuestions')
@ -97,16 +97,25 @@ function LogStats (stats, oldData, newData) {
LogDataCount(newData) LogDataCount(newData)
} }
function LogDataCount (data) { function LogDataCount(data) {
const subjLength = data.Subjects.length const subjLength = data.Subjects.length
const qLength = data.Subjects.reduce((acc, subj) => { const qLength = data.Subjects.reduce((acc, subj) => {
return acc + subj.Questions.length return acc + subj.Questions.length
}, 0) }, 0)
console.log('Subjects: ' + C('green') + subjLength + C() + ', Questions: ' + C('green') + qLength + C()) console.log(
'Subjects: ' +
C('green') +
subjLength +
C() +
', Questions: ' +
C('green') +
qLength +
C()
)
} }
function PrintDB (data) { function PrintDB(data) {
const maxSubjNameLength = MaxLengthOf(data.Subjects, 'Name') const maxSubjNameLength = MaxLengthOf(data.Subjects, 'Name')
data.Subjects.forEach((subj) => { data.Subjects.forEach((subj) => {
@ -127,7 +136,7 @@ function PrintDB (data) {
console.log(hr()) console.log(hr())
} }
function GetExactLength (s, length) { function GetExactLength(s, length) {
let toLog = s.toString() let toLog = s.toString()
const lengthDiff = length - toLog.length const lengthDiff = length - toLog.length
for (let i = 0; i < lengthDiff; i++) { for (let i = 0; i < lengthDiff; i++) {
@ -137,7 +146,7 @@ function GetExactLength (s, length) {
return toLog return toLog
} }
function MaxLengthOf (prop, key) { function MaxLengthOf(prop, key) {
return prop.reduce((acc, currStat) => { return prop.reduce((acc, currStat) => {
if (acc < currStat[key].toString().length) { if (acc < currStat[key].toString().length) {
acc = currStat[key].toString().length acc = currStat[key].toString().length
@ -146,13 +155,14 @@ function MaxLengthOf (prop, key) {
}, 0) }, 0)
} }
function RemoveDuplicates (data) { function RemoveDuplicates(data) {
console.log(C('yellow') + 'Removing duplicates' + C()) console.log(C('yellow') + 'Removing duplicates' + C())
const res = new classes.QuestionDB() const res = new classes.QuestionDB()
const stats = [] const stats = []
data.Subjects.forEach((subj, i) => { data.Subjects.forEach((subj, i) => {
const logFile = logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-') const logFile =
logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-')
LogSubjProgress(i, subj, data.Subjects.length) LogSubjProgress(i, subj, data.Subjects.length)
let addedQuestions = 0 let addedQuestions = 0
let removedQuestions = 0 let removedQuestions = 0
@ -185,13 +195,13 @@ function RemoveDuplicates (data) {
name: subj.Name, name: subj.Name,
prevQuestions: subj.Questions.length, prevQuestions: subj.Questions.length,
addedQuestions: addedQuestions, addedQuestions: addedQuestions,
removedQuestions: removedQuestions removedQuestions: removedQuestions,
}) })
}) })
return { res, stats } return { res, stats }
} }
function LogSubjProgress (i, subj, subjCount) { function LogSubjProgress(i, subj, subjCount) {
log( log(
'[ ' + '[ ' +
C('cyan') + C('cyan') +
@ -211,7 +221,7 @@ function LogSubjProgress (i, subj, subjCount) {
) )
} }
function LogResultProgress (subj, addedQuestions, removedQuestions) { function LogResultProgress(subj, addedQuestions, removedQuestions) {
log( log(
' ' + ' ' +
C('cyan') + C('cyan') +
@ -227,11 +237,11 @@ function LogResultProgress (subj, addedQuestions, removedQuestions) {
) )
} }
function log (msg) { function log(msg) {
process.stdout.write(msg) process.stdout.write(msg)
} }
function hr (char) { function hr(char) {
let h = '' let h = ''
const cols = process.stdout.columns || 20 const cols = process.stdout.columns || 20
for (let i = 0; i < cols; i++) { for (let i = 0; i < cols; i++) {
@ -240,21 +250,27 @@ function hr (char) {
return h return h
} }
function C (color) { function C(color) {
return logger.C(color) return logger.C(color)
} }
function GetParams () { function GetParams() {
return process.argv.splice(2) return process.argv.splice(2)
} }
function GetDateString () { function GetDateString() {
const m = new Date() const m = new Date()
const d = m.getFullYear() + '-' + const d =
('0' + (m.getMonth() + 1)).slice(-2) + '-' + m.getFullYear() +
('0' + m.getDate()).slice(-2) + ' ' + '-' +
('0' + m.getHours()).slice(-2) + ':' + ('0' + (m.getMonth() + 1)).slice(-2) +
('0' + m.getMinutes()).slice(-2) + ':' + '-' +
('0' + m.getDate()).slice(-2) +
' ' +
('0' + m.getHours()).slice(-2) +
':' +
('0' + m.getMinutes()).slice(-2) +
':' +
('0' + m.getSeconds()).slice(-2) ('0' + m.getSeconds()).slice(-2)
return d return d
} }

View file

@ -11,7 +11,7 @@ module.exports = {
WatchFile: WatchFile, WatchFile: WatchFile,
ReadDir: ReadDir, ReadDir: ReadDir,
CopyFile: CopyFile, CopyFile: CopyFile,
GetDateString: GetDateString GetDateString: GetDateString,
} }
var fs = require('fs') var fs = require('fs')
@ -20,26 +20,33 @@ var logger = require('../utils/logger.js')
const dataFile = './qminingPublic/data.json' const dataFile = './qminingPublic/data.json'
function GetDateString () { function GetDateString() {
const m = new Date() const m = new Date()
return m.getFullYear() + '-' + return (
('0' + (m.getMonth() + 1)).slice(-2) + '-' + m.getFullYear() +
('0' + m.getDate()).slice(-2) + ' ' + '-' +
('0' + m.getHours()).slice(-2) + ':' + ('0' + (m.getMonth() + 1)).slice(-2) +
('0' + m.getMinutes()).slice(-2) + ':' + '-' +
('0' + m.getDate()).slice(-2) +
' ' +
('0' + m.getHours()).slice(-2) +
':' +
('0' + m.getMinutes()).slice(-2) +
':' +
('0' + m.getSeconds()).slice(-2) ('0' + m.getSeconds()).slice(-2)
)
} }
function CopyFile (from, to) { function CopyFile(from, to) {
CreatePath(to) CreatePath(to)
fs.copyFileSync(from, to) fs.copyFileSync(from, to)
} }
function ReadDir (path) { function ReadDir(path) {
return fs.readdirSync(path) return fs.readdirSync(path)
} }
function ReadJSON (name) { function ReadJSON(name) {
try { try {
return JSON.parse(ReadFile(name)) return JSON.parse(ReadFile(name))
} catch (e) { } catch (e) {
@ -48,16 +55,18 @@ function ReadJSON (name) {
} }
} }
function ReadFile (name) { function ReadFile(name) {
if (!FileExists(name)) { throw new Error('No such file: ' + name) } if (!FileExists(name)) {
throw new Error('No such file: ' + name)
}
return fs.readFileSync(name, 'utf8') return fs.readFileSync(name, 'utf8')
} }
function FileExists (path) { function FileExists(path) {
return fs.existsSync(path) return fs.existsSync(path)
} }
function WatchFile (file, callback) { function WatchFile(file, callback) {
if (FileExists(file)) { if (FileExists(file)) {
fs.watchFile(file, (curr, prev) => { fs.watchFile(file, (curr, prev) => {
fs.readFile(file, 'utf8', (err, data) => { fs.readFile(file, 'utf8', (err, data) => {
@ -76,8 +85,10 @@ function WatchFile (file, callback) {
} }
} }
function CreatePath (path, onlyPath) { function CreatePath(path, onlyPath) {
if (FileExists(path)) { return } if (FileExists(path)) {
return
}
var p = path.split('/') var p = path.split('/')
var currDir = p[0] var currDir = p[0]
@ -85,7 +96,9 @@ function CreatePath (path, onlyPath) {
if (currDir !== '' && !fs.existsSync(currDir)) { if (currDir !== '' && !fs.existsSync(currDir)) {
try { try {
fs.mkdirSync(currDir) fs.mkdirSync(currDir)
} catch (e) { console.log('Failed to make ' + currDir + ' directory... ') } } catch (e) {
console.log('Failed to make ' + currDir + ' directory... ')
}
} }
currDir += '/' + p[i] currDir += '/' + p[i]
} }
@ -95,32 +108,38 @@ function CreatePath (path, onlyPath) {
} }
} }
function WriteFile (content, path) { function WriteFile(content, path) {
CreatePath(path) CreatePath(path)
fs.writeFileSync(path, content) fs.writeFileSync(path, content)
} }
function WriteFileAsync (content, path) { function WriteFileAsync(content, path) {
CreatePath(path) CreatePath(path)
fs.writeFile(path, content, function (err) { fs.writeFile(path, content, function(err) {
if (err) { if (err) {
logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg')) logger.Log(
'Error writing file: ' + path + ' (sync)',
logger.GetColor('redbg')
)
} }
}) })
} }
function AppendToFile (data, file) { function AppendToFile(data, file) {
CreatePath(file) CreatePath(file)
try { try {
fs.appendFileSync(file, '\n' + data) fs.appendFileSync(file, '\n' + data)
} catch (e) { } catch (e) {
logger.Log('Error appendig to file log file: ' + file + ' (sync)', logger.GetColor('redbg')) logger.Log(
'Error appendig to file log file: ' + file + ' (sync)',
logger.GetColor('redbg')
)
logger.Log(data) logger.Log(data)
console.log(e) console.log(e)
} }
} }
function Beep () { function Beep() {
try { try {
process.stdout.write('\x07') process.stdout.write('\x07')
} catch (e) { } catch (e) {
@ -128,9 +147,12 @@ function Beep () {
} }
} }
function WriteBackup () { function WriteBackup() {
try { try {
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString()) WriteFileAsync(
ReadFile(dataFile),
'public/backs/data_' + new Date().toString()
)
} catch (e) { } catch (e) {
logger.Log('Error backing up data json file!', logger.GetColor('redbg')) logger.Log('Error backing up data json file!', logger.GetColor('redbg'))
console.log(e) console.log(e)