From b6517c544237b2e02e34203fcb307dabd5ac28ca Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Fri, 16 Nov 2018 13:33:40 -0800 Subject: [PATCH] port fix for #1005 to vnext --- app/api.js | 6 +++--- app/fileReceiver.js | 10 +++++----- server/routes/download.js | 12 +----------- server/storage/gcs.js | 2 +- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/app/api.js b/app/api.js index 2def6039..ce4c8a16 100644 --- a/app/api.js +++ b/app/api.js @@ -318,8 +318,8 @@ function download(id, keychain, onprogress, canceller) { }); xhr.addEventListener('progress', function(event) { - if (event.lengthComputable && event.target.status === 200) { - onprogress([event.loaded, event.total]); + if (event.target.status === 200) { + onprogress(event.loaded); } }); const auth = await keychain.authHeader(); @@ -327,7 +327,7 @@ function download(id, keychain, onprogress, canceller) { xhr.setRequestHeader('Authorization', auth); xhr.responseType = 'blob'; xhr.send(); - onprogress([0, 1]); + onprogress(0); }); } diff --git a/app/fileReceiver.js b/app/fileReceiver.js index 84445b8d..e2aa907d 100644 --- a/app/fileReceiver.js +++ b/app/fileReceiver.js @@ -48,7 +48,7 @@ export default class FileReceiver extends Nanobus { this.fileInfo.name = meta.name; this.fileInfo.type = meta.type; this.fileInfo.iv = meta.iv; - this.fileInfo.size = meta.size; + this.fileInfo.size = +meta.size; this.fileInfo.manifest = meta.manifest; this.state = 'ready'; } @@ -77,7 +77,7 @@ export default class FileReceiver extends Nanobus { this.fileInfo.id, this.keychain, p => { - this.progress = p; + this.progress = [p, this.fileInfo.size]; this.emit('progress'); } ); @@ -113,7 +113,7 @@ export default class FileReceiver extends Nanobus { async downloadStream(noSave = false) { const onprogress = p => { - this.progress = p; + this.progress = [p, this.fileInfo.size]; this.emit('progress'); }; @@ -142,7 +142,7 @@ export default class FileReceiver extends Nanobus { }; await this.sendMessageToSw(info); - onprogress([0, this.fileInfo.size]); + onprogress(0); if (noSave) { const res = await fetch(`/api/download/${this.fileInfo.id}`); @@ -166,7 +166,7 @@ export default class FileReceiver extends Nanobus { id: this.fileInfo.id }); prog = msg.progress; - onprogress([prog, this.fileInfo.size]); + onprogress(prog); await delay(1000); } diff --git a/server/routes/download.js b/server/routes/download.js index 5680ed36..2f37de22 100644 --- a/server/routes/download.js +++ b/server/routes/download.js @@ -6,14 +6,6 @@ module.exports = async function(req, res) { const id = req.params.id; try { const meta = req.meta; - const contentLength = await storage.length(id); - res.writeHead(200, { - 'Content-Disposition': 'attachment', - 'Content-Type': 'application/octet-stream', - 'Content-Length': contentLength, - 'WWW-Authenticate': `send-v1 ${req.nonce}` - }); - const fileStream = await storage.get(id); let cancelled = false; @@ -22,7 +14,7 @@ module.exports = async function(req, res) { fileStream.destroy(); }); - fileStream.on('end', async () => { + fileStream.pipe(res).on('finish', async () => { if (cancelled) { return; } @@ -39,8 +31,6 @@ module.exports = async function(req, res) { log.info('StorageError:', id); } }); - - fileStream.pipe(res); } catch (e) { res.sendStatus(404); } diff --git a/server/storage/gcs.js b/server/storage/gcs.js index 8db209e7..1b7d0c5a 100644 --- a/server/storage/gcs.js +++ b/server/storage/gcs.js @@ -13,7 +13,7 @@ class GCSStorage { } getStream(id) { - return this.bucket.file(id).createReadStream(); + return this.bucket.file(id).createReadStream({ validation: false }); } set(id, file) {