-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
225 lines (197 loc) · 6.65 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const http = require('http');
const httpShutdown = require('http-shutdown');
const util = require("util");
const url = require('url');
const spawn = require('child_process').spawn;
const fs = require('fs');
const req = require('request');
const tmp = require('tmp-promise');
const Promise = require('bluebird');
Promise.longStackTraces();
const PORT = 8080;
const countryCodes = ['us', 'eu', 'gb'];
var validateRequest = function (request, response) {
switch (request.method) {
case 'GET':
return validateGetRequest(request, response);
case 'POST':
return validatePostRequest(request, response);
default:
return false;
}
}
var validateGetRequest = function (request, response) {
var urlParts = url.parse(request.url, true);
var imageUrl = urlParts.query['image_url'];
if (urlParts.pathname !== '/') {
return null;
} else if (!imageUrl) {
throw new Error('Missing required parameter');
}
const supportedTypes = ['jpg', 'jpeg'];
const regexType = /\.(\w+)$/;
const match = regexType.exec(imageUrl.toLowerCase());
var result = { 'url': imageUrl }
if (match) {
const type = match[1];
if (supportedTypes.indexOf(type) === -1) {
throw new Error('Unsupported type');
}
result['type'] = match[1];
}
//check country code
if (countryCodes.indexOf(urlParts.query['country_code']) !== -1) {
result['country_code'] = urlParts.query['country_code'];
}
//check pattern
if (/^\w{2,3}$/.exec(urlParts.query['pattern']) !== null) {
result['pattern'] = urlParts.query['pattern'];
}
return result;
}
var validatePostRequest = function (request, response) {
var urlParts = url.parse(request.url, true);
var result = {};
//check country code
if (countryCodes.indexOf(urlParts.query['country_code']) !== -1) {
result['country_code'] = urlParts.query['country_code'];
}
return result;
}
var createTempFile = function (postfix) {
return tmp.file({ postfix: postfix });
};
var downloadFile = function (uri, tmpFile, countryCode) {
return new Promise(function (resolve, reject) {
console.log("Download %s to %s", uri, tmpFile);
writeStream = fs.createWriteStream(tmpFile);
writeStream
.on('finish', function () {
resolve({
filepath: tmpFile,
countrycode: countryCode || 'eu'
});
})
.on('error', reject);
req(uri)
.pipe(writeStream)
.on('error', reject);
});
};
var runAlpr = function (result) {
return new Promise(function (resolve, reject) {
console.log("Run alpr for %s", result.filepath);
const alpr = spawn('alpr', ['-j', '-c', result.countrycode, '-p', 'sk', result.filepath]);
var outputData = "";
alpr.on('error', reject);
alpr.stdout.on('data', function (data) {
outputData += data.toString();
});
alpr.stderr.on('data', function (data) {
reject(data);
});
alpr.on('close', function () {
resolve(outputData);
});
});
};
var handleValidRequest = function (request, response, params) {
switch (request.method) {
case 'GET':
return handleValidGetRequest(request, response, params);
case 'POST':
return handleValidPostRequest(request, response, params);
default:
return false;
}
}
var handleValidGetRequest = function (request, response, params) {
var tmpFile;
return createTempFile('.' + params['type'])
.then(function (file) {
tmpFile = file;
return downloadFile(params.url, file.path, params['country_code']);
})
.then(runAlpr)
.then(function (data) {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(data);
})
.finally(function () {
fs.unlink(tmpFile.path, function (err) {
console.log('Cleaned up %s - %s', tmpFile.path, err);
});
});
}
var handleValidPostRequest = function (request, response, params) {
return new Promise(function (resolve, reject) {
var data = new Buffer('');
request
.on('data', function (chunk) {
data = Buffer.concat([data, chunk]);
})
.on('end', function () {
var tmpFile;
return createTempFile('.jpg')
.then(function (file) {
tmpFile = file;
return saveToDisk(data, file.path, params['country_code']);
})
.then(runAlpr)
.then(function (data) {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(data);
resolve();
})
.finally(function () {
fs.unlink(tmpFile.path, function (err) {
console.log('Cleaned up %s - %s', tmpFile.path, err);
});
});
});
});
}
var saveToDisk = function (data, tmpFile, countryCode) {
return new Promise(function (resolve, reject) {
console.log("Saving data to %s", tmpFile);
fs.writeFile(tmpFile, data, 'binary', function (err) {
if (err){
reject(err);
}
resolve({
filepath: tmpFile,
countrycode: countryCode || 'eu'
});
});
});
};
var handleError = function (response, msg) {
console.error(msg);
response.writeHead(503, { "Content-Type": "text/plain" });
response.write(msg.toString());
response.end();
}
function handleRequest(request, response) {
try {
const params = validateRequest(request, response);
if (params) {
handleValidRequest(request, response, params)
.catch(function (e) {
handleError(response, e);
});
} else {
response.writeHead(404, { "Content-Type": "text/plain" });
response.end();
}
} catch (e) {
handleError(response, e);
}
}
//Create a server
var server = http.createServer(handleRequest);
server = httpShutdown(server);
//Lets start our server
server.listen(PORT, function () {
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});