neutral-old/routes/cv.js
Raphix eafa16e46d
All checks were successful
Neutral/pipeline/head This commit looks good
Final Improvement for CV Editor
2023-03-29 18:48:39 +02:00

555 lines
17 KiB
JavaScript

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');
const { response } = require('../app.js');
/* 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 >= 2) {
const bod = req.body
if(bod.request == "get") {
const markdownFolder = "/home/gitlab-ci/cv/dashboard-raphael/markdown/"
if(bod.value == "WHOAMI") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "whoami.md")
} else {
fileData = fs.readFileSync("../MD_TEST/whoami.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.value == "CONTACT") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "info.md")
} else {
fileData = fs.readFileSync("../MD_TEST/info.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.value == "FORMATION") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "formation.md")
} else {
fileData = fs.readFileSync("../MD_TEST/formation.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.value == "DIPLOME") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "certif.md")
} else {
fileData = fs.readFileSync("../MD_TEST/certif.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.value == "LOBBIES") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "passions.md")
} else {
fileData = fs.readFileSync("../MD_TEST/passions.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.value == "FIND") {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "find.md")
} else {
fileData = fs.readFileSync("../MD_TEST/find.md")
}
res.send({"result":"success", "content":fileData.toString()})
} else {
var fileData = null;
if(fs.existsSync(markdownFolder)) {
fileData = fs.readFileSync(markdownFolder + "project/" + bod.value + ".md")
} else {
fileData = fs.readFileSync("../MD_TEST/project/" + bod.value + ".md")
}
res.send({"result":"success", "content":fileData.toString()})
}
} else if(bod.request == "set") {
const markdownFolder = "/home/gitlab-ci/cv/dashboard-raphael/markdown/"
if(bod.type == "WHOAMI") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "whoami.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/whoami.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
} else if(bod.type == "CONTACT") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "info.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/info.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
res.send({"result":"success", "content":fileData.toString()})
} else if(bod.type == "FORMATION") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "formation.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/formation.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
} else if(bod.type == "DIPLOME") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "certif.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/certif.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
} else if(bod.type == "LOBBIES") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "passions.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/passions.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
} else if(bod.type == "FIND") {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "find.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/find.md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
} else {
if(fs.existsSync(markdownFolder)) {
fs.writeFile(markdownFolder + "/project/" + bod.type + ".md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
} else {
fs.writeFile("../MD_TEST/project/" + bod.type + ".md", bod.value, (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
console.log(err)
}
})
}
}
} else if(bod.request == "get-projects") {
const projectFolder = "/home/gitlab-ci/cv/dashboard-raphael/markdown/project/"
if(fs.existsSync(projectFolder)) {
var allProject = new Array()
fs.readdirSync(projectFolder).forEach((file) => {
allProject.push((file).replace(".md", ""))
})
res.send({"result":"success", "content":allProject})
} else {
var allProject = new Array()
fs.readdirSync("../MD_TEST/project/").forEach((file) => {
if(fs.statSync("../MD_TEST/project/" + file).isDirectory()) {
console.log(file)
} else {
allProject.push((file).replace(".md", ""))
}
})
res.send({"result":"success", "content":allProject})
}
} else if(bod.request == "del") {
const projectFolder = "/home/gitlab-ci/cv/dashboard-raphael/markdown/project/"
if(fs.existsSync(projectFolder + bod.value + ".md")) {
fs.rm(projectFolder + bod.value + ".md", (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
}
})
} else if(fs.existsSync("../MD_TEST/project/" + bod.value + ".md")) {
fs.rm("../MD_TEST/project/" + bod.value + ".md", (err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
}
})
} else {
res.send({"result":"failed", "content":"FILE_NO_EXIST"})
}
} else if(bod.request == "add") {
const projectFolder = "/home/gitlab-ci/cv/dashboard-raphael/markdown/project/"
if(fs.existsSync(projectFolder)) {
fs.writeFile(projectFolder + bod.value + ".md", "**" + bod.value + "**" ,(err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
}
})
} else {
fs.writeFile("../MD_TEST/project/" + bod.value + ".md", "**" + bod.value + "**" ,(err) => {
if(err) {
res.send({"result":"success", "content":"ERROR_NOT_PERMITTED"})
console.log(err)
} else {
res.send({"result":"success", "content":"NO_PROBLEM"})
}
})
}
}
} else {
res.send({"result":"failed", "content":"ERROR_USER_PERMISSION_TOO_LOW"})
}
}
})
module.exports = router;