drop.chapril.org-firefoxsend/app/fileReceiver.js

159 lines
4.0 KiB
JavaScript
Raw Normal View History

import Nanobus from 'nanobus';
2018-01-24 19:23:13 +01:00
import Keychain from './keychain';
import { bytes } from './utils';
2018-07-05 21:40:49 +02:00
import { metadata, downloadFile, downloadStream } from './api';
2017-06-02 21:38:05 +02:00
export default class FileReceiver extends Nanobus {
2018-01-24 19:23:13 +01:00
constructor(fileInfo) {
super('FileReceiver');
2018-01-24 19:23:13 +01:00
this.keychain = new Keychain(fileInfo.secretKey, fileInfo.nonce);
if (fileInfo.requiresPassword) {
this.keychain.setPassword(fileInfo.password, fileInfo.url);
}
2018-01-24 19:23:13 +01:00
this.fileInfo = fileInfo;
2018-02-05 03:30:33 +01:00
this.reset();
}
get progressRatio() {
return this.progress[0] / this.progress[1];
}
2018-02-21 22:59:06 +01: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 19:23:13 +01:00
}
}
2018-02-05 03:30:33 +01:00
reset() {
this.msg = 'fileSizeProgress';
this.state = 'initialized';
this.progress = [0, 1];
}
2018-01-24 19:23:13 +01: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 21:40:49 +02:00
async streamToArrayBuffer(stream, streamSize, onprogress) {
2018-06-29 18:36:08 +02:00
try {
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();
2018-07-05 21:40:49 +02:00
onprogress([offset, streamSize]);
2018-06-29 18:36:08 +02:00
}
2018-07-05 21:40:49 +02:00
onprogress([streamSize, streamSize]);
2018-06-29 18:36:08 +02:00
return result.slice(0, offset).buffer;
} catch (e) {
2018-07-05 21:40:49 +02:00
console.log(e);
throw (e);
2018-06-29 18:36:08 +02:00
}
2018-06-21 02:05:33 +02:00
}
2018-02-21 05:31:27 +01:00
async download(noSave = false) {
2018-07-05 21:40:49 +02:00
const onprogress = p => {
this.progress = p;
this.emit('progress');
}
2018-06-21 02:05:33 +02:00
try {
2018-07-05 21:40:49 +02:00
this.state = 'downloading';
this.downloadRequest = downloadStream(
this.fileInfo.id,
this.keychain
);
onprogress([0, this.fileInfo.size]);
const download = await this.downloadRequest.result;
const plainstream = this.keychain.decryptStream(download);
2018-06-29 18:36:08 +02:00
2018-07-05 21:40:49 +02:00
//temporary
const plaintext = await this.streamToArrayBuffer(
plainstream,
this.fileInfo.size,
onprogress
);
this.downloadRequest = null;
2018-07-05 21:40:49 +02:00
this.msg = 'decryptingFile';
this.state = 'decrypting';
this.emit('decrypting');
2018-06-21 02:05:33 +02:00
2018-02-21 05:31:27 +01:00
if (!noSave) {
await saveFile({
plaintext,
name: decodeURIComponent(this.fileInfo.name),
type: this.fileInfo.type
});
}
2018-06-29 18:36:08 +02:00
this.msg = 'downloadFinish';
this.state = 'complete';
} catch (e) {
this.downloadRequest = null;
throw e;
}
2017-06-02 21:38:05 +02: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 });
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);
}
});
}