diff --git a/creator-node/src/routes/files.js b/creator-node/src/routes/files.js index 212aa0727f3..8eb3adc5048 100644 --- a/creator-node/src/routes/files.js +++ b/creator-node/src/routes/files.js @@ -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)) @@ -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 } @@ -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)) { @@ -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) { @@ -473,7 +486,6 @@ const getCID = async (req, res) => { time: `${Date.now() - startMs}ms` }) - _setHeadersForStreaming() logGetCIDDecisionTree(decisionTree, req) return fsStream } catch (e) { @@ -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