From 890998bce504ea2b21757585467c563c06ccccaf Mon Sep 17 00:00:00 2001 From: MrFry Date: Wed, 25 Mar 2020 16:39:55 +0100 Subject: [PATCH] Added uploaddata API for dataEditor module --- devel/readme.md | 7 +++++ modules/api/api.js | 70 +++++++++++++++++++++++++++++++++++++++++++--- utils/utils.js | 8 +++++- 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/devel/readme.md b/devel/readme.md index bf0ddfa..6069497 100644 --- a/devel/readme.md +++ b/devel/readme.md @@ -22,6 +22,9 @@ Jelenleg a max ilyen 3-4 körül van. Minél nagyobb a szám annál bővebb a lo ## Hogy az API és a többi modul tudjon kommunikálni: +__Ezt a rész csak nagyon kevés esetben kell megcsinálni, ajánlott kihagyni! Ennélkül is működik +lokálisan az API és a usercript!__ + 1. Ezt a pár sort add hozzá a `/etc/hosts` fájlhoz: ``` @@ -71,3 +74,7 @@ Cliens ID összes statisztika napokba rendezve ### recdata Az az adat, amit a szerver az `/isadding` végpontra kap + +### dataEdits + +Néhány fontos log amit az api generál mikor a felhasználók a dataEditor modult használják diff --git a/modules/api/api.js b/modules/api/api.js index 70e2115..ed3a210 100644 --- a/modules/api/api.js +++ b/modules/api/api.js @@ -36,6 +36,8 @@ const dataFile = 'public/data.json' const msgFile = 'stats/msgs' const motdFile = 'public/motd' const versionFile = 'public/version' +const passwordFile = 'data/dataEditorPasswords.json' +const dataEditsLog = 'stats/dataEdits' app.set('view engine', 'ejs') app.set('views', [ @@ -45,16 +47,15 @@ app.set('views', [ app.use(express.static('public')) app.use(busboy({ limits: { - fileSize: 10000 * 1024 * 1024 + fileSize: 50000 * 1024 * 1024 } })) -app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ - limit: '5mb', + limit: '10mb', extended: true })) app.use(bodyParser.json({ - limit: '5mb' + limit: '10mb' })) var data = actions.LoadJSON(dataFile) @@ -159,6 +160,67 @@ app.get('/allqr.txt', function (req, res) { // ------------------------------------------------------------------------------------------- // API +app.post('/uploaddata', (req, res) => { + // body: JSON.stringify({ + // newData: data, + // count: getCount(data), + // initialCount: initialCount, + // password: password, + // editedQuestions: editedQuestions + // }) + + const { count, initialCount, editedQuestions, password, newData } = req.body + const respStatuses = { + invalidPass: 'invalidPass', + ok: 'ok', + error: 'error' + } + + logger.LogReq(req) + + try { + // finding user + const pwds = JSON.parse(utils.ReadFile(passwordFile)) + let user = Object.keys(pwds).find((key) => { + const u = pwds[key] + return u.password === password + }) + user = pwds[user] + + // logging and stuff + logger.Log(`Data upload`, logger.GetColor('bluebg'), true) + logger.Log(`PWD: ${password}`, logger.GetColor('bluebg'), true) + // returning if user password is not ok + if (!user) { + logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red'), true) + utils.AppendToFile(logger.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) + res.json({ status: respStatuses.invalidPass }) + return + } + + logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg'), true) + logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg'), true) + // saving detailed editedCount + utils.AppendToFile(logger.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) + + // making backup + utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${new Date().toString().replace(/ /g, '_')}`) + // writing data + utils.WriteFile(JSON.stringify(data), dataFile) + // reloading data file + data = newData + + res.json({ + status: respStatuses.ok, + user: user.name + }) + } catch (e) { + logger.Log(`Data upload error! `, logger.GetColor('redbg'), true) + console.error(e) + res.json({ status: respStatuses.error, msg: e.message }) + } +}) + app.post('/isAdding', function (req, res) { logger.LogReq(req) diff --git a/utils/utils.js b/utils/utils.js index 82c5843..c376773 100755 --- a/utils/utils.js +++ b/utils/utils.js @@ -8,7 +8,8 @@ module.exports = { FileExists: FileExists, CreatePath: CreatePath, WatchFile: WatchFile, - ReadDir: ReadDir + ReadDir: ReadDir, + CopyFile: CopyFile } var fs = require('fs') @@ -17,6 +18,11 @@ var logger = require('../utils/logger.js') const dataFile = './public/data.json' +function CopyFile (from, to) { + CreatePath(to) + fs.copyFileSync(from, to) +} + function ReadDir (path) { return fs.readdirSync(path) }