Skip to content

Commit

Permalink
Set end range in content stream if undefined (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanjunath committed Mar 17, 2021
1 parent 25f4045 commit 24ce49b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions creator-node/src/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ const streamFromFileSystem = async (req, res, path) => {

// TODO - route doesn't support multipart ranges.
if (stat && range) {
const { start, end } = range
let { start, end } = range
if (end >= stat.size) {
// Set "Requested Range Not Satisfiable" header and exit
res.status(416)
return sendResponse(req, res, errorResponseRangeNotSatisfiable('Range not satisfiable'))
}

fileStream = fs.createReadStream(path, { start, end: end || (stat.size - 1) })
// set end in case end is undefined or null
end = end || (stat.size - 1)

fileStream = fs.createReadStream(path, { start, end })

// Add a content range header to the response
res.set('Content-Range', formatContentRange(start, end, stat.size))
Expand Down Expand Up @@ -183,15 +186,18 @@ const getCID = async (req, res) => {
const range = getRequestRange(req)

if (req.params.streamable && range) {
const { start, end } = range
let { start, end } = range
if (end >= stat.size) {
// Set "Requested Range Not Satisfiable" header and exit
res.status(416)
return sendResponse(req, res, errorResponseRangeNotSatisfiable('Range not satisfiable'))
}

// set end in case end is undefined or null
end = end || (stat.size - 1)

// Set length to be end - start + 1 so it matches behavior of fs.createReadStream
const length = end ? end - start + 1 : stat.size - start
const length = end - start + 1
stream = req.app.get('ipfsAPI').catReadableStream(
CID, { offset: start, length }
)
Expand Down

0 comments on commit 24ce49b

Please sign in to comment.