neutral-old/routes/upload.js
2023-02-22 13:57:27 +01:00

240 lines
8.4 KiB
JavaScript

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");
/* 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(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) throw err;
}).write(uploadDir + uploadPath.replace("jpg", "png"))
fs.rmSync(uploadDir + req.headers.uploadforuser + ".jpg")
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
})
} else if(req.files.apic.mimetype == "image/png") {
Jimp.read(uploadDir + uploadPath, function (err, image) {
image.resize(200,200, function(err){
if (err) throw err;
}).write(uploadDir + uploadPath)
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
})
} else {
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
}
} 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.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(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) throw err;
}).write(uploadDir + uploadPath.replace("jpg", "png"))
fs.rmSync(uploadDir + ntr.checkToken(req, res).name + ".jpg")
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
})
} else if(req.files.apic.mimetype == "image/png") {
Jimp.read(uploadDir + uploadPath, function (err, image) {
image.resize(200,200, function(err){
if (err) throw err;
}).write(uploadDir + uploadPath)
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
})
} else {
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>La photo de profil a été changé avec succès.</span>",})
}
} 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;