drop.chapril.org-firefoxsend/server/routes/download.js
Danny Coates 6184a70ba4
fixes #1005
The upstream gcp aggressively closes the connection once it has
received Content-Length bytes. However the @google-cloud/storage
module doesn't handle this well and emits no event in this case.
We were setting Content-Length because it's slightly more
efficient and was important for our download progress
bar (not anymore). The download should function fine without
setting the Content-Length, and allows the storage stream to finish
before closing the upstream socket.
2018-11-14 16:38:46 -08:00

38 lines
813 B
JavaScript

const storage = require('../storage');
const mozlog = require('../log');
const log = mozlog('send.download');
module.exports = async function(req, res) {
const id = req.params.id;
try {
const meta = req.meta;
const file_stream = storage.get(id);
let cancelled = false;
req.on('close', () => {
cancelled = true;
file_stream.destroy();
});
file_stream.pipe(res).on('finish', async () => {
if (cancelled) {
return;
}
const dl = meta.dl + 1;
const dlimit = meta.dlimit;
try {
if (dl >= dlimit) {
await storage.del(id);
} else {
await storage.setField(id, 'dl', dl);
}
} catch (e) {
log.info('StorageError:', id);
}
});
} catch (e) {
res.sendStatus(404);
}
};