From 7d9115551db6da22a0512099ec77a1927f4a405c Mon Sep 17 00:00:00 2001 From: Alban Mouton Date: Fri, 2 Aug 2024 16:56:08 +0200 Subject: [PATCH] fix: undefined content-type error --- server/router/portals.js | 2 +- server/utils/assets.js | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/server/router/portals.js b/server/router/portals.js index 64db640..c9fe1a1 100644 --- a/server/router/portals.js +++ b/server/router/portals.js @@ -703,7 +703,7 @@ router.get('/:id/uses/:useId/image', asyncWrap(setPortalAnonymous), asyncWrap(as if (!use.image) return res.status(404).send('use image not found') res.sendFile(path.join(usesUtils.directory(req.params.id, req.params.useId), 'image'), { headers: { - 'content-type': use.image.type, + 'content-type': use.image.type ?? 'image/png', 'cache-control': use.published ? 'public' : 'private' } }) diff --git a/server/utils/assets.js b/server/utils/assets.js index 88ed5c6..4ecb864 100644 --- a/server/utils/assets.js +++ b/server/utils/assets.js @@ -128,18 +128,14 @@ exports.downloadAsset = async (req, res) => { const filePath = resolvePath(portalDir, req.params.assetId) if (req.query.hash && req.query.hash !== 'undefined') { const maxAge = draft ? 0 : 31536000 - return res.sendFile(`${filePath}-${req.query.hash}`, { - headers: { - 'content-type': mime.contentType(req.query.hash) || mime.contentType(asset.name), - 'cache-control': 'public,max-age=' + maxAge - } - }) + const headers = { 'cache-control': 'public,max-age=' + maxAge } + const contentType = mime.contentType(req.query.hash) || mime.contentType(asset.name) + if (contentType) headers['content-type'] = contentType + return res.sendFile(`${filePath}-${req.query.hash}`, { headers }) } else { - return res.sendFile(filePath, { - headers: { - 'content-type': mime.contentType(asset.name), - 'cache-control': 'public,max-age=0' - } - }) + const headers = { 'cache-control': 'public,max-age=0' } + const contentType = mime.contentType(asset.name) + if (contentType) headers['content-type'] = contentType + return res.sendFile(filePath, { headers }) } }