-
Notifications
You must be signed in to change notification settings - Fork 0
/
CascadeIdentifierService.node.js
149 lines (110 loc) · 3.4 KB
/
CascadeIdentifierService.node.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
console.log("starting");
// testing image : http://images.metmuseum.org/CRDImages/an/web-large/1999,325,018.jpg
var fs = require("fs");
var request = require("request");
var urlparser = require("url");
var pathparser = require("path");
var Percolator = require('percolator').Percolator;
var port = 3256;
var server = new Percolator({port: port});
var Indentifier = require("./classes/CascadeIdentifier.class.js").CascadeIdentifier();
var image = "/home/metmuseum/projects/facerecog/vilniusSmiling.jpg";
var download = function(uri, filename, callback){
console.log("downloading " + uri)
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
server.route('*', {
GET : function(req, res){
var parsed = urlparser.parse(req.url, true)
var query = parsed.query;
var imageUrl = query.imageurl;
var results = {foo : imageUrl};
var tempname = "tmp"+Date.now()+ Math.random();
if(req.url.match(/index\.html/i)
|| req.url.match(/raphael-min\.js/)
|| req.url.match(/index2\.html/i)
|| req.url.match(/masonry\.pkgd\.min\.js/)){
if(!query.action){
// this is doing it client-side
sendFile(parsed.pathname, query, res);
return;
}
}
if(req.url.match(/^\/proxy\//)){
console.log("calling proxy");
var split = req.url.split("/");
split.shift(); split.shift();
var theurl = split.join("/");
console.log("url is " + theurl);
var proxy = require("./classes/proxy/proxy.js").ProxyManager();
var result = proxy.callUrlNoCallback(theurl, req, res);
return;
}
console.log("getting identify");
download(imageUrl, tempname, function(){
Indentifier.match_all_cascades(tempname, function(results){
fs.unlink(tempname);
res.object(results).send();
});
});
console.log("imageurl " + imageUrl);
// if it's a url for an image, dl it here, make a temp, then delete it later.
}
});
server.listen(function(err){
console.log('server is listening on port ', port);
});
var dataCache = {};
function sendFile(path, query, res){
if(path == "/"){
path = "/index.html";
}
var extname = pathparser.extname(path);
var contentType = 'text/html';
if(path.match(/secrets\.js/)){
res.writeHead(404, {'Content-Type': contentType});
//indexhtml = data;
res.end("I'm afraid I can't do that.");
return;
}
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.jpg':
contentType = 'image/jpeg';
break;
case '.png':
contentType = 'image/png';
break;
case '.ico':
contentType = 'image/vnd.microsoft.icon';
break;
}
if(!dataCache[path]){
fs.readFile("."+path, function(err, data){
if(err){
console.log("file read error");
console.log(err);
res.writeHead(404, {'Content-Type': contentType});
//indexhtml = data;
res.end(data);
}else{
res.writeHead(200, {'Content-Type': contentType});
//dataCache[path] = data;
res.end(data);
}
});
}else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(dataCache[path]);
}
}
console.log("done");