-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (54 loc) · 1.82 KB
/
Copy pathserver.js
File metadata and controls
58 lines (54 loc) · 1.82 KB
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
var PORT = process.env.port || 1337;
var http = require("http");
var url = require("url");
var fs = require('fs');
var path = require('path');
var tempDir = require('./src/temp_dir.js');
var tempDirectory = path.resolve('./tmp');
var Requestor = require('./src/requestor.js');
var requestors = {};
var routes = {
events: /^\/transfers\/(.+)\/(\d+)\/events$/,
chunks: /^\/transfers\/(.+)\/chunks\/(\d+)\/(.+)$/
};
http.createServer(function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
var parsedURL = url.parse(req.url);
var pathname = parsedURL.pathname;
console.log(pathname);
var match = null;
if ((match = pathname.match(routes.chunks)) && req.method === "POST") {
var id = match[1];
var chunkNumber = match[2];
var chunkMd5 = match[3];
var rx = requestors[match[1]];
if (rx) {
rx.receiveChunk(chunkNumber, chunkMd5, req, res);
} else {
res.writeHead(500); res.end();
}
} else if ((match = pathname.match(routes.events)) && req.method === "GET") {
res.writeHead(200, {
"Content-Type":"text/event-stream",
"Cache-Control":"no-cache",
"Connection":"keep-alive"
});
res.write("retry: 1000\n");
var id = match[1];
var numChunks = match[2];
var opts = { id: id, numChunks: match[2], tmp: tempDirectory }
requestors[id] = null;
requestors[id] = new Requestor(opts, res);
req.connection.addListener("close", function() {
requestors[id] = null;
});
} else {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("You know, for uploads");
}
}).listen(PORT);
console.log("Server running at http://0.0.0.0:"+PORT+"/");
// Setup temp directory
tempDir(tempDirectory);