fix cancelled downloads increasing count

This commit is contained in:
Emily 2018-07-12 14:00:22 -07:00
parent dfea1e96fb
commit 5ff92c6452
1 changed files with 7 additions and 1 deletions

View File

@ -15,9 +15,14 @@ module.exports = async function(req, res) {
}); });
const file_stream = storage.get(id); const file_stream = storage.get(id);
let sentBytes = 0; let sentBytes = 0;
let cancelled = false;
req.on('close', () => (cancelled = true));
file_stream.on('data', c => (sentBytes += c.length)); file_stream.on('data', c => (sentBytes += c.length));
file_stream.on('end', async () => { file_stream.on('end', async () => {
if (sentBytes < contentLength) { if (cancelled) {
return; return;
} }
const dl = meta.dl + 1; const dl = meta.dl + 1;
@ -32,6 +37,7 @@ module.exports = async function(req, res) {
log.info('StorageError:', id); log.info('StorageError:', id);
} }
}); });
file_stream.pipe(res); file_stream.pipe(res);
} catch (e) { } catch (e) {
res.sendStatus(404); res.sendStatus(404);