Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.router.get("/", (req: Request, res: Response) => {
- this.client.add(req.headers.magneturi as string, (torrent) => {
- const files = torrent.files;
- files.forEach((file) => {
- const length = file.length;
- 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)
- : length - 1;
- const chunksize = (end - start) + 1;
- let source = file.createReadStream({ start, end });
- const head = {
- "Content-Range": `bytes ${start}-${end}/${length}`,
- "Accept-Ranges": "bytes",
- "Content-Length": chunksize,
- "Content-Type": "video/mp4",
- }
- res.writeHead(206, head);
- source.pipe(res);
- } else {
- const head = {
- "Content-Length": length,
- "Content-Type": "video/mp4",
- };
- res.writeHead(200, head);
- file.createReadStream().pipe(res);
- }
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement