mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Added uploaddata API for dataEditor module
This commit is contained in:
parent
105d1ee8be
commit
890998bce5
3 changed files with 80 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue