Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.get('/tracks/:id', (req, res) => {
- const { id } = req.params;
- const _id = new mongoose.Types.ObjectId(id);
- const { range } = req.headers;
- if(!range) {
- res.statusCode = 416;
- res.end();
- }
- const cursor = bucket.find({ _id });
- cursor.on('data', document => {
- const positions = range.replace(/bytes=/, '').split('-');
- const start = parseInt(positions[0], 10);
- const end = positions[1] ? parseInt(positions[1], 10) : document.length - 1;
- const head = {
- 'Accept-Ranges': 'bytes',
- 'Content-Type': 'audio/mpeg',
- 'Content-Length': (end - start) + 1,
- 'Content-Range': `bytes ${start}-${end}/${document.length}`,
- };
- res.writeHead(206, head);
- bucket.openDownloadStream(_id, { start, end })
- .pipe(res)
- .on('error', () => {
- res.statusCode = 404;
- res.end();
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement