Skip to content

Commit

Permalink
Fix streaming via /ipfs/<cid> and /content/<cid> (#3349)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicky-g authored and SidSethi committed Jun 30, 2022
1 parent 0c1dcf9 commit 9963a98
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions creator-node/src/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ const streamFromFileSystem = async (
res.set('Content-Length', stat.size)
}

// If client has provided filename, set filename in header to be auto-populated in download prompt.
if (req.query.filename) {
res.setHeader(
'Content-Disposition',
contentDisposition(req.query.filename)
)
}

// Set the CID cache-control so that client caches the response for 30 days
res.setHeader('cache-control', 'public, max-age=2592000, immutable')

await new Promise((resolve, reject) => {
fileStream
.on('open', () => fileStream.pipe(res))
Expand All @@ -123,6 +134,9 @@ const streamFromFileSystem = async (
})
})
} catch (e) {
// Unset the cache-control header so that a bad response is not cached
res.removeHeader('cache-control')

// Unable to stream from file system. Throw a server error message
throw e
}
Expand All @@ -141,10 +155,10 @@ const logGetCIDDecisionTree = (decisionTree, req) => {
/**
* Given a CID, return the appropriate file
* 1. Check if file exists at expected storage path (current and legacy)
* 1. If found, stream from FS
* 2. Else, check if CID exists in DB. If not, return 404 not found error
* 3. If exists in DB, fetch file from CN network, save to FS, and stream from FS
* 4. If not avail in CN network, respond with 400 server error
* 2. If found, stream from FS
* 3. Else, check if CID exists in DB. If not, return 404 not found error
* 4. If exists in DB, fetch file from CN network, save to FS, and stream from FS
* 5. If not avail in CN network, respond with 400 server error
*/
const getCID = async (req, res) => {
if (!(req.params && req.params.CID)) {
Expand Down Expand Up @@ -372,7 +386,6 @@ const getCID = async (req, res) => {
stage: `STREAM_FROM_FILE_SYSTEM_COMPLETE`,
time: `${Date.now() - startMs}ms`
})
_setHeadersForStreaming(req, res)
logGetCIDDecisionTree(decisionTree, req)
return fsStream
} catch (e) {
Expand Down Expand Up @@ -473,7 +486,6 @@ const getCID = async (req, res) => {
time: `${Date.now() - startMs}ms`
})

_setHeadersForStreaming()
logGetCIDDecisionTree(decisionTree, req)
return fsStream
} catch (e) {
Expand Down Expand Up @@ -581,21 +593,6 @@ const _verifyContentMatchesHash = async function (req, resizeResp, dirCID) {
}
}

/**
* Sets headers for streaming
* @param {Object} req
* @param {Object} res
*/
function _setHeadersForStreaming(req, res) {
// If client has provided filename, set filename in header to be auto-populated in download prompt.
if (req.query.filename) {
res.setHeader('Content-Disposition', contentDisposition(req.query.filename))
}

// Set the CID cache-control so that client caches the response for 30 days
res.setHeader('cache-control', 'public, max-age=2592000, immutable')
}

/**
* Helper fn to generate the input for `generateImageCids()`
* @param {File[]} resizeResp resizeImage.js response; should be a File[] of resized images
Expand Down

0 comments on commit 9963a98

Please sign in to comment.