Skip to content

Commit c4f6a52

Browse files
author
Toni Isotalo
committed
Merge remote-tracking branch 'origin/bot-editing'
2 parents fb4fe22 + c1e1d1c commit c4f6a52

File tree

15 files changed

+416
-2415
lines changed

15 files changed

+416
-2415
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules/
22
cypress.env.json
33
*/cypress/videos/
44
*/cypress/downloads/
5-
*/cypress/screenshots/
5+
*/cypress/screenshots/
6+
data/bot_pictures/

app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const http = require("http");
44
const express = require("express")
55
const app = express()
66
const dataLimit = "50mb"
7+
const constants = require("./modules/constants")
78
app.use(express.json({limit: dataLimit}))
89
app.use(express.urlencoded({limit: dataLimit}))
910

@@ -19,6 +20,9 @@ app.get("/favicon.ico", function(req, res) {
1920
})
2021
app.use("", indexRouters);
2122

23+
app.get(constants.botPictureAccessPath + "/:img", (req, res) => {
24+
res.sendFile(constants.botPicturesPath + "/" + req.params["img"])
25+
})
2226
app.get("/static/*", (req, res) => {
2327
res.sendFile(__dirname + "/src/" + req.originalUrl)
2428
})

modules/api.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const db = require("./db")
22
const constants = require("./constants");
3+
const fs = require("fs")
34
let getBot = (id, callback, error) => {
45
db.query("SELECT * FROM bots WHERE id='" + id + "'", (result) => {
56
callback(result[0])
@@ -27,17 +28,50 @@ let saveBot = (owner, name, data, callback, error) => {
2728
function buildUpdateParams(dict) {
2829
let sql = "";
2930
for(key in dict) {
31+
if(key == "public" && dict[key] == "true") {
32+
dict[key] = 1
33+
}
34+
if(key == "public" && dict[key] == "false") {
35+
dict[key] = 0
36+
}
3037
sql += key + "='" + dict[key] + "',";
3138
}
3239
return sql.substring(0, sql.length - 1);;
3340
}
3441

3542
let updateBot = (id, data, callback, error=() => {}) => {
36-
const sql = buildUpdateParams(data);
37-
db.query("UPDATE bots SET " + sql + " WHERE id=" + id, (result) => {
43+
function insertSql() {
44+
const sql = buildUpdateParams(data);
45+
db.query("UPDATE bots SET " + sql + " WHERE id=" + id, (result) => {
46+
callback(result);
47+
}, (err) => {
48+
error(err);
49+
})
50+
}
51+
52+
if("picture" in data && typeof data["picture"] == "object" && typeof data["picture"]["data"] == "object") {
53+
//Image
54+
let imageType = data["picture"]["mimetype"].split("/")[1];
55+
const imagePath = constants.botPicturesPath + "/" + id + "." + imageType
56+
fs.writeFile(imagePath, data["picture"]["data"], "binary", (err) => {
57+
if(err) {
58+
error(err);
59+
return
60+
} else {
61+
data["picture"] = constants.botPictureAccessPath + "/" + id + "." + imageType;
62+
insertSql();
63+
}
64+
});
65+
} else {
66+
insertSql();
67+
}
68+
}
69+
70+
let deleteBot = (id, callback, error=()=>{}) => {
71+
db.query("DELETE FROM bots WHERE id=" + id, (result) => {
3872
callback(result);
3973
}, (err) => {
40-
error(err);
74+
error(err)
4175
})
4276
}
4377

@@ -61,4 +95,5 @@ exports.getMethods = getMethods
6195
exports.getBot = getBot;
6296
exports.saveBot = saveBot;
6397
exports.updateBot = updateBot;
98+
exports.deleteBot = deleteBot;
6499
exports.getBotsByUserId = getBotsByUserId

modules/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
module.exports.defaultAvatar = "/static/images/avatar.jpg"
1+
2+
module.exports.defaultAvatar = "/static/images/avatar.jpg"
3+
module.exports.botPicturesPath = require("./parentUrl") + "/data/bot_pictures"
4+
module.exports.botPictureAccessPath = "/static/images/bots"

modules/routers/apiRouter.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const api = require("../api");
66
const responses = require("../responses");
77
const auth = require("../auth");
88
const common = require("../common");
9-
9+
const fileUpload = require('express-fileupload')
10+
const constants = require("../constants");
1011
router.get("/bot/:id", (req, res) => {
1112
//Get bot by id
1213
const id = req.params.id;
@@ -85,8 +86,12 @@ router.post("/bot", function(req, res) {
8586
responses.internal_server_error(res);
8687
})
8788
})
88-
89+
router.use(fileUpload());
8990
router.put("/bot/:id", function(req, res) {
91+
for(key in req.files) {
92+
//Add files to the body for easier processing
93+
req.body[key] = req.files[key];
94+
}
9095
const id = req.params.id;
9196
db.getCols("bots", (result) => {
9297
for(key in req.body) {
@@ -106,13 +111,20 @@ router.put("/bot/:id", function(req, res) {
106111
return;
107112
}
108113
api.updateBot(id, req.body, (re) => {
109-
responses.ok(res);
114+
responses.ok(res, req.body);
110115
return;
111116
}, (err) => {
112117
responses.internal_server_error(res);
113118
})
114119
})
115120
})
116121
})
122+
router.delete("/bot/:id", function(req, res) {
123+
api.deleteBot(req.params.id, function(result) {
124+
responses.ok(res)
125+
}, (err) => {
126+
responses.internal_server_error(res);
127+
})
128+
})
117129

118130
module.exports = router

0 commit comments

Comments
 (0)