-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
144 lines (114 loc) · 2.67 KB
/
server.mjs
File metadata and controls
144 lines (114 loc) · 2.67 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
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
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import cors from 'cors';
import helmet from 'helmet';
import WebTorrent from 'webtorrent';
const app = express();
const port = process.env.PORT || 5000;
app.use(express.static(`./client/build`));
app.use(morgan('dev'));
app.use(cors());
app.use(helmet());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.sendFile(`./client/build/index.html`);
});
/* WebTorrent logic */
let client;
let errorMessage = '';
let stats = {
progress: 0,
downloadSpeed: 0,
};
app.post('/addtorrent', (req, res) => {
const magnet = req.body.value;
console.log(magnet);
client = new WebTorrent();
client.on('error', error => {
errorMessage = error.message;
});
client.on('download', () => {
stats = {
progress: ((client.progress * 100 * 100) / 100).toFixed(0),
downloadSpeed: client.downloadSpeed,
};
});
client.add(magnet, torrent => {
let files = [];
if (torrent.progress === 1) {
stats = {
progress: 100,
downloadSpeed: 0,
};
}
torrent.files.forEach(data => {
files.push({
name: data.name,
length: data.length,
});
});
console.log('ADD SUCCESS');
res.json({
status: 200,
msg: 'OK',
files: files,
torrents: client.torrents.length,
});
});
});
app.get('/removetorrent', (req, res) => {
if (client) {
client.destroy(() => {
stats = {
progress: 0,
downloadSpeed: 0,
};
res.json({
status: 200,
msg: 'removed - OK',
remove: true,
});
});
} else {
res.json({
status: 200,
msg: 'client not exist',
remove: false,
});
}
});
app.get('/status', (req, res) => {
res.json({
status: 200,
msg: 'OK',
stats,
});
});
app.get('/stream', (req, res) => {
let range = req.headers.range;
console.log(range);
let file = client.torrents[0].files[0];
let positions = range.replace(/bytes=/, '').split('-');
let start = parseInt(positions[0], 10);
let file_size = file.length;
let end = positions[1] ? parseInt(positions[1], 10) : file_size - 1;
let chunksize = end - start + 1;
let head = {
'Content-Range': 'bytes ' + start + '-' + end + '/' + file_size,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
res.writeHead(206, head);
let stream_position = {
start: start,
end: end,
};
let stream = file.createReadStream(stream_position);
console.log(stream_position);
stream.pipe(res);
});
app.listen(port, () => {
console.log(`app start - OK on port ${port}`);
});