var express = require('express'); var router = express.Router(); var path = require("path") var fs = require("fs") var CryptoJS = require("crypto-js"); var uuid = require("uuid") var ntr = require("../neutral-functions.js"); const Jimp = require("jimp"); const { compileString } = require('sass'); const { exec } = require('child_process'); /* 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 { const image = req.files.apic if(req.headers.uploadforuser != null) { if(check.permLevel == 4) { if(req.files.apic.mimetype == "image/png" | req.files.apic.mimetype == "image/jpeg" ) { var uploadPath = ""; var uploadDir = __dirname.replace("routes","public" + path.sep + "images" + path.sep + "userspics" + path.sep) if(req.files.apic.mimetype == "image/png") { uploadPath = req.headers.uploadforuser + ".png" } if(req.files.apic.mimetype == "image/jpeg") { uploadPath = req.headers.uploadforuser+ ".jpg" } image.mv(__dirname.replace("routes", "") + path.sep + uploadPath, function(err) { if(err) { res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."}) console.log(err) } else { if(fs.existsSync(uploadDir + req.headers.uploadforuser + ".png") | fs.existsSync(uploadDir + req.headers.uploadforuser + ".jpg")) { if(fs.existsSync(uploadDir + req.headers.uploadforuser+ ".png")) { fs.rmSync(uploadDir + req.headers.uploadforuser + ".png") } fs.renameSync(__dirname.replace("routes", "") + uploadPath , uploadDir + uploadPath) if(req.files.apic.mimetype == "image/jpeg") { Jimp.read(uploadDir + uploadPath, function (err, image) { image.resize(200,200, function(err){ if (err) { console.log(err) } }).write(uploadDir + uploadPath.replace("jpg", "png")) fs.rmSync(uploadDir + req.headers.uploadforuser + ".jpg") res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) }) } else if(req.files.apic.mimetype == "image/png") { Jimp.read(uploadDir + uploadPath, function (err, image) { image.resize(200,200, function(err){ if (err) { console.log(err) } }).write(uploadDir + uploadPath) res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) }) } else { res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) } } else { console.log(uploadDir + uploadPath) res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."}) console.log("ERROR - UPLOAD NOT WORK CORRECYLT") } } }) } else { res.send({"result":"success", "content": "Seul les images (JPEG et PNG) sont acceptés."}) } } } else if(req.headers.uploadfiledirectory != null) { if(fs.existsSync(req.headers.uploadfiledirectory)) { image.mv(req.headers.uploadfiledirectory + path.sep + req.files.apic.name, (error) => { if(error) { res.send({"result":"ERROR"}) } else { res.send({"result":"SUCCESS"}) } }) } else { res.send({"result":"ERROR"}) } } else if(req.headers.uploadforproject != null) { const projectFolder = "/home/gitlab-ci/cv/dashboard-raphael/public/images/" if(fs.existsSync(projectFolder)) { image.mv(projectFolder + req.headers.uploadforproject + ".jpg", (error) => { if(error) { res.send({"result":"ERROR"}) console.log(error) } else { res.send({"result":"SUCCESS"}) } }) } else { const mdtestfolder = "../MD_TEST/project/image/" image.mv(mdtestfolder + req.headers.uploadforproject + ".jpg", (error) => { if(error) { res.send({"result":"ERROR"}) } else { res.send({"result":"SUCCESS"}) } }) } } else { if(req.files.apic.mimetype == "image/png" | req.files.apic.mimetype == "image/jpeg" ) { var uploadPath = ""; var uploadDir = __dirname.replace("routes","public" + path.sep + "images" + path.sep + "userspics" + path.sep) if(req.files.apic.mimetype == "image/png") { uploadPath = ntr.checkToken(req, res).name + ".png" } if(req.files.apic.mimetype == "image/jpeg") { uploadPath = ntr.checkToken(req, res).name + ".jpg" } image.mv(__dirname.replace("routes", "") + path.sep + uploadPath, function(err) { if(err) { res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."}) console.log(err) } else { if(fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".png") | fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".jpg")) { if(fs.existsSync(uploadDir + ntr.checkToken(req, res).name + ".png")) { fs.rmSync(uploadDir + ntr.checkToken(req, res).name + ".png") } fs.renameSync(__dirname.replace("routes", "") + uploadPath , uploadDir + uploadPath) if(req.files.apic.mimetype == "image/jpeg") { Jimp.read(uploadDir + uploadPath, function (err, image) { image.resize(200,200, function(err){ if (err) { console.log(err) } }).write(uploadDir + uploadPath.replace("jpg", "png")) fs.rmSync(uploadDir + ntr.checkToken(req, res).name + ".jpg") res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) }) } else if(req.files.apic.mimetype == "image/png") { Jimp.read(uploadDir + uploadPath, function (err, image) { image.resize(200,200, function(err){ if (err) { console.log(err) } }).write(uploadDir + uploadPath) res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) }) } else { res.send({"result":"success", "content": "La photo de profil a été changé avec succès.",}) } } else { console.log(uploadDir + uploadPath) res.send({"result":"success", "content": "La photo de profil n'a pas pu être chargé."}) console.log("ERROR - UPLOAD NOT WORK CORRECYLT") } } }) } else { res.send({"result":"success", "content": "Seul les images (JPEG et PNG) sont acceptés."}) } } } }) module.exports = router;