2016-07-04 20:59:07 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-07-05 18:06:22 +00:00
|
|
|
let fs = require('fs');
|
2016-07-08 20:44:48 +00:00
|
|
|
let async = require('async');
|
|
|
|
|
2016-07-04 20:59:07 +00:00
|
|
|
let express = require('express');
|
|
|
|
let app = express();
|
|
|
|
|
2016-07-08 20:44:48 +00:00
|
|
|
let addRequestId = require('./libs/expressRequestId')();
|
2016-07-05 18:06:22 +00:00
|
|
|
let multer = require('multer');
|
2016-07-07 22:07:17 +00:00
|
|
|
let bodyParser = require('body-parser');
|
2016-07-08 20:44:48 +00:00
|
|
|
let morgan = require('morgan');
|
2016-07-05 18:06:22 +00:00
|
|
|
|
2016-07-06 18:44:20 +00:00
|
|
|
let taskManager = new (require('./libs/taskManager'))();
|
|
|
|
let Task = require('./libs/Task');
|
|
|
|
|
2016-07-08 20:44:48 +00:00
|
|
|
app.use(morgan('tiny'));
|
2016-07-07 22:07:17 +00:00
|
|
|
app.use(bodyParser.urlencoded({extended: true}));
|
|
|
|
app.use(bodyParser.json());
|
2016-07-05 18:06:22 +00:00
|
|
|
app.use(express.static('public'));
|
|
|
|
|
|
|
|
let upload = multer({
|
|
|
|
storage: multer.diskStorage({
|
|
|
|
destination: (req, file, cb) => {
|
|
|
|
let path = `tmp/${req.id}/`;
|
|
|
|
fs.exists(path, exists => {
|
|
|
|
if (!exists){
|
|
|
|
fs.mkdir(path, undefined, () => {
|
|
|
|
cb(null, path);
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
cb(null, path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
cb(null, file.originalname)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2016-07-08 20:44:48 +00:00
|
|
|
|
2016-07-09 16:58:14 +00:00
|
|
|
app.post('/task/new', addRequestId, upload.array('images'), (req, res) => {
|
2016-07-06 18:44:20 +00:00
|
|
|
if (req.files.length === 0) res.json({error: "Need at least 1 file."});
|
|
|
|
else{
|
|
|
|
// Move to data
|
2016-07-08 20:44:48 +00:00
|
|
|
async.series([
|
|
|
|
cb => {
|
|
|
|
fs.stat(`data/${req.id}`, (err, stat) => {
|
|
|
|
if (err && err.code === 'ENOENT') cb();
|
|
|
|
else cb(new Error(`Directory exists (should not have happened: ${err.code})`));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
cb => { fs.mkdir(`data/${req.id}`, undefined, cb); },
|
|
|
|
cb => {
|
|
|
|
fs.rename(`tmp/${req.id}`, `data/${req.id}/images`, err => {
|
|
|
|
if (!err){
|
|
|
|
new Task(req.id, req.body.name, (err, task) => {
|
|
|
|
if (err) cb(err);
|
|
|
|
else{
|
|
|
|
taskManager.addNew(task);
|
|
|
|
res.json({uuid: req.id, success: true});
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
cb(new Error("Could not move images folder."))
|
2016-07-07 22:07:17 +00:00
|
|
|
}
|
|
|
|
});
|
2016-07-06 18:44:20 +00:00
|
|
|
}
|
2016-07-08 20:44:48 +00:00
|
|
|
], err => {
|
|
|
|
if (err) res.json({error: err.message})
|
2016-07-06 18:44:20 +00:00
|
|
|
});
|
2016-07-05 18:06:22 +00:00
|
|
|
}
|
2016-07-06 18:44:20 +00:00
|
|
|
});
|
|
|
|
|
2016-07-08 20:44:48 +00:00
|
|
|
let getTaskFromUuid = (req, res, next) => {
|
2016-07-06 18:44:20 +00:00
|
|
|
let task = taskManager.find(req.params.uuid);
|
|
|
|
if (task){
|
2016-07-08 20:44:48 +00:00
|
|
|
req.task = task;
|
|
|
|
next();
|
2016-07-06 18:44:20 +00:00
|
|
|
}else res.json({error: `${req.params.uuid} not found`});
|
2016-07-08 20:44:48 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 16:58:14 +00:00
|
|
|
app.get('/task/:uuid/info', getTaskFromUuid, (req, res) => {
|
2016-07-08 20:44:48 +00:00
|
|
|
res.json(req.task.getInfo());
|
|
|
|
});
|
2016-07-09 16:58:14 +00:00
|
|
|
app.get('/task/:uuid/output', getTaskFromUuid, (req, res) => {
|
|
|
|
res.json(req.task.getOutput(req.query.line));
|
2016-07-04 20:59:07 +00:00
|
|
|
});
|
|
|
|
|
2016-07-07 22:07:17 +00:00
|
|
|
let uuidCheck = (req, res, next) => {
|
|
|
|
if (!req.body.uuid) res.json({error: "uuid param missing."});
|
|
|
|
else next();
|
|
|
|
};
|
|
|
|
|
|
|
|
let successHandler = res => {
|
|
|
|
return err => {
|
|
|
|
if (!err) res.json({success: true});
|
|
|
|
else res.json({error: err.message});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-07-09 16:58:14 +00:00
|
|
|
app.post('/task/cancel', uuidCheck, (req, res) => {
|
2016-07-07 22:07:17 +00:00
|
|
|
taskManager.cancel(req.body.uuid, successHandler(res));
|
|
|
|
});
|
|
|
|
|
2016-07-09 16:58:14 +00:00
|
|
|
app.post('/task/remove', uuidCheck, (req, res) => {
|
2016-07-07 22:07:17 +00:00
|
|
|
taskManager.remove(req.body.uuid, successHandler(res));
|
|
|
|
});
|
|
|
|
|
2016-07-09 16:58:14 +00:00
|
|
|
app.post('/task/restart', uuidCheck, (req, res) => {
|
2016-07-07 22:07:17 +00:00
|
|
|
taskManager.restart(req.body.uuid, successHandler(res));
|
|
|
|
});
|
|
|
|
|
2016-07-04 20:59:07 +00:00
|
|
|
app.listen(3000, () => {
|
2016-07-08 20:44:48 +00:00
|
|
|
console.log('Server has started on port 3000');
|
2016-07-04 20:59:07 +00:00
|
|
|
});
|