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

@ -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,18 +91,21 @@ 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 = ''
@ -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,7 +243,7 @@ function GetApp () {
requestedPWS: user.pwRequestCount, requestedPWS: user.pwRequestCount,
maxPWCount: maxPWCount, maxPWCount: maxPWCount,
// daysAfterUserGetsPWs: daysAfterUserGetsPWs, // daysAfterUserGetsPWs: daysAfterUserGetsPWs,
addPWPerDay: addPWPerDay addPWPerDay: addPWPerDay,
}) })
}) })
@ -238,33 +255,45 @@ 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,
}) })
}) })
@ -272,29 +301,37 @@ function GetApp () {
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,11 +496,11 @@ 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',
}) })
}) })
@ -448,19 +523,32 @@ function GetApp () {
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 })
}) })
@ -473,12 +561,22 @@ function GetApp () {
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) {
@ -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) {
@ -590,15 +722,17 @@ 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,
}) })
}) })
}) })
@ -610,7 +744,7 @@ function GetApp () {
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,7 +773,7 @@ function GetApp () {
message: `Invalid question :(`, message: `Invalid question :(`,
result: [], result: [],
recievedData: JSON.stringify(req.query), recievedData: JSON.stringify(req.query),
success: false success: false,
}) })
} }
} }
@ -647,14 +784,14 @@ function GetApp () {
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,
} }
}) })
} }
@ -664,7 +801,7 @@ function GetApp () {
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())
@ -678,7 +815,7 @@ function GetApp () {
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()
@ -704,20 +841,28 @@ function GetApp () {
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!')
}) })
@ -749,12 +894,22 @@ 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,
}
)
} }
}) })
} }
@ -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

@ -33,33 +33,36 @@ 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,
}) })
}) })
@ -72,7 +75,7 @@ function GetApp () {
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -42,19 +42,21 @@ try {
} }
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,19 +68,22 @@ 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
@ -87,67 +92,70 @@ function GetApp () {
// 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) => {
@ -191,7 +199,7 @@ function GetApp () {
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)
}) })
@ -205,7 +213,7 @@ function GetApp () {
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -42,27 +42,30 @@ 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',
})
)
// -------------------------------------------------------------- // --------------------------------------------------------------
@ -79,12 +82,22 @@ function GetApp () {
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) {
@ -108,7 +121,7 @@ function GetApp () {
}) })
return { return {
app: app app: app,
} }
} }

View file

@ -42,27 +42,30 @@ 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',
})
)
// -------------------------------------------------------------- // --------------------------------------------------------------
@ -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,7 +133,7 @@ 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) {
@ -150,14 +151,21 @@ function GetApp () {
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,7 +223,7 @@ function GetApp () {
} catch (e) { } catch (e) {
res.render('nofile', { res.render('nofile', {
missingFile: curr, missingFile: curr,
url url,
}) })
} }
}) })
@ -225,7 +239,7 @@ function GetApp () {
}) })
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'
@ -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)
@ -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

@ -18,22 +18,17 @@ 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 = [
',',
'\\.',
':',
'!',
'\\+',
'\\s*\\.'
] ]
const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
const specialChars = ['&', '\\+'] 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 {
@ -113,8 +108,15 @@ class StringUtils {
// 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
@ -137,20 +139,23 @@ 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'
@ -167,7 +172,9 @@ 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)
@ -177,22 +184,18 @@ class StringUtils {
} }
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),
]) ])
} }
@ -235,7 +238,10 @@ class Question {
} }
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
@ -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 {
@ -277,7 +287,7 @@ class Question {
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,7 +317,7 @@ class Question {
qMatch: qMatch, qMatch: qMatch,
aMatch: aMatch, aMatch: aMatch,
dMatch: dMatch, dMatch: dMatch,
avg: avg avg: avg,
} }
} }
} }
@ -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,
}) })
} }
} }
@ -381,7 +391,9 @@ class Subject {
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')
} }
} }
@ -401,8 +413,12 @@ class QuestionDB {
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++
} }
@ -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!`)
} }
} }
@ -483,7 +511,9 @@ class QuestionDB {
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)
@ -494,7 +524,9 @@ 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

@ -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')
@ -69,7 +69,6 @@ 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()
@ -118,7 +120,8 @@ function CreateTable (db, name, columns, foreignKeys) {
// 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(', ')})`
)
}) })
} }
@ -180,12 +188,15 @@ 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)
@ -217,7 +229,9 @@ 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')
@ -39,7 +39,10 @@ function Load () {
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,7 +50,10 @@ 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)
} }
} }
@ -69,7 +75,7 @@ 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++
@ -78,12 +84,19 @@ function Inc (value, 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++

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
@ -78,7 +72,9 @@ function DebugLog (msg, name, lvl) {
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 {
@ -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) {
@ -203,7 +202,10 @@ 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()
@ -227,7 +229,9 @@ function LogStat (url, ip, hostname, userId) {
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'))
@ -238,9 +242,18 @@ 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'))
@ -249,16 +262,29 @@ 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]++
} }
@ -315,15 +341,33 @@ function GetColor (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

@ -103,7 +103,16 @@ function LogDataCount (data) {
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) {
@ -152,7 +161,8 @@ function RemoveDuplicates (data) {
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,7 +195,7 @@ 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 }
@ -250,11 +260,17 @@ function GetParams () {
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')
@ -22,12 +22,19 @@ 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) {
@ -49,7 +56,9 @@ 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')
} }
@ -77,7 +86,9 @@ 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]
} }
@ -104,7 +117,10 @@ 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')
)
} }
}) })
} }
@ -114,7 +130,10 @@ function AppendToFile (data, 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)
} }
@ -130,7 +149,10 @@ 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)