-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
156 lines (135 loc) · 3.54 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
152
153
154
155
156
#!/usr/bin/env node
var torrentStream = require('torrent-stream');
var express = require('express');
var request = require('request').defaults({encoding: null});
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var os = require('os');
var del = require('del');
var mime = require('mime');
var archiver = require('archiver');
var client, url;
var DIR = os.tmpdir()+'/torrent-web-poc';
var PORT = parseArg('--port') || parseArg('-p') || process.env.PORT || 80;
server.listen(PORT);
app.use(express.static(__dirname + '/public'));
console.log('Torrent Web started on port '+PORT+' ...');
//===============================
// API
//===============================
io.on('connection', function (socket) {
console.log('New socket connection.');
if (client && client.files.length) socket.emit('torrent', torrentRepresentation());
else socket.emit('no-torrent');
socket.on('add-torrent', addTorrent);
socket.on('remove-torrent', removeTorrent);
});
app.get('/torrent/:filename', function(req, res) {
console.log('Torrent file request.');
var file = findFile(req.params.filename);
if (file) {
var stream = file.createReadStream();
res.set('Content-Type', mime.lookup(file.name));
res.set('Content-Length', file.length);
stream.pipe(res);
}
else res.status(404).end();
});
app.get('/torrent/', function(req, res) {
var archive = archiver.create('zip', {});
var filename = client.torrent.name + '.zip';
res.set('Content-Type', 'application/zip');
res.set('Content-disposition', 'attachment; filename=' + filename);
archive.pipe(res);
client.files.forEach(function(file) {
archive.append(file.createReadStream(), {name: file.path});
});
archive.finalize();
});
//===============================
// Main functions
//===============================
function findFile(filename) {
var f = null;
client.files.forEach(function(file) {
if (file.name === filename) f = file;
});
return f;
}
function addTorrent(incoming) {
removeTorrent();
url = incoming;
if (url.indexOf('magnet:') === 0) createTorrentEngine(url);
else {
request.get(url, function(err, res, body) {
createTorrentEngine(body);
});
}
}
function removeTorrent() {
if (client) {
console.log('Destroying client.');
client.destroy();
client = null;
io.emit('torrent-removed');
}
deleteFiles();
}
//===============================
// Helper functions
//===============================
/**
* Checks process.argv for one beginning with arg+'='
* @param {string} arg
*/
function parseArg(arg) {
for (var i = 0; i < process.argv.length; i++) {
var val = process.argv[i];
if (startsWith(val, arg+'=')) return val.substring(arg.length+1);
}
function startsWith(string, beginsWith) {
return string.indexOf(beginsWith) === 0;
}
}
function deleteFiles() {
setTimeout(function() {
del.sync(DIR+'/**', {force: true});
}, 1000)
}
function createTorrentEngine(torrent) {
try {
client = torrentStream(torrent, {
uploads: 3,
connections: 30,
path: DIR
});
client.ready(torrentReady);
}
catch(e) {
console.log('Error creating torrent', e);
io.emit('bad-torrent');
}
}
function torrentReady() {
io.emit('torrent', torrentRepresentation());
console.log('client:', client);
}
function simplifyFilesArray(files) {
return files.map(function(file) {
return {
name: file.name,
path: file.path,
length: file.length
};
});
}
function torrentRepresentation() {
return {
url: url,
name: client.torrent.name,
comment: client.torrent.comment,
infoHash: client.infoHash,
files: simplifyFilesArray(client.files)
};
}