-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
151 lines (128 loc) · 4.66 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 express = require('express');
var crypto = require('crypto');
var azure = require('azure-storage');
var tableJob = require('./azure-table.js');
var queueJob = require('./azure-queue.js');
var INPUT_CONTAINER = 'distart-input';
var OUTPUT_CONTAINER = 'distart-output';
var app = express();
var port = process.env.PORT || 1337;
var bodyParser = require('body-parser');
var blobSvc = azure.createBlobService();
app.get('/create', createSession);
app.post('/content/:token', postContent);
app.post('/style/:token', postStyle);
app.get('/get/:token', getImage);
app.get('/status/:token', getStatus);
app.get('/start/:token/:size', start);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
console.log("started");
app.listen(port);
function createSession(req, res){
createToken(function(token){
var job = {
jobID: token,
status: 'INCOMPLETE'
};
tableJob.updateJob(job, function(error, result, response){
console.log(result + " "+ response);
if(!error){
res.status(200).send(token);
} else{
res.status(400).send(error);
}
});
});
}
function postContent(req, res){
//save the image into the queue at the right job
var token = req.params.token;
console.log("postContent token: " + token);
var blobService = azure.createBlobService();
blobService.createBlockBlobFromStream (INPUT_CONTAINER, token + '_1', req, req.headers["content-length"], function(error, result, response) {
if(!error){
console.log('blob stored: '+result + " "+response);
tableJob.updateJobProperty(token, 'imageBlobName', token + '_1', function(error, result, response){
console.log('Job property updated');
res.status(200).send('OK: '+result + " "+response);
});
} else{
res.status(400).send(error);
}
});
}
function postStyle(req, res){
//save the style into the queue at the right job
var token = req.params.token;
console.log("postStyle token: " + token);
var blobService = azure.createBlobService();
blobService.createBlockBlobFromStream (INPUT_CONTAINER, token + '_2', req, req.headers["content-length"], function(error, result, response) {
if(!error){
tableJob.updateJobProperty(token, 'patternBlobName', token + '_2', function(){
res.status(200).send('OK: '+result + " "+response);
});
}else{
res.status(400).send(error);
}
});
}
function getImage(req, res){
var token = req.params.token;
/*
blobSvc.getBlobToStream(OUTPUT_CONTAINER, job.outputBlobName, res, function(error, result, response){
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end();
});
*/
tableJob.getJob(token, function(error, result, response, job){
if(job){
console.log("OUTPUTBLOBNAME: "+ job.outputBlobName);
blobSvc.getBlobToStream(OUTPUT_CONTAINER, job.outputBlobName, res, function(error, result, response){
});
} else {
res.status(404).send("Job not found");
}
})
}
function start(req, res){
//start the job
var token = req.params.token;
console.log('starting job id:', token);
tableJob.getJob(token, function(error, result, response, job) {
// let's check that all the data is here:
if (!job || !job.imageBlobName || !job.patternBlobName) {
res.status(400).send("pattern or image file is missing");
} else {
tableJob.updateJobProperty(token, 'status', 'WAITING', function(){ //don't give a shit about this callback.
tableJob.updateJobProperty(token, 'params', JSON.stringify({imageSize: req.params.size}), function(){
queueJob.pushJob(token, function(){
res.status(200).send("Job started");
})
});
});
}
});
}
function getStatus(req, res){
//get the status of the job
var token = req.params.token;
tableJob.getJob(token, function(error, result, response, job){
if(job){
res.status(200).send(job.status);
} else {
res.status(404).send("Job not found");
}
})
}
function createToken(callback){
//gen token and all the information for the job
var date = new Date();
var salt = [date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds(), process.hrtime()[0], process.hrtime()[1]].join(";");
var hash = crypto.createHmac('sha256', 'a secret')
.update(salt)
.digest('hex');
callback(hash);
}