From 2d48bb165a6b9911f7ab5cfcd26e6cdc4e1532a8 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 10 Mar 2018 23:41:10 +0900 Subject: [PATCH] Improve interface for command running --- index.js | 54 ++++++++++-------------- views/cmd.handlebars | 10 ++++- views/partials/dialogue-cmd.handlebars | 2 +- views/partials/dialogue-mkdir.handlebars | 2 +- views/partials/toolbar.handlebars | 2 + 5 files changed, 34 insertions(+), 36 deletions(-) mode change 100644 => 100755 index.js diff --git a/index.js b/index.js old mode 100644 new mode 100755 index d94ac46..558184a --- a/index.js +++ b/index.js @@ -425,11 +425,13 @@ app.get("/*", (req, res) => { Promise.all(promises).then((files) => { res.render("list", flashify(req, { + shellable: shellable, path: res.filename, files: files, })); }).catch((err) => { res.render("list", flashify(req, { + shellable: shellable, path: res.filename, errors: [ err @@ -438,6 +440,7 @@ app.get("/*", (req, res) => { }); }).catch((err) => { res.render("list", flashify(req, { + shellable: shellable, path: res.filename, errors: [ err @@ -452,10 +455,12 @@ app.get("/*", (req, res) => { // shell -const shellable = process.env.ENABLE_SHELL ? true : false; +const shellable = process.env.ENABLE_SHELL == "true" ? true : false; if (shellable) { - app.run("/*@cmd", (req, res) => { + const child_process = require("child_process"); + + app.post("/*@cmd", (req, res) => { res.filename = req.params[0]; let cmd = req.body.cmd; @@ -463,36 +468,21 @@ if (shellable) { return res.status(400).end(); } - let fileExists = new Promise((resolve, reject) => { - // Check if file exists - fs.stat(res.filename, (err, stats) => { - if (err || !stats.isDirectory()) { - return reject(err); - } - return resolve(stats); - }); - }); - - fileExists.then((stats) => { - child_process.exec(cmd, { - cwd: res.filename, - timeout: 60 * 1000, - }, (err, stdout, stderr) => { - if (err) { - req.flash("error", "Unable to execute command " + cmd); - res.redirect("back"); - return; - } - res.render("cmd", flashify(req, { - path: res.filename, - cmd: cmd, - stdout: stdout, - stderr: stderr, - })); - }); - }).catch((err) => { - req.flash("error", "Folder to use as working directory doesn't exist."); - res.redirect("back"); + child_process.exec(cmd, { + cwd: res.filename, + timeout: 60 * 1000, + }, (err, stdout, stderr) => { + if (err) { + req.flash("error", "Unable to execute command " + cmd); + res.redirect("back"); + return; + } + res.render("cmd", flashify(req, { + path: res.filename, + cmd: cmd, + stdout: stdout, + stderr: stderr, + })); }); }); } diff --git a/views/cmd.handlebars b/views/cmd.handlebars index 85ab59d..ea29c4a 100644 --- a/views/cmd.handlebars +++ b/views/cmd.handlebars @@ -22,9 +22,15 @@ + +{{> dialogue-cmd}} diff --git a/views/partials/dialogue-cmd.handlebars b/views/partials/dialogue-cmd.handlebars index b3e2458..8b5e712 100644 --- a/views/partials/dialogue-cmd.handlebars +++ b/views/partials/dialogue-cmd.handlebars @@ -11,7 +11,7 @@