Skip to content

Instantly share code, notes, and snippets.

@jrabary
Forked from geta6/partial.coffee
Created January 15, 2013 16:51
Show Gist options
  • Save jrabary/4540062 to your computer and use it in GitHub Desktop.
Save jrabary/4540062 to your computer and use it in GitHub Desktop.
#
# 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