-
-
Save jrabary/4540062 to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# blob : Object from MongoDB | |
# | |
# blob.body: Buffer | |
# blob.size: length of buffer, substitute for blob.body.length | |
# blob.type: MIME (Eg. audio/x-wav) | |
# | |
# req : Object from http | |
# res : Object from http | |
# _ : Object from underscore | |
# | |
if blob | |
range = req.headers.range | |
unless range | |
res.writeHead 200, | |
'Content-Type': blob.type | |
res.end blob.body | |
else | |
[ini, end] = _.map range.replace(/bytes=/, '').split('-'), (n) -> parseInt n, 10 | |
total = blob.size | |
chunk = end - ini + 1 | |
#console.log "#{ini}-#{end}/#{total} #{chunk}".cyan | |
res.writeHead 206, | |
'Content-Type': blob.type | |
'Content-Length': chunk | |
'Content-Range': "bytes #{ini}-#{end}/#{total}" | |
'Accept-Ranges': 'bytes' | |
res.end blob.body | |
else | |
res.writeHead 404 | |
res.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment