-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-server.js
68 lines (59 loc) · 2.55 KB
/
stream-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
var fs = require('fs'),
url = require('url'),
mime = require('mime'),
path = require('path'),
http = require('http'),
request = require('request');
var MongoClient = require('mongodb').MongoClient;
// Connect to the db 'imageArchivesDB'
MongoClient.connect("mongodb://localhost:27017/imageArchivesDB", function(err, db) {
if(!err) {
console.log("We are connected");
}
var imageManifest = db.collection("imageManifest");
var server = http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname.substring(1);
imageManifest.find({"_id": uri}).toArray(function(err, resultImageManifest){
if(!resultImageManifest.length){
console.log('404 File Not Found: ' + filename);
res.writeHead(404, {
'Content-Type': "text/plain",
'Access-Control-Allow-Origin': '*',
});
res.write("404: File Not Found\n");
res.end();
return;
}
else{
console.log('Request for file ' + uri);
var file = resultImageManifest[0].imagePath;
var filename = path.basename(file);
var mimetype = mime.lookup(file);
fs.stat(file, function(error, stat){
if(error){ throw error; }
res.writeHead(200, {
'Content-Type' : mimetype,
'Content-Disposition': 'attachment; filename=' + uri,
'X-Total-Length' : stat.size,
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': 'X-Total-Length, Content-Disposition'
});
var readStream = fs.createReadStream(file, {
autoClose: true
});
// Waits until the readable stream is actually valid before piping
readStream.on('open', function(){
// Pipes the read stream to the response object(which goes to the client)
// req.pipe(request('http://127.0.0.1/blob')).pipe(res);
readStream.pipe(res);
});
// Catches any errors that happen(invalid filenames, etc)
readStream.on('error', function(err){
res.end(err);
});
});
}
});
});
server.listen(8090);
});