-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
151 lines (140 loc) · 4.08 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var port = 8801;
var express = require('express');
var url = require('url');
var _ = require('underscore');
var fs = require('fs');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser({ keepExtensions: true, uploadDir: './tmp_upload' }));
var works = [];
var old_works = fs.readdirSync('work');
var old_results = fs.readdirSync('result');
_.each(old_works,function(old_work){
var match=old_work.match(/d+/);
if(match){
var id=m[0];
works[id]={
'id':id,
'state':'todo',
'path':'work/' + old_work,
'createdAt': Date.now(),
}
}
});
_.each(old_results,function(old_result){
var match=old_result.match(/d+/);
if(match){
var id=m[0];
if(0<=id && id < works.length){
works[id].state = 'done';
works[id].result_path = 'result/' + old_result;
works[id].finishedAt = Date.now();
}
}
});
app.param('work_id',function(req,res,next,work_id){
if(work_id<0 || works.length<=work_id){
res.status(404).json("No such work");
}else{
req.work = works[work_id];
next();
}
});
app.get('/',function(req,res){
res.type('text/html');
fs.readFile('template.jst', function (err, data) {
if(err){
res.json("Error: could not load template");
}else{
res.send(_.template(data.toString(),{works:works},{variable:'d'}));
}
});
});
app.get('/watch_work/:work_id',function(req,res){
res.type('application/json');
res.json(req.work);
});
app.get('/download_work/:work_id',function(req,res){
res.sendfile(req.work.path);
});
app.get('/download_result/:work_id',function(req,res){
if(req.work.status == "done"){
res.sendfile(req.work.result_path);
}else{
res.status(403).json("The work status is " + req.work.status + " so you can not see results");
}
});
app.get('/give_me_work',function(req,res){
var work = _.find(works,function(work){
return work.status=="todo";
});
if(work){
work.status = "assigned";
work.assignedAt = Date.now();
res.json(work);
}else{
res.json(null);
}
});
app.post('/solved/:work_id',function(req,res){
res.type('application/json');
var work = req.work;
if(work.status == "uploading"){
res.status(403).json("This job is being uploaded at the moment. Please retry in 60 seconds.");
}else if(work.status == "done"){
res.status(403).json("This job is already done, thank you.");
}else if(work.status == "assigned"){
var params = url.parse(req.url,true).query;
var uploaded_file_info = req.files.result;
work.status = 'uploading';
fs.readFile(uploaded_file_info.path, function (err, data) {
if(err){
work.status = "assigned";
res.json("Error: could not read uploaded file");
}else{
work.result_path = './result/' + work.id;
fs.writeFile(work.result_path, data, function (err) {
if(err){
work.status = "assigned";
res.json("Error: could not move file");
}else{
work.status = "done";
work.finishedAt = Date.now();
res.json(work);
}
});
}
});
}else{
res.status(403).json("Current status of the work is " + work.status + " so you can not upload it.");
}
});
app.post('/add_work',function(req,res){
res.type('application/json');
var params = url.parse(req.url,true).query;
var uploaded_file_info = req.files.work;
var id = works.length;
var work = {
id: id,
path: "./work/" + id,
status: "todo",
createdAt : Date.now(),
};
works.push(work);
fs.readFile(uploaded_file_info.path, function (err, data) {
if(err){
res.json("Error: could not read uploaded file");
}else{
fs.writeFile(work.path, data, function (err) {
if(err){
res.json("Error: could not move file");
}else{
res.json(work);
console.log("Enqueued work #" + work.id);
}
});
}
});
});
app.listen(port);
console.log("Listening on port " + port);