Skip to content

A local-first personal finance app

Stream file without loading to memory in /download-user-file (#172)

Jakub Kuczys9b2d74c3adbf
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