From 5ff92c64529de99ed19858ec3e7eb06db4700710 Mon Sep 17 00:00:00 2001 From: Emily Date: Thu, 12 Jul 2018 14:00:22 -0700 Subject: [PATCH] fix cancelled downloads increasing count --- server/routes/download.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/routes/download.js b/server/routes/download.js index b320d899..8fe177c7 100644 --- a/server/routes/download.js +++ b/server/routes/download.js @@ -15,9 +15,14 @@ module.exports = async function(req, res) { }); const file_stream = storage.get(id); let sentBytes = 0; + let cancelled = false; + + req.on('close', () => (cancelled = true)); + file_stream.on('data', c => (sentBytes += c.length)); + file_stream.on('end', async () => { - if (sentBytes < contentLength) { + if (cancelled) { return; } const dl = meta.dl + 1; @@ -32,6 +37,7 @@ module.exports = async function(req, res) { log.info('StorageError:', id); } }); + file_stream.pipe(res); } catch (e) { res.sendStatus(404);