fox-send/app/fileReceiver.js

246 lines
6.5 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';
2018-07-31 18:29:26 +00:00
import { downloadFile, metadata } from './api';
import { blobStream } from './streams';
import Zip from './zip';
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;
2018-07-26 05:26:11 +00:00
this.fileInfo.manifest = meta.manifest;
this.state = 'ready';
}
2018-07-09 22:39:06 +00:00
sendMessageToSw(msg) {
2018-07-10 00:00:19 +00:00
return new Promise((resolve, reject) => {
2018-07-09 22:39:06 +00:00
const channel = new MessageChannel();
channel.port1.onmessage = function(event) {
2018-07-11 23:52:46 +00:00
if (event.data === undefined) {
reject('bad response from serviceWorker');
} else if (event.data.error !== undefined) {
2018-07-09 22:39:06 +00:00
reject(event.data.error);
} else {
resolve(event.data);
}
2018-07-10 00:00:19 +00:00
};
2018-07-11 23:52:46 +00:00
2018-07-10 00:00:19 +00:00
navigator.serviceWorker.controller.postMessage(msg, [channel.port2]);
2018-07-09 22:39:06 +00:00
});
}
2018-07-31 18:29:26 +00:00
async downloadBlob(noSave = false) {
this.state = 'downloading';
this.downloadRequest = await downloadFile(
this.fileInfo.id,
this.keychain,
p => {
this.progress = p;
this.emit('progress');
}
);
try {
const ciphertext = await this.downloadRequest.result;
this.downloadRequest = null;
this.msg = 'decryptingFile';
this.state = 'decrypting';
this.emit('decrypting');
let size = this.fileInfo.size;
let plainStream = this.keychain.decryptStream(blobStream(ciphertext));
if (this.fileInfo.type === 'send-archive') {
const zip = new Zip(this.fileInfo.manifest, plainStream);
plainStream = zip.stream;
size = zip.size;
}
const plaintext = await streamToArrayBuffer(plainStream, size);
if (!noSave) {
await saveFile({
plaintext,
name: decodeURIComponent(this.fileInfo.name),
type: this.fileInfo.type
});
}
this.msg = 'downloadFinish';
this.state = 'complete';
} catch (e) {
this.downloadRequest = null;
throw e;
}
}
async downloadStream(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: () => {
2018-07-11 23:52:46 +00:00
this.sendMessageToSw({ request: 'cancel', id: this.fileInfo.id });
2018-07-09 22:39:06 +00:00
}
2018-07-10 00:00:19 +00:00
};
2018-07-09 22:39:06 +00:00
try {
2018-07-05 19:40:49 +00:00
this.state = 'downloading';
2018-07-06 22:49:50 +00:00
const info = {
2018-07-11 23:52:46 +00:00
request: 'init',
id: this.fileInfo.id,
2018-07-06 22:49:50 +00:00
filename: this.fileInfo.name,
2018-07-13 18:13:09 +00:00
type: this.fileInfo.type,
2018-07-26 05:26:11 +00:00
manifest: this.fileInfo.manifest,
2018-07-11 23:52:46 +00:00
key: this.fileInfo.secretKey,
requiresPassword: this.fileInfo.requiresPassword,
password: this.fileInfo.password,
url: this.fileInfo.url,
2018-07-12 22:32:07 +00:00
size: this.fileInfo.size,
2018-07-19 20:20:10 +00:00
nonce: this.keychain.nonce,
2018-07-11 23:52:46 +00:00
noSave
2018-07-06 22:49:50 +00:00
};
2018-07-09 22:39:06 +00:00
await this.sendMessageToSw(info);
2018-06-29 16:36:08 +00:00
2018-07-11 23:52:46 +00:00
onprogress([0, this.fileInfo.size]);
2018-06-21 00:05:33 +00:00
2018-07-11 23:52:46 +00:00
if (noSave) {
const res = await fetch(`/api/download/${this.fileInfo.id}`);
if (res.status !== 200) {
throw new Error(res.status);
}
} else {
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();
2018-07-12 18:22:49 +00:00
}
2018-07-09 22:39:06 +00:00
2018-07-12 18:22:49 +00:00
let prog = 0;
while (prog < this.fileInfo.size) {
const msg = await this.sendMessageToSw({
request: 'progress',
id: this.fileInfo.id
});
prog = msg.progress;
onprogress([prog, this.fileInfo.size]);
2018-07-13 18:13:09 +00:00
await delay(1000);
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';
2018-07-31 18:09:18 +00:00
this.emit('complete');
2018-07-09 22:39:06 +00:00
this.state = 'complete';
} catch (e) {
this.downloadRequest = null;
if (e === 'cancelled' || e.message === '400') {
2018-07-12 18:22:49 +00:00
throw new Error(0);
}
throw e;
}
2017-06-02 19:38:05 +00:00
}
2018-07-31 18:29:26 +00:00
download(options) {
if (options.stream) {
return this.downloadStream(options.noSave);
}
return this.downloadBlob(options.noSave);
}
}
async function streamToArrayBuffer(stream, size) {
const result = new Uint8Array(size);
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();
}
return result.buffer;
}
async function saveFile(file) {
return new Promise(function(resolve, reject) {
const dataView = new DataView(file.plaintext);
const blob = new Blob([dataView], { type: file.type });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, file.name);
return resolve();
} else if (/iPhone|fxios/i.test(navigator.userAgent)) {
// This method is much slower but createObjectURL
// is buggy on iOS
const reader = new FileReader();
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);
} else {
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = file.name;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
setTimeout(resolve, 100);
}
});
2017-06-02 19:38:05 +00:00
}