Browse Source

port fix for #1005 to vnext

Danny Coates 6 years ago
parent
commit
b6517c5442
4 changed files with 10 additions and 20 deletions
  1. 3 3
      app/api.js
  2. 5 5
      app/fileReceiver.js
  3. 1 11
      server/routes/download.js
  4. 1 1
      server/storage/gcs.js

+ 3 - 3
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);
   });
 }
 

+ 5 - 5
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);
       }
 

+ 1 - 11
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);
   }

+ 1 - 1
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) {