From a36f944eba775b1946fdfaa6f5796cea83e0055e Mon Sep 17 00:00:00 2001 From: tycrek Date: Sat, 8 Oct 2022 21:26:33 -0600 Subject: [PATCH] fix: Replace `fs.createReadStream` with `res.sendFile` Some [local installs (non-docker)](https://canary.discord.com/channels/848274994375294986/848313252009869312/1027077979580416050) may have issues with the filesystem calls used by `fs.createReadStream`. Discord user `Ryan#7385` showed that this fixed *something* (though not what we were actually troubleshooting) --- src/routers/resource.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/routers/resource.ts b/src/routers/resource.ts index f9acf7c..9bcc2d3 100644 --- a/src/routers/resource.ts +++ b/src/routers/resource.ts @@ -83,11 +83,12 @@ router.get('/direct*', (req: Request, res: Response, next) => data().get(req.ass .then((stream) => stream.pipe(res)) .then(() => SkynetDelete(fileData)), local: () => fs.pathExists(path(fileData.path)) - .then((exists) => { - if (!exists) throw new Error('File does not exist'); - res.header('Accept-Ranges', 'bytes').header('Content-Length', `${fileData.size}`).type(fileData.mimetype); - fs.createReadStream(fileData.path).pipe(res); - }) + .then((exists) => new Promise((resolve, reject) => !exists + ? reject(new Error('File does not exist')) + : res.header('Accept-Ranges', 'bytes') + .header('Content-Length', `${fileData.size}`) + .type(fileData.mimetype) + .sendFile(path(fileData.path), (err) => err ? reject(err) : resolve(void 0)))) }; return uploaders[fileData.randomId.startsWith('sia://') ? 'sia' : s3enabled ? 's3' : 'local']();