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;
|
|
|
|
this.fileDownload = null;
|
2017-08-24 21:54:02 +00:00
|
|
|
this.msg = 'fileSizeProgress';
|
2017-08-31 16:43:36 +00:00
|
|
|
this.state = 'initialized';
|
2017-08-24 21:54:02 +00:00
|
|
|
this.progress = [0, 1];
|
2018-01-24 18:23:13 +00:00
|
|
|
this.cancelled = false;
|
2017-08-24 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get progressRatio() {
|
|
|
|
return this.progress[0] / this.progress[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
get sizes() {
|
|
|
|
return {
|
|
|
|
partialSize: bytes(this.progress[0]),
|
|
|
|
totalSize: bytes(this.progress[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
2018-01-24 18:23:13 +00:00
|
|
|
this.cancelled = true;
|
|
|
|
if (this.fileDownload) {
|
|
|
|
this.fileDownload.cancel();
|
|
|
|
}
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
async getMetadata() {
|
|
|
|
const meta = await metadata(this.fileInfo.id, this.keychain);
|
|
|
|
if (meta) {
|
|
|
|
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;
|
2017-08-31 16:43:36 +00:00
|
|
|
this.state = 'ready';
|
2018-01-24 18:23:13 +00:00
|
|
|
return;
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
2018-01-24 18:23:13 +00:00
|
|
|
this.state = 'invalid';
|
|
|
|
return;
|
2017-08-31 16:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
async download() {
|
2017-08-31 16:43:36 +00:00
|
|
|
this.state = 'downloading';
|
|
|
|
this.emit('progress', this.progress);
|
|
|
|
try {
|
2018-01-24 18:23:13 +00:00
|
|
|
const download = await downloadFile(this.fileInfo.id, this.keychain);
|
|
|
|
download.onprogress = p => {
|
|
|
|
this.progress = p;
|
|
|
|
this.emit('progress', p);
|
|
|
|
};
|
|
|
|
this.fileDownload = download;
|
|
|
|
const ciphertext = await download.result;
|
|
|
|
this.fileDownload = 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-01-24 18:23:13 +00:00
|
|
|
const plaintext = await this.keychain.decryptFile(ciphertext);
|
|
|
|
if (this.cancelled) {
|
|
|
|
throw new Error(0);
|
|
|
|
}
|
2017-08-31 16:43:36 +00:00
|
|
|
this.msg = 'downloadFinish';
|
|
|
|
this.state = 'complete';
|
|
|
|
return {
|
|
|
|
plaintext,
|
2018-01-24 18:23:13 +00:00
|
|
|
name: decodeURIComponent(this.fileInfo.name),
|
|
|
|
type: this.fileInfo.type
|
2017-08-31 16:43:36 +00:00
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
this.state = 'invalid';
|
|
|
|
throw e;
|
|
|
|
}
|
2017-06-02 19:38:05 +00:00
|
|
|
}
|
|
|
|
}
|