-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
106 lines (92 loc) · 3.26 KB
/
Copy pathmain.js
File metadata and controls
106 lines (92 loc) · 3.26 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
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
var express = require('express');
var app = express();
var WebTorrent = require('webtorrent')
var client = new WebTorrent()
var path = require('path');
app.use(function(req, res, next) {
var allowedOrigins = ['http://127.0.0.1:3010', 'http://localhost:8080'];
var origin = req.headers.origin;
if(allowedOrigins.indexOf(origin) > -1){
res.setHeader('Access-Control-Allow-Origin', origin);
}
//res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:8020');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
return next();
});
app.get('/', (req, res) => {
res.json({api: 'work', status: '1'})
})
app.get('/add/torrent/:magnet', (req, res) => {
if(req.params.magnet){
let magnetURI = 'magnet:?xt=urn:btih:'+req.params.magnet;
if(client.get(magnetURI) == null){
client.add(magnetURI, (torrent) => {
res.json({status: 'ok'})
console.log("[STATUS] ok")
})
}else{
res.json({status: 'error', message: 'Cannot add duplicate torrent. Try stream'})
console.log("[ERROR] Cannot add duplicate torrent. Try stream")
}
}
})
var getLargestFile = function (torrent) {
var file;
for(i = 0; i < torrent.files.length; i++) {
if (!file || file.length < torrent.files[i].length) {
file = torrent.files[i];
}
}
return file;
}
app.get('/stream/torrent/:magnet', (req, res) => {
if(req.params.magnet){
let magnetURI = 'magnet:?xt=urn:btih:'+req.params.magnet
var torrent = client.get(magnetURI)
var file = getLargestFile(torrent)
var total = file.length
if(typeof req.headers.range != 'undefined') {
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total - 1;
var chunksize = (end - start) + 1;
} else {
var start = 0; var end = total;
var chunksize = (end - start) + 1;
}
var stream = file.createReadStream({start: start, end: end})
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes', 'Content-Length': chunksize,
'Content-Type': 'video/mp4'
})
stream.pipe(res)
}
})
app.get('/status/torrent/:magnet', (req, res) =>{
if(req.params.magnet){
let magnetURI = 'magnet:?xt=urn:btih:'+req.params.magnet;
client.get(magnetURI);
console.log(client.torrents)
res.json({
downloadSpeed: client.downloadSpeed,
uploadSpeed: client.uploadSpeed,
totalDownloaded: client.downloaded,
totalProgress: client.progress,
torrentPeers: client.numPeers,
torrentPath: client.path,
timeRemaining: client.timeRemaining,
});
}
});
app.get('/remove/torrent/:magnet', (req, res) => {
client.remove(req.params.magnet, function callback (err) {
res.json({status: 'ok'})
})
})
app.listen(3010);