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) => { Promise.all(promises).then((files) => {
res.render("list", flashify(req, { res.render("list", flashify(req, {
shellable: shellable,
path: res.filename, path: res.filename,
files: files, files: files,
})); }));
}).catch((err) => { }).catch((err) => {
res.render("list", flashify(req, { res.render("list", flashify(req, {
shellable: shellable,
path: res.filename, path: res.filename,
errors: [ errors: [
err err
@ -438,6 +440,7 @@ app.get("/*", (req, res) => {
}); });
}).catch((err) => { }).catch((err) => {
res.render("list", flashify(req, { res.render("list", flashify(req, {
shellable: shellable,
path: res.filename, path: res.filename,
errors: [ errors: [
err err
@ -452,10 +455,12 @@ app.get("/*", (req, res) => {
// shell // shell
const shellable = process.env.ENABLE_SHELL ? true : false; const shellable = process.env.ENABLE_SHELL == "true" ? true : false;
if (shellable) { if (shellable) {
app.run("/*@cmd", (req, res) => { const child_process = require("child_process");
app.post("/*@cmd", (req, res) => {
res.filename = req.params[0]; res.filename = req.params[0];
let cmd = req.body.cmd; let cmd = req.body.cmd;
@ -463,36 +468,21 @@ if (shellable) {
return res.status(400).end(); return res.status(400).end();
} }
let fileExists = new Promise((resolve, reject) => { child_process.exec(cmd, {
// Check if file exists cwd: res.filename,
fs.stat(res.filename, (err, stats) => { timeout: 60 * 1000,
if (err || !stats.isDirectory()) { }, (err, stdout, stderr) => {
return reject(err); if (err) {
} req.flash("error", "Unable to execute command " + cmd);
return resolve(stats); res.redirect("back");
}); return;
}); }
res.render("cmd", flashify(req, {
fileExists.then((stats) => { path: res.filename,
child_process.exec(cmd, { cmd: cmd,
cwd: res.filename, stdout: stdout,
timeout: 60 * 1000, stderr: stderr,
}, (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");
}); });
}); });
} }

View File

@ -22,9 +22,15 @@
<nav class="navbar navbar-light bg-light fixed-bottom justify-content-between"> <nav class="navbar navbar-light bg-light fixed-bottom justify-content-between">
<div class="btn-group mr-auto" role="group"> <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> <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> </a>
</div> </div>
</nav> </nav>
{{> dialogue-cmd}}

View File

@ -11,7 +11,7 @@
<div class="modal-body"> <div class="modal-body">
<div class="form-group"> <div class="form-group">
<label for="cmd-cmd">Command: </label> <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> </div>
<div class="modal-footer"> <div class="modal-footer">

View File

@ -11,7 +11,7 @@
<div class="modal-body"> <div class="modal-body">
<div class="form-group"> <div class="form-group">
<label for="mkdir-folder">Folder name: </label> <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> </div>
<div class="modal-footer"> <div class="modal-footer">

View File

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