fox-send/app/fileSender.js

107 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

import Nanobus from 'nanobus';
2018-01-24 18:23:13 +00:00
import OwnedFile from './ownedFile';
import Keychain from './keychain';
import { arrayToB64, bytes } from './utils';
2018-06-21 00:05:33 +00:00
import { uploadWs } from './api';
import { encryptedSize } from './utils';
export default class FileSender extends Nanobus {
2018-08-31 21:20:15 +00:00
constructor() {
super('FileSender');
2018-01-24 18:23:13 +00:00
this.keychain = new Keychain();
this.reset();
2017-06-02 03:59:27 +00:00
}
get progressRatio() {
return this.progress[0] / this.progress[1];
}
2018-02-21 21:59:06 +00:00
get progressIndefinite() {
2019-02-22 15:42:08 +00:00
return (
['fileSizeProgress', 'notifyUploadEncryptDone'].indexOf(this.msg) === -1
);
2018-02-21 21:59:06 +00:00
}
get sizes() {
return {
partialSize: bytes(this.progress[0]),
totalSize: bytes(this.progress[1])
};
}
reset() {
this.uploadRequest = null;
this.msg = 'importingFile';
this.progress = [0, 1];
this.cancelled = false;
}
2017-07-18 17:52:32 +00:00
cancel() {
this.cancelled = true;
2018-01-24 18:23:13 +00:00
if (this.uploadRequest) {
this.uploadRequest.cancel();
}
2017-07-18 17:52:32 +00:00
}
2019-02-12 19:50:06 +00:00
async upload(archive, bearerToken) {
if (this.cancelled) {
throw new Error(0);
}
this.msg = 'encryptingFile';
2017-08-14 19:04:03 +00:00
this.emit('encrypting');
2019-02-12 19:50:06 +00:00
const totalSize = encryptedSize(archive.size);
const encStream = await this.keychain.encryptStream(archive.stream);
const metadata = await this.keychain.encryptMetadata(archive);
2018-01-24 18:23:13 +00:00
const authKeyB64 = await this.keychain.authKeyB64();
2018-06-21 00:05:33 +00:00
2018-08-08 18:07:09 +00:00
this.uploadRequest = uploadWs(
encStream,
metadata,
authKeyB64,
2019-02-12 19:50:06 +00:00
archive.timeLimit,
archive.dlimit,
2018-08-31 21:20:15 +00:00
bearerToken,
2018-08-08 18:07:09 +00:00
p => {
this.progress = [p, totalSize];
this.emit('progress');
2018-08-07 22:40:17 +00:00
}
2018-08-08 18:07:09 +00:00
);
2018-06-21 00:05:33 +00:00
if (this.cancelled) {
throw new Error(0);
}
2018-01-24 18:23:13 +00:00
this.msg = 'fileSizeProgress';
2018-03-12 19:24:09 +00:00
this.emit('progress'); // HACK to kick MS Edge
try {
2018-01-24 18:23:13 +00:00
const result = await this.uploadRequest.result;
2019-02-22 15:42:08 +00:00
this.msg = 'notifyUploadEncryptDone';
2018-01-24 18:23:13 +00:00
this.uploadRequest = null;
this.progress = [1, 1];
const secretKey = arrayToB64(this.keychain.rawSecret);
2018-01-31 19:12:36 +00:00
const ownedFile = new OwnedFile({
id: result.id,
url: `${result.url}#${secretKey}`,
2019-02-12 19:50:06 +00:00
name: archive.name,
size: archive.size,
manifest: archive.manifest,
time: result.duration,
speed: archive.size / (result.duration / 1000),
2018-01-31 19:12:36 +00:00
createdAt: Date.now(),
2019-02-12 19:50:06 +00:00
expiresAt: Date.now() + archive.timeLimit * 1000,
2018-01-31 19:12:36 +00:00
secretKey: secretKey,
nonce: this.keychain.nonce,
2018-08-08 18:07:09 +00:00
ownerToken: result.ownerToken,
2019-02-12 19:50:06 +00:00
dlimit: archive.dlimit,
timeLimit: archive.timeLimit
2018-01-31 19:12:36 +00:00
});
2018-07-31 18:09:18 +00:00
2018-01-24 18:23:13 +00:00
return ownedFile;
} catch (e) {
2018-01-24 18:23:13 +00:00
this.msg = 'errorPageHeader';
this.uploadRequest = null;
throw e;
}
2017-06-02 03:59:27 +00:00
}
}