From 9b2d74c3adbf53357d50cc931055c73742ea6f3f Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Tue, 28 Mar 2023 22:05:31 +0200 Subject: [PATCH] Stream file without loading to memory in /download-user-file (#172) --- src/app-sync.js | 11 +---------- src/app-sync.test.js | 2 +- upcoming-release-notes/172.md | 6 ++++++ 3 files changed, 8 insertions(+), 11 deletions(-) create mode 100644 upcoming-release-notes/172.md diff --git a/src/app-sync.js b/src/app-sync.js index c24a4020b6..2f7aa34d23 100644 --- a/src/app-sync.js +++ b/src/app-sync.js @@ -313,17 +313,8 @@ app.get('/download-user-file', async (req, res) => { return; } - let buffer; - try { - buffer = await fs.readFile(getPathForUserFile(fileId)); - } catch (e) { - console.log(`Error: file does not exist: ${getPathForUserFile(fileId)}`); - res.status(500).send('File does not exist on server'); - return; - } - res.setHeader('Content-Disposition', `attachment;filename=${fileId}`); - res.send(buffer); + res.sendFile(getPathForUserFile(fileId)); }); app.post('/update-user-filename', (req, res) => { diff --git a/src/app-sync.test.js b/src/app-sync.test.js index a183621b5c..c8d5e22f90 100644 --- a/src/app-sync.test.js +++ b/src/app-sync.test.js @@ -50,7 +50,7 @@ describe('/download-user-file', () => { .set('x-actual-token', 'valid-token') .set('x-actual-file-id', 'missing-fs-file'); - expect(res.statusCode).toEqual(500); + expect(res.statusCode).toEqual(404); }); it('returns an attachment file', async () => { diff --git a/upcoming-release-notes/172.md b/upcoming-release-notes/172.md new file mode 100644 index 0000000000..f2dc39760b --- /dev/null +++ b/upcoming-release-notes/172.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [Jackenmen] +--- + +Changed budget file download endpoint to use less memory by using streams