Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.get('/tracks/:id', (req, res) => {
  2.     const { id } = req.params;
  3.     const _id = new mongoose.Types.ObjectId(id);
  4.  
  5.     const { range } = req.headers;
  6.  
  7.     if(!range) {
  8.         res.statusCode = 416;
  9.         res.end();
  10.     }
  11.  
  12.     const cursor = bucket.find({ _id });
  13.  
  14.     cursor.on('data', document => {
  15.         const positions = range.replace(/bytes=/, '').split('-');
  16.         const start = parseInt(positions[0], 10);
  17.         const end = positions[1] ? parseInt(positions[1], 10) : document.length - 1;
  18.         const head = {
  19.             'Accept-Ranges': 'bytes',
  20.             'Content-Type': 'audio/mpeg',
  21.             'Content-Length': (end - start) + 1,
  22.             'Content-Range': `bytes ${start}-${end}/${document.length}`,
  23.         };
  24.        
  25.         res.writeHead(206, head);
  26.  
  27.         bucket.openDownloadStream(_id, { start, end })
  28.             .pipe(res)
  29.             .on('error', () => {
  30.                 res.statusCode = 404;
  31.                 res.end();
  32.             });
  33.     });
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement