2017-08-24 21:54:02 +00:00
|
|
|
import Nanobus from 'nanobus';
|
2018-01-24 18:23:13 +00:00
|
|
|
import Keychain from './keychain';
|
|
|
|
import { bytes } from './utils';
|
|
|
|
import { metadata, downloadFile } from './api';
|
2017-06-02 19:38:05 +00:00
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
export default class FileReceiver extends Nanobus {
|
2018-01-24 18:23:13 +00:00
|
|
|
constructor(fileInfo) {
|
2017-08-24 21:54:02 +00:00
|
|
|
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);
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
2018-01-24 18:23:13 +00:00
|
|
|
this.fileInfo = fileInfo;
|
2018-02-05 02:30:33 +00:00
|
|
|
this.reset();
|
2017-08-24 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get progressRatio() {
|
|
|
|
return this.progress[0] / this.progress[1];
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:59:06 +00:00
|
|
|
get progressIndefinite() {
|
|
|
|
return this.state !== 'downloading';
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
get sizes() {
|
|
|
|
return {
|
|
|
|
partialSize: bytes(this.progress[0]),
|
|
|
|
totalSize: bytes(this.progress[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
2018-02-24 19:24:12 +00:00
|
|
|
if (this.downloadRequest) {
|
|
|
|
this.downloadRequest.cancel();
|
2018-01-24 18:23:13 +00:00
|
|
|
}
|
2017-08-31 16:43:36 +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);
|
2018-02-24 19:24:12 +00:00
|
|
|
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';
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 20:17:23 +00:00
|
|
|
async streamToArrayBuffer(stream, streamSize) {
|
2018-06-21 00:05:33 +00:00
|
|
|
const reader = stream.getReader();
|
2018-06-25 17:57:52 +00:00
|
|
|
const result = new Uint8Array(streamSize);
|
2018-06-22 20:17:23 +00:00
|
|
|
let offset = 0;
|
2018-06-21 00:05:33 +00:00
|
|
|
|
|
|
|
let state = await reader.read();
|
|
|
|
while (!state.done) {
|
2018-06-22 20:17:23 +00:00
|
|
|
result.set(state.value, offset);
|
|
|
|
offset += state.value.length;
|
2018-06-21 00:05:33 +00:00
|
|
|
state = await reader.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.buffer;
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:31:27 +00:00
|
|
|
async download(noSave = false) {
|
2017-08-31 16:43:36 +00:00
|
|
|
this.state = 'downloading';
|
2018-02-24 19:24:12 +00:00
|
|
|
this.downloadRequest = await downloadFile(
|
|
|
|
this.fileInfo.id,
|
|
|
|
this.keychain,
|
|
|
|
p => {
|
2018-01-24 18:23:13 +00:00
|
|
|
this.progress = p;
|
2018-02-24 19:24:12 +00:00
|
|
|
this.emit('progress');
|
|
|
|
}
|
|
|
|
);
|
2018-06-21 00:05:33 +00:00
|
|
|
|
2018-02-24 19:24:12 +00:00
|
|
|
try {
|
|
|
|
const ciphertext = await this.downloadRequest.result;
|
|
|
|
this.downloadRequest = null;
|
2017-08-31 16:43:36 +00:00
|
|
|
this.msg = 'decryptingFile';
|
2018-02-01 19:42:07 +00:00
|
|
|
this.state = 'decrypting';
|
2017-08-31 16:43:36 +00:00
|
|
|
this.emit('decrypting');
|
2018-06-21 00:05:33 +00:00
|
|
|
|
|
|
|
const dec = await this.keychain.decryptStream(ciphertext);
|
|
|
|
const plainstream = dec.stream;
|
2018-06-22 20:17:23 +00:00
|
|
|
const plaintext = await this.streamToArrayBuffer(
|
|
|
|
plainstream,
|
|
|
|
dec.streamInfo.fileSize
|
|
|
|
);
|
2018-06-21 00:05:33 +00:00
|
|
|
|
2018-02-21 04:31:27 +00:00
|
|
|
if (!noSave) {
|
|
|
|
await saveFile({
|
|
|
|
plaintext,
|
|
|
|
name: decodeURIComponent(this.fileInfo.name),
|
|
|
|
type: this.fileInfo.type
|
|
|
|
});
|
|
|
|
}
|
2018-02-21 22:58:41 +00:00
|
|
|
this.msg = 'downloadFinish';
|
|
|
|
this.state = 'complete';
|
2017-08-31 16:43:36 +00:00
|
|
|
} catch (e) {
|
2018-02-24 19:24:12 +00:00
|
|
|
this.downloadRequest = null;
|
2017-08-31 16:43:36 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2017-06-02 19:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-21 22:58:41 +00:00
|
|
|
|
|
|
|
async function saveFile(file) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
const dataView = new DataView(file.plaintext);
|
|
|
|
const blob = new Blob([dataView], { type: file.type });
|
|
|
|
|
2018-02-21 23:43:32 +00:00
|
|
|
if (navigator.msSaveBlob) {
|
|
|
|
navigator.msSaveBlob(blob, file.name);
|
2018-02-21 22:58:41 +00:00
|
|
|
return resolve();
|
2018-02-21 23:43:32 +00:00
|
|
|
} 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);
|
2018-02-21 22:58:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|