mirror of
https://github.com/TheGreyDiamond/elevatormapRewritten.git
synced 2025-12-16 23:10:45 +01:00
Images are now uploaded and the paths saved to DB
This commit is contained in:
71
index.js
71
index.js
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,38 +218,40 @@
|
|||||||
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)
|
||||||
});
|
document.getElementById("imageUploadInfo").style.display = 'block';
|
||||||
console.log($('#myFile'))
|
var filesToSend = $('#myFile').prop('files').length;
|
||||||
document.getElementById("imageUploadInfo").style.display = 'block';
|
var i = 0;
|
||||||
var filesToSend = $('#myFile').prop('files').length;
|
while (i < filesToSend) {
|
||||||
var i = 0;
|
document.getElementById("imageUploadInfo").innerHTML = "Uploading image " + String(i) + "/" + String(filesToSend)
|
||||||
while (i < filesToSend) {
|
console.log("Files left to send: ", filesToSend - i)
|
||||||
document.getElementById("imageUploadInfo").innerHTML = "Uploading image " + String(i) + "/" + String(filesToSend)
|
var file_data = $('#myFile').prop('files')[i];
|
||||||
console.log("Files left to send: ", filesToSend - i)
|
var form_data = new FormData();
|
||||||
var file_data = $('#myFile').prop('files')[i];
|
form_data.append('file', file_data);
|
||||||
var form_data = new FormData();
|
|
||||||
form_data.append('file', file_data);
|
|
||||||
|
|
||||||
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,
|
||||||
processData: false,
|
processData: false,
|
||||||
data: form_data,
|
data: form_data,
|
||||||
type: 'post',
|
type: 'post',
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
alert(data);
|
alert(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("Skipping nonimage file")
|
||||||
}
|
}
|
||||||
});
|
i++;
|
||||||
} else {
|
}
|
||||||
console.log("Skipping nonimage file")
|
});
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user