Skip to content

Instantly share code, notes, and snippets.

@zajca
Created February 27, 2015 09:48
Show Gist options
  • Select an option

  • Save zajca/2bda3b97da6fb7e71692 to your computer and use it in GitHub Desktop.

Select an option

Save zajca/2bda3b97da6fb7e71692 to your computer and use it in GitHub Desktop.
NODEJS - gridfs-stream - video
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
@zajca
Copy link
Copy Markdown
Author

zajca commented Feb 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment