1
0
Fork 0

Improve interface for command running

pull/2/head
Ambrose Chua 2018-03-10 23:41:10 +09:00
parent 5209b2b8ee
commit 2d48bb165a
5 changed files with 34 additions and 36 deletions

54
index.js Normal file → Executable file
View File

@ -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,
}));
});
});
}

View File

@ -22,9 +22,15 @@
<nav class="navbar navbar-light bg-light fixed-bottom justify-content-between">
<div class="btn-group mr-auto" role="group">
<a class="btn btn-primary" href="{{path}}">
<a class="btn btn-primary" href="/{{path}}">
<span class="octicon octicon-chevron-left"></span>
Back
Back to file list
</a>
<a class="btn btn-warning" href="@cmd" data-toggle="modal" data-target="#cmd">
<span class="octicon octicon-terminal"></span>
Run command
</a>
</div>
</nav>
{{> dialogue-cmd}}

View File

@ -11,7 +11,7 @@
<div class="modal-body">
<div class="form-group">
<label for="cmd-cmd">Command: </label>
<input name="cmd" class="form-control" type="text" id="cmd-cmd" placeholder="g++ sort.c" />
<input name="cmd" class="form-control" type="text" id="cmd-cmd" placeholder="g++ sort.c" required />
</div>
</div>
<div class="modal-footer">

View File

@ -11,7 +11,7 @@
<div class="modal-body">
<div class="form-group">
<label for="mkdir-folder">Folder name: </label>
<input name="folder" class="form-control" type="text" id="mkdir-folder" placeholder="folder" />
<input name="folder" class="form-control" type="text" id="mkdir-folder" placeholder="folder" required />
</div>
</div>
<div class="modal-footer">

View File

@ -8,10 +8,12 @@
<span class="octicon octicon-file-directory"></span>
New folder
</a>
{{#if shellable}}
<a class="btn btn-warning" href="@cmd" data-toggle="modal" data-target="#cmd">
<span class="octicon octicon-terminal"></span>
Run command
</a>
{{/if}}
</div>
<div class="btn-group mr-2" role="group">
<a class="btn btn-success" href="@download" data-toggle="modal" data-target="#download">