send/app/fileReceiver.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

import Nanobus from 'nanobus';
2018-01-24 18:23:13 +00:00
import Keychain from './keychain';
2018-07-09 22:39:06 +00:00
import { delay, bytes } from './utils';
import { parseNonce, metadata } from './api';
2017-06-02 19:38:05 +00:00
export default class FileReceiver extends Nanobus {
2018-01-24 18:23:13 +00:00
constructor(fileInfo) {
super('FileReceiver');
2018-01-24 18:23:13 +00:00
this.keychain = new Keychain(fileInfo.secretKey, fileInfo.nonce);
if (fileInfo.requiresPassword) {
this.keychain.setPassword(fileInfo.password, fileInfo.url);
}
2018-01-24 18:23:13 +00:00
this.fileInfo = fileInfo;
2018-02-05 02:30:33 +00:00
this.reset();
}
get progressRatio() {
return this.progress[0] / this.progress[1];
}
2018-02-21 21:59:06 +00:00
get progressIndefinite() {
return this.state !== 'downloading';
}
get sizes() {
return {
partialSize: bytes(this.progress[0]),
totalSize: bytes(this.progress[1])
};
}
cancel() {
if (this.downloadRequest) {
this.downloadRequest.cancel();
2018-01-24 18:23:13 +00:00
}
}
2018-02-05 02:30:33 +00:00
reset() {
this.msg = 'fileSizeProgress';
this.state = 'initialized';
this.progress = [0, 1];
}
2018-01-24 18:23:13 +00:00
async getMetadata() {
const meta = await metadata(this.fileInfo.id, this.keychain);
this.keychain.setIV(meta.iv);
this.fileInfo.name = meta.name;
this.fileInfo.type = meta.type;
this.fileInfo.iv = meta.iv;
this.fileInfo.size = meta.size;
this.state = 'ready';
}
2018-07-05 19:40:49 +00:00
async streamToArrayBuffer(stream, streamSize, onprogress) {
2018-07-06 22:49:50 +00:00
const result = new Uint8Array(streamSize);
let offset = 0;
const reader = stream.getReader();
let state = await reader.read();
while (!state.done) {
result.set(state.value, offset);
offset += state.value.length;
state = await reader.read();
onprogress([offset, streamSize]);
2018-06-29 16:36:08 +00:00
}
2018-07-06 22:49:50 +00:00
onprogress([streamSize, streamSize]);
return result.slice(0, offset).buffer;
2018-06-21 00:05:33 +00:00
}
2018-07-09 22:39:06 +00:00
sendMessageToSw(msg) {
return new Promise( (resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = function(event) {
if(event.data.error !== undefined) {
reject(event.data.error);
} else {
resolve(event.data);
}
}
navigator.serviceWorker.controller.postMessage(msg, [channel.port2]);
});
}
2018-02-21 04:31:27 +00:00
async download(noSave = false) {
2018-07-05 19:40:49 +00:00
const onprogress = p => {
this.progress = p;
this.emit('progress');
2018-07-06 22:49:50 +00:00
};
2018-06-21 00:05:33 +00:00
2018-07-09 22:39:06 +00:00
this.downloadRequest = {
cancel: () => {
this.sendMessageToSw('cancel');
//throw new Error(0);
}
}
try {
2018-07-05 19:40:49 +00:00
this.state = 'downloading';
2018-07-06 22:49:50 +00:00
const auth = await this.keychain.authHeader();
const info = {
key: this.fileInfo.secretKey,
nonce: this.fileInfo.nonce,
filename: this.fileInfo.name,
auth: auth
};
2018-07-09 22:39:06 +00:00
await this.sendMessageToSw(info);
2018-06-29 16:36:08 +00:00
2018-07-09 22:39:06 +00:00
console.log("SENDING REQUEST FROM PAGE ONCE")
2018-06-21 00:05:33 +00:00
2018-02-21 04:31:27 +00:00
if (!noSave) {
2018-07-06 22:49:50 +00:00
const downloadUrl = `${location.protocol}//${
location.host
}/api/download/${this.fileInfo.id}`;
const a = document.createElement('a');
a.href = downloadUrl;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
2018-07-09 22:39:06 +00:00
/*
const auth = await this.sendMessageToSw('authHeader');
if (auth) {
this.keychain.nonce = parseNonce(auth);
}
*/
let prog = 0;
while (prog < this.fileInfo.size) {
prog = await this.sendMessageToSw('progress');
onprogress([prog, this.fileInfo.size]);
await delay();
}
2018-02-21 04:31:27 +00:00
}
2018-06-29 16:36:08 +00:00
2018-07-09 22:39:06 +00:00
this.downloadRequest = null;
this.msg = 'downloadFinish';
this.state = 'complete';
} catch (e) {
this.downloadRequest = null;
2018-07-09 22:39:06 +00:00
if (e === 'cancelled') {
throw new Error(0);
}
throw e;
}
2017-06-02 19:38:05 +00:00
}
}