Created
February 27, 2015 09:48
-
-
Save zajca/2bda3b97da6fb7e71692 to your computer and use it in GitHub Desktop.
NODEJS - gridfs-stream - video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| StreamGridFile = (req, res,size,id,contentType) -> | |
| if req.headers['range'] | |
| parts = req.headers['range'].replace(/bytes=/, '').split('-') | |
| partialStart = parts[0] | |
| partialEnd = parts[1] | |
| start = parseInt(partialStart, 10) | |
| end = if partialEnd then parseInt(partialEnd, 10) else size - 1 | |
| chunkSize = end - start + 1 | |
| res.writeHead 206, | |
| 'Content-Range': 'bytes ' + start + '-' + end + '/' + size | |
| 'Accept-Ranges': 'bytes' | |
| 'Content-Length': chunkSize | |
| 'Content-Type': contentType | |
| global.gfs.createReadStream( | |
| _id: id | |
| range: | |
| startPos:start | |
| endPos:end | |
| ).pipe res | |
| else | |
| res.header 'Content-Type', contentType | |
| res.header 'Content-Length', size | |
| readStream = global.gfs.createReadStream(_id: id) | |
| readStream.pipe res |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on: https://gist.github.com/psi-4ward/7099001