var express = require('express'); var router = express.Router(); var ntr = require("../neutral-functions.js") var fs = require("fs") var path = require("path") var mime = require('mime-types'); const { compileString } = require('sass'); /* GET home page. */ router.get('/', function(req, res, next) { var check = ntr.checkToken(req, res) if(check.name != false) { res.redirect(302, "/") } else { res.render('login', {error: ""}); } }); router.post("/", function(req, res, next) { var check = ntr.checkToken(req, res) if(check.name == false) { res.send({"result":"failed", "content":"ERROR_TOKEN_NOT_VALID"}) } else { if(check.permLevel >= 3) { const bod = req.body if(bod.request == "add") { res.send({"result":"failed", "content":"ERROR_NOT_MAKE"}) } else if(bod.request == "get") { var files = new Array() const directory = req.body.value fs.readdir(directory.replace("/", path.sep.replace("\\", path.sep)), (err, data) => { if(err) { res.send({"result":"success", "content": "ERROR_NOT_EXIST"}) } else { try { for(var file of data) { const stat = fs.statSync(directory.replace("/", path.sep.replace("\\", path.sep)) + path.sep + file) files.push({"name":file, "fileDirectory" :directory.replace("/", path.sep.replace("\\", path.sep)) + path.sep + file , "type":mime.lookup(file), "size":stat.size, "lastedition":stat.mtimeMs, "directory":stat.isDirectory()}) } res.send({"result":"success", "content": files}) } catch(err) { res.send({"result":"success", "content": "ERROR_NOT_PERMITTED"}) } } }) } else if(bod.request == "del") { const file = bod.value if(fs.existsSync(bod.value.fileDirectory) == false) { res.send({"result":"success", "content":"ERROR_NOT_EXIST"}) } else { try { if(file.directory == true) { fs.rmdirSync(file.fileDirectory , { recursive: true, force: true }) } else { fs.rmSync(file.fileDirectory) } console.log("DELETED") res.send({"result":"success", "content":"DELETED"}) } catch(error) { console.log(error) console.log("NOT DELETED") res.send({"result":"success", "content":"ERROR_NOT_MAKEABLE"}) } console.log("NOT DELETED") } } else if(bod.request == "rename") { if(fs.existsSync(bod.value) == true) { res.send({"result":"success", "content":"ERROR_ALREADY_EXIST"}) } else { try { fs.renameSync(bod.currentDir, bod.value) res.send({"result":"success", "content":"RENAMED"}) } catch(error) { console.log(error) res.send({"result":"success", "content":"ERROR_NOT_MAKEABLE"}) } } } else if(bod.request == "addfolder") { if(fs.existsSync(bod.currentDir + path.sep + bod.value) == true) { res.send({"result":"success", "content":"ERROR_ALREADY_EXIST"}) } else { try { fs.mkdirSync(bod.currentDir + path.sep + bod.value) res.send({"result":"success", "content":"CREATED"}) } catch(error) { console.log(error) res.send({"result":"success", "content":"ERROR_NOT_MAKEABLE"}) } } } else { res.send({"result":"failed", "content":"ERROR_REQUEST_MISSING_FIELDS", "additional": "REQUEST_MISSING"}) } } else { res.send({"result":"failed", "content":"ERROR_USER_PERMISSION_TOO_LOW"}) } } }) module.exports = router;