Images are now uploaded and the paths saved to DB

This commit is contained in:
TheGreyDiamond
2021-06-18 19:40:48 +02:00
parent f99a1808b0
commit 5f7385a1b6
2 changed files with 96 additions and 37 deletions

View File

@@ -12,6 +12,7 @@ const session = require("express-session");
const nodemailer = require("nodemailer"); const nodemailer = require("nodemailer");
const crypto = require("crypto"); const crypto = require("crypto");
const multer = require("multer"); const multer = require("multer");
const path = require("path");
const upload = multer({ dest: "static/uploads/" }); const upload = multer({ dest: "static/uploads/" });
@@ -859,10 +860,66 @@ app.get("/api/getElevators", function (req, res) {
}); });
app.post("/api/uploadImage", upload.any(), function (req, res) { app.post("/api/uploadImage", upload.any(), function (req, res) {
//TODO: Fix file ending, add image to elevator in DB //TODO: Fix file ending, add image to elevator in DBw
console.log("UPLOAD"); console.log(req.query.id)
console.log(req.files); i = 0;
req.files[0]; const sql = 'SELECT id, images FROM elevators WHERE id=?';
// req.query.id
allImages = []
while (i < req.files.length) {
fObj = req.files[i];
const currentPath = path.join(fObj["path"]);
const destinationPath =
currentPath +
"." +
fObj["originalname"].split(".")[
fObj["originalname"].split(".").length - 1
]; // Add the file end
fs.rename(currentPath, destinationPath, function (err) {
if (err) {
throw err;
} else {
console.log("Successfully moved the file!");
}
});
allImages.push({"path": destinationPath, "alt": "No alt was provided."})
i++;
}
con.query(
sql, [req.query.id],
function (err, result, fields) {
if (err) {
res.status(500);
res.send(
JSON.stringify({
state: "Failed",
message: "A server side error occured.",
results: [],
})
);
logger.error("The server failed to execute a request");
mysqlIsUpAndOkay = false;
} else {
jData = JSON.parse(result[0].images)
console.log(jData)
jData.images.push.apply(jData.images, allImages)
console.log(jData);
console.log(result);
var sql = "UPDATE elevators SET images = ? WHERE id = ?";
con.query(sql, [JSON.stringify(jData), req.query.id], function(err, result, fields){
if (err) {
console.log("Update failure")
}else{
console.log("Okay")
}
})
}
}
);
// Save Image End // Save Image End
}); });
@@ -886,7 +943,7 @@ app.post("/api/saveNewElevatorMeta", function (req, res) {
tempJs = JSON.parse(decodeURIComponent(getAppCookies(req, res)["tempStore"])); tempJs = JSON.parse(decodeURIComponent(getAppCookies(req, res)["tempStore"]));
console.log(tempJs); console.log(tempJs);
const sql = const sql =
"#INSERT INTO elevators (lat, lng, manufacturer, modell, info, visitabilty, technology, amountOfFloors, maxPassangers, maxWeight) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; "INSERT INTO elevators (lat, lng, manufacturer, modell, info, visitabilty, technology, amountOfFloors, maxPassangers, maxWeight, images) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '{ \"images\": []}')";
con.query( con.query(
sql, sql,
[ [
@@ -903,11 +960,11 @@ app.post("/api/saveNewElevatorMeta", function (req, res) {
], ],
function (err, result) { function (err, result) {
if (err) throw err; if (err) throw err;
console.log("1 record inserted"); console.log("1 record inserted with id " + result.insertId);
res.setHeader("Content-Type", "application/json"); res.setHeader("Content-Type", "application/json");
res.send( res.send(
JSON.stringify({ state: "Okay", message: "Ok.", id: res.insertId }) JSON.stringify({ state: "Okay", message: "Ok. No fault!", id: result.insertId })
); );
res.status(200); res.status(200);
} }

View File

@@ -218,9 +218,7 @@
fetch('/api/saveNewElevatorMeta', options) fetch('/api/saveNewElevatorMeta', options)
.then(response => response.json()) .then(response => response.json())
.then(response => { .then(response => {
console.log(response) console.warn("!!!!!!!!!!!!", response)
});
console.log($('#myFile'))
document.getElementById("imageUploadInfo").style.display = 'block'; document.getElementById("imageUploadInfo").style.display = 'block';
var filesToSend = $('#myFile').prop('files').length; var filesToSend = $('#myFile').prop('files').length;
var i = 0; var i = 0;
@@ -234,7 +232,7 @@
console.log(file_data) console.log(file_data)
if (String(file_data.type).includes("image/")) { if (String(file_data.type).includes("image/")) {
$.ajax({ $.ajax({
url: '/api/uploadImage', url: '/api/uploadImage?id=' + response.id,
dataType: 'json', dataType: 'json',
cache: false, cache: false,
contentType: false, contentType: false,
@@ -250,6 +248,10 @@
} }
i++; i++;
} }
});