-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
175 lines (146 loc) · 3.95 KB
/
app.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* npm module deps
*/
const express = require('express');
const { File } = require('megajs');
/**
* local deps
*/
const { createError, log } = require('./helpers');
const middlewares = require('./middleware');
/**
* bootstrap express app
*/
const app = new express();
/**
* various things
*/
const baseMega = 'https://mega.nz/#';
/**
* helpers
*/
const genFolderJSON = (req, folder, child) => {
return folder.map((file, i) => {
if (file.directory) {
return {
name: file.attributes.n,
children: genFolderJSON(req, file.children, i)
}
} else {
return {
name: file.attributes.n,
streamableLink: `${genFullURL(req)}/${child ? child + '/' : ''}${i}`
}
}
})
}
const genHeaders = (start, end, fileSize, chunkSize) => {
return {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Range': 'bytes',
'Content-Length': chunkSize,
'Content-Type': 'video/x-matroska'
}
}
const genFullURL = (req) => { return req.protocol + '://' + req.get('host') + req.originalUrl }
/**
* init some vars
*/
let currentStream;
let currentReq = 0;
let lastReq = 0;
let isStreaming = false;
/**
* handle streaming individual files from mega
* TODO: Handle multiple streams possibly
*/
app.get('/stream/:link?', (req, res) => {
const file = File.fromURL(baseMega + req.params.link);
let fileSize = null;
// init requests interval var
let streamTimer;
// increment current requests
currentReq++;
// close event handler
req.on('close', () => {
if (lastReq == currentReq) {
log(`Stopping playback of ${currentStream}`);
currentStream = '';
isStreaming = false;
currentReq = 0;
lastReq = 0;
}
lastReq = currentReq;
})
file.loadAttributes((err, data) => {
if (err) res.writeHead(500).send(err);
// Handle current stream
if (currentStream !== data.name) {
log(`Streaming ${data.name} to ${req.headers['user-agent']} via ${req.protocol}`);
currentStream = data.name;
}
// Assign true filesize from mega file
fileSize = data.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunkSize = (end - start) + 1;
res.writeHead(206, genHeaders(start, end, fileSize, chunkSize));
const dlPipe = file.download({start, end, maxConnections: 1});
// create 5 second interval to watch for stream end
streamTimer = setInterval(() => {
if (!isStreaming) {
dlPipe.emit('close');
clearInterval(streamTimer);
}
}, 5000);
dlPipe.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'type': 'video/mp4'
};
res.writeHead(206, head);
file.download({start: 0, end: 1024 * 1024, maxConnections: 1}).pipe(res);
}
isStreaming = true;
})
})
/**
* barebones api for folders
* TODO: make this actually work
*/
app.get('/folder/:link?', function (req, res, next) {
if (req.params.link === undefined) return next(createError(404, "No link provided"));
const folder = File.fromURL(baseMega + req.params.link)
if (req.params.child === undefined) {
folder.loadAttributes((err, data) => {
res.json(genFolderJSON(req, data.children))
/*res.json(data.children.map((f, i) => {
console.log(f.children)
if (f.directory) {
return {
name: f.attributes.n,
children: f.children.map()
}
} else {
return {
name: f.attributes.n,
streamableLink: `${genFullURL(req)}/${i}`
}
}
}))*/
})
}
})
/**
* load all middlewares
*/
for (let middleware in middlewares) {
app.use(middlewares[middleware]);
}
// TODO: allow server config
app.listen(8000);
console.log('MEGA streaming on port 8000');