moved saveFile from utils to fileReceiver

This commit is contained in:
Danny Coates 2018-02-21 14:58:41 -08:00
parent 12443db891
commit e4b98fe65a
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
3 changed files with 34 additions and 35 deletions

View File

@ -5,8 +5,7 @@ import {
delay, delay,
fadeOut, fadeOut,
openLinksInNewTab, openLinksInNewTab,
percent, percent
saveFile
} from './utils'; } from './utils';
import * as metrics from './metrics'; import * as metrics from './metrics';
@ -170,12 +169,11 @@ export default function(state, emitter) {
try { try {
const start = Date.now(); const start = Date.now();
metrics.startedDownload({ size: file.size, ttl: file.ttl }); metrics.startedDownload({ size: file.size, ttl: file.ttl });
const f = await state.transfer.download(); await state.transfer.download();
const time = Date.now() - start; const time = Date.now() - start;
const speed = size / (time / 1000); const speed = size / (time / 1000);
await delay(1000); await delay(1000);
await fadeOut('.page'); await fadeOut('.page');
saveFile(f);
state.storage.totalDownloads += 1; state.storage.totalDownloads += 1;
state.transfer.reset(); state.transfer.reset();
metrics.completedDownload({ size, time, speed }); metrics.completedDownload({ size, time, speed });

View File

@ -78,16 +78,44 @@ export default class FileReceiver extends Nanobus {
if (this.cancelled) { if (this.cancelled) {
throw new Error(0); throw new Error(0);
} }
this.msg = 'downloadFinish'; await saveFile({
this.state = 'complete';
return {
plaintext, plaintext,
name: decodeURIComponent(this.fileInfo.name), name: decodeURIComponent(this.fileInfo.name),
type: this.fileInfo.type type: this.fileInfo.type
}; });
this.msg = 'downloadFinish';
this.state = 'complete';
return;
} catch (e) { } catch (e) {
this.state = 'invalid'; this.state = 'invalid';
throw e; throw e;
} }
} }
} }
async function saveFile(file) {
return new Promise(function(resolve, reject) {
const reader = new FileReader();
const dataView = new DataView(file.plaintext);
const blob = new Blob([dataView], { type: file.type });
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, file.name);
return resolve();
}
reader.addEventListener('loadend', function() {
if (reader.error) {
return reject(reader.error);
}
if (reader.result) {
const a = document.createElement('a');
a.href = reader.result;
a.download = file.name;
document.body.appendChild(a);
a.click();
}
resolve();
});
reader.readAsDataURL(blob);
});
}

View File

@ -141,32 +141,6 @@ function fadeOut(selector) {
return delay(300); return delay(300);
} }
function saveFile(file) {
const reader = new FileReader();
const dataView = new DataView(file.plaintext);
const blob = new Blob([dataView], { type: file.type });
if (window.navigator.msSaveBlob) {
return window.navigator.msSaveBlob(blob, file.name);
}
reader.addEventListener('loadend', function() {
if (reader.result) {
const a = document.createElement('a');
a.href = reader.result;
a.download = file.name;
document.body.appendChild(a);
a.click();
return;
}
if (reader.error) {
console.error(reader.error);
window.location.href = '/error';
//TODO
}
});
reader.readAsDataURL(blob);
}
function openLinksInNewTab(links, should = true) { function openLinksInNewTab(links, should = true) {
links = links || Array.from(document.querySelectorAll('a:not([target])')); links = links || Array.from(document.querySelectorAll('a:not([target])'));
if (should) { if (should) {
@ -195,6 +169,5 @@ module.exports = {
b64ToArray, b64ToArray,
canHasSend, canHasSend,
isFile, isFile,
saveFile,
openLinksInNewTab openLinksInNewTab
}; };