From eb364a0ce69192fea09c0f48a9652d7ede537b3f Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Thu, 28 Mar 2019 16:16:51 +0100 Subject: [PATCH] So much better now --- package-lock.json | 5 ++ package.json | 1 + server.js | 65 ++++++++++++++----- views/{base.pug => html/base_html.pug} | 0 views/{battles.pug => html/battles_html.pug} | 0 views/{error.pug => html/error_html.pug} | 2 +- views/{index.pug => html/index_html.pug} | 2 +- .../leaderboard_html.pug} | 0 views/{upload.pug => html/upload_html.pug} | 2 +- views/txt/base_txt.pug | 0 views/txt/error_txt.pug | 2 + 11 files changed, 58 insertions(+), 21 deletions(-) rename views/{base.pug => html/base_html.pug} (100%) rename views/{battles.pug => html/battles_html.pug} (100%) rename views/{error.pug => html/error_html.pug} (90%) rename views/{index.pug => html/index_html.pug} (98%) rename views/{leaderboard.pug => html/leaderboard_html.pug} (100%) rename views/{upload.pug => html/upload_html.pug} (99%) create mode 100644 views/txt/base_txt.pug create mode 100644 views/txt/error_txt.pug diff --git a/package-lock.json b/package-lock.json index 0386cfc..b5776df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -361,6 +361,11 @@ "busboy": "^0.2.14" } }, + "express-useragent": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/express-useragent/-/express-useragent-1.0.12.tgz", + "integrity": "sha1-W64BCakl7Js1QX8xpOitE/GRJTo=" + }, "finalhandler": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz", diff --git a/package.json b/package.json index f696d06..9d5aa64 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "bcryptjs": "^2.4.3", "express": "^4.16.4", "express-fileupload": "^1.1.3-alpha.1", + "express-useragent": "^1.0.12", "pug": "^2.0.3", "touch": "^3.1.0", "unzip": "^0.1.11" diff --git a/server.js b/server.js index e85ae7d..a94546a 100644 --- a/server.js +++ b/server.js @@ -9,6 +9,7 @@ const pug = require('pug'); const unzip = require('unzip'); const touch = require('touch'); const bcrypt = require('bcryptjs'); +const useragent = require('express-useragent'); const { port, pythonPath, aisPath, aisPathOld, hashPath } = require('./config'); // Create the directories to store the files @@ -141,7 +142,8 @@ function readData(req, res, callback) { fs.readFile('pytron_run/assets/data.json', 'utf-8', (err, data) => { if (err != null) { console.log(err); - res.render('error', {message: 'It seems like the site is broken :\'('}); + res.status(400); + render(req, res, 'error', {message: 'It seems like the site is broken :\'('}); return; } let parsed = parse(data); @@ -161,7 +163,8 @@ function saveArchiveAndRun(req, res) { if (err != null) { console.log("Failed to save archive"); console.log(err); - res.render('error', {message: 'Unable to save the ZIP archive to the server'}); + res.status(400); + render(req, res, 'error', {message: 'Unable to save the ZIP archive to the server'}); return; } fs.createReadStream(zipfile) @@ -171,23 +174,36 @@ function saveArchiveAndRun(req, res) { touch(pathtools.join(path, '__init__.py'), () => { // Trigger python_run runPython(); - res.redirect('/'); + if (req.useragent.isCurl) { + res.send('Success'); + } else { + res.redirect('/'); + } }); }) .on('error', () => { - res.render('error', {message: 'Failed to unzip the archive'}); + render(req, res, 'error', {message: 'Failed to unzip the archive'}); }); }); } +function render(req, res, template, context) { + if (req.useragent.isCurl) { + res.render("txt/" + template + "_txt", context); + } else { + res.render("html/" + template + "_html", context); + } +} + function startServer() { const app = express(); app.set('view engine', 'pug'); app.use(fileUpload()); + app.use(useragent.express()); app.get('/', (req, res) => { readData(req, res, (parsed) => { - res.render('index', { + render(req, res, 'index', { data: parsed, running: pythonRunning, }); @@ -204,7 +220,7 @@ function startServer() { app.get('/leaderboard', (req, res) => { readData(req, res, (parsed) => { - res.render('leaderboard', { + render(req, res, 'leaderboard', { data: parsed, running: pythonRunning, }); @@ -213,7 +229,7 @@ function startServer() { app.get('/battles', (req, res) => { readData(req, res, (parsed) => { - res.render('battles', { + render(req, res, 'battles', { data: parsed, running: pythonRunning, }); @@ -221,28 +237,37 @@ function startServer() { }); app.get('/upload', (req, res) => { - res.render('upload', {}); + render(req, res, 'upload', {}); }); app.post('/upload-target', (req, res) => { console.log("/upload-target"); + if (!req.body) { + res.status(400); + render(req, res, 'error', {message: "You have to send the form"}); + } + if (!req.body.name) { - res.render('error', {message: "You have to enter a name in the form"}); + res.status(400); + render(req, res, 'error', {message: "You have to enter a name in the form"}); return; } if (!req.body.password) { - res.render('error', {message: "You have to enter a password in the form"}); + res.status(400); + render(req, res, 'error', {message: "You have to enter a password in the form"}); } if (req.body.name.indexOf('.') !== -1) { - res.render('error', {message: "The name of your AI can't contain dots"}); + res.status(400); + render(req, res, 'error', {message: "The name of your AI can't contain dots"}); } if (!req.files.archive) { - res.render('error', {message: "You have to send a ZIP archive"}); + res.status(400); + render(req, res, 'error', {message: "You have to send a ZIP archive"}); return; } @@ -256,7 +281,6 @@ function startServer() { fs.mkdirSync(path); } - if (aiExisted) { console.log("Ai existed, try to verify password"); @@ -264,7 +288,8 @@ function startServer() { fs.readFile(pathtools.join(path, hashPath), 'utf-8', function(err, data) { if (err != null) { - res.render('error', {message: "Couldn't read hashed password"}); + res.status(400); + render(req, res, 'error', {message: "Couldn't read hashed password"}); console.log(err); return; } @@ -272,13 +297,15 @@ function startServer() { // If the AI already existed, verify the password bcrypt.compare(req.body.password, data, function(err, success) { if (err != null) { - res.render('error', {message: "Couldn't compare password"}); + res.status(400); + render(req, res, 'error', {message: "Couldn't compare password"}); return; } if (!success) { console.log("Authentication failed"); - res.render('error', {message: "Authentication failed"}); + res.status(400); + render(req, res, 'error', {message: "Authentication failed"}); return; } @@ -331,7 +358,8 @@ function startServer() { if (err != null) { console.log(err); - res.render('error', {message: "Couldn't hash password"}); + res.status(400); + render(req, res, 'error', {message: "Couldn't hash password"}); return; } @@ -342,7 +370,8 @@ function startServer() { if (err != null) { console.log(err); - res.render('error', {message: "Couldn't save hashed password"}); + res.status(400); + render(req, res, 'error', {message: "Couldn't save hashed password"}); return; } diff --git a/views/base.pug b/views/html/base_html.pug similarity index 100% rename from views/base.pug rename to views/html/base_html.pug diff --git a/views/battles.pug b/views/html/battles_html.pug similarity index 100% rename from views/battles.pug rename to views/html/battles_html.pug diff --git a/views/error.pug b/views/html/error_html.pug similarity index 90% rename from views/error.pug rename to views/html/error_html.pug index 0eaf81d..46b73ae 100644 --- a/views/error.pug +++ b/views/html/error_html.pug @@ -1,4 +1,4 @@ -extends base +extends base_html block content section.section diff --git a/views/index.pug b/views/html/index_html.pug similarity index 98% rename from views/index.pug rename to views/html/index_html.pug index e65783f..4344a7b 100644 --- a/views/index.pug +++ b/views/html/index_html.pug @@ -1,4 +1,4 @@ -extends base +extends base_html block extracss link(rel="stylesheet", href="https://use.fontawesome.com/releases/v5.8.1/css/all.css", integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf", crossorigin="anonymous") diff --git a/views/leaderboard.pug b/views/html/leaderboard_html.pug similarity index 100% rename from views/leaderboard.pug rename to views/html/leaderboard_html.pug diff --git a/views/upload.pug b/views/html/upload_html.pug similarity index 99% rename from views/upload.pug rename to views/html/upload_html.pug index 4eced28..7448262 100644 --- a/views/upload.pug +++ b/views/html/upload_html.pug @@ -1,4 +1,4 @@ -extends base +extends base_html block content section.section diff --git a/views/txt/base_txt.pug b/views/txt/base_txt.pug new file mode 100644 index 0000000..e69de29 diff --git a/views/txt/error_txt.pug b/views/txt/error_txt.pug new file mode 100644 index 0000000..e283057 --- /dev/null +++ b/views/txt/error_txt.pug @@ -0,0 +1,2 @@ +| #{message} +|