2017-08-24 23:54:02 +02:00
|
|
|
import Nanobus from 'nanobus';
|
2018-01-24 19:23:13 +01:00
|
|
|
import Keychain from './keychain';
|
2018-08-08 00:40:17 +02:00
|
|
|
import { delay, bytes, streamToArrayBuffer } from './utils';
|
2020-07-27 20:18:52 +02:00
|
|
|
import {
|
|
|
|
downloadFile,
|
|
|
|
downloadDone,
|
|
|
|
metadata,
|
|
|
|
getApiUrl,
|
|
|
|
reportLink,
|
|
|
|
getDownloadToken
|
|
|
|
} from './api';
|
2018-07-31 20:29:26 +02:00
|
|
|
import { blobStream } from './streams';
|
|
|
|
import Zip from './zip';
|
2017-06-02 21:38:05 +02:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
export default class FileReceiver extends Nanobus {
|
2018-01-24 19:23:13 +01:00
|
|
|
constructor(fileInfo) {
|
2017-08-24 23:54:02 +02:00
|
|
|
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);
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
2018-01-24 19:23:13 +01:00
|
|
|
this.fileInfo = fileInfo;
|
2020-07-27 20:18:52 +02:00
|
|
|
this.dlToken = null;
|
2018-02-05 03:30:33 +01:00
|
|
|
this.reset();
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:18:52 +02:00
|
|
|
get id() {
|
|
|
|
return this.fileInfo.id;
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
get progressRatio() {
|
|
|
|
return this.progress[0] / this.progress[1];
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:59:06 +01:00
|
|
|
get progressIndefinite() {
|
|
|
|
return this.state !== 'downloading';
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
get sizes() {
|
|
|
|
return {
|
|
|
|
partialSize: bytes(this.progress[0]),
|
|
|
|
totalSize: bytes(this.progress[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
2018-02-24 20:24:12 +01:00
|
|
|
if (this.downloadRequest) {
|
|
|
|
this.downloadRequest.cancel();
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
2017-08-31 18:43:36 +02: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);
|
2018-02-24 20:24:12 +01:00
|
|
|
this.fileInfo.name = meta.name;
|
|
|
|
this.fileInfo.type = meta.type;
|
2018-11-16 22:33:40 +01:00
|
|
|
this.fileInfo.size = +meta.size;
|
2018-07-26 07:26:11 +02:00
|
|
|
this.fileInfo.manifest = meta.manifest;
|
2020-07-13 19:21:28 +02:00
|
|
|
this.fileInfo.flagged = meta.flagged;
|
2018-02-24 20:24:12 +01:00
|
|
|
this.state = 'ready';
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 00:36:09 +02:00
|
|
|
async reportLink(reason) {
|
|
|
|
await reportLink(this.fileInfo.id, this.keychain, reason);
|
|
|
|
}
|
|
|
|
|
2018-07-10 00:39:06 +02:00
|
|
|
sendMessageToSw(msg) {
|
2018-07-10 02:00:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-07-10 00:39:06 +02:00
|
|
|
const channel = new MessageChannel();
|
|
|
|
|
|
|
|
channel.port1.onmessage = function(event) {
|
2018-07-12 01:52:46 +02:00
|
|
|
if (event.data === undefined) {
|
|
|
|
reject('bad response from serviceWorker');
|
|
|
|
} else if (event.data.error !== undefined) {
|
2018-07-10 00:39:06 +02:00
|
|
|
reject(event.data.error);
|
|
|
|
} else {
|
|
|
|
resolve(event.data);
|
|
|
|
}
|
2018-07-10 02:00:19 +02:00
|
|
|
};
|
2018-07-12 01:52:46 +02:00
|
|
|
|
2018-07-10 02:00:19 +02:00
|
|
|
navigator.serviceWorker.controller.postMessage(msg, [channel.port2]);
|
2018-07-10 00:39:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:29:26 +02:00
|
|
|
async downloadBlob(noSave = false) {
|
|
|
|
this.state = 'downloading';
|
|
|
|
this.downloadRequest = await downloadFile(
|
|
|
|
this.fileInfo.id,
|
2020-07-27 20:18:52 +02:00
|
|
|
this.dlToken,
|
2018-07-31 20:29:26 +02:00
|
|
|
p => {
|
2018-11-16 22:33:40 +01:00
|
|
|
this.progress = [p, this.fileInfo.size];
|
2018-07-31 20:29:26 +02:00
|
|
|
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';
|
2018-09-04 20:08:44 +02:00
|
|
|
this.emit('complete');
|
2018-07-31 20:29:26 +02:00
|
|
|
this.state = 'complete';
|
|
|
|
} catch (e) {
|
|
|
|
this.downloadRequest = null;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadStream(noSave = false) {
|
2019-08-08 22:54:02 +02:00
|
|
|
const start = Date.now();
|
2018-07-05 21:40:49 +02:00
|
|
|
const onprogress = p => {
|
2018-11-16 22:33:40 +01:00
|
|
|
this.progress = [p, this.fileInfo.size];
|
2018-07-05 21:40:49 +02:00
|
|
|
this.emit('progress');
|
2018-07-07 00:49:50 +02:00
|
|
|
};
|
2018-06-21 02:05:33 +02:00
|
|
|
|
2018-07-10 00:39:06 +02:00
|
|
|
this.downloadRequest = {
|
|
|
|
cancel: () => {
|
2018-07-12 01:52:46 +02:00
|
|
|
this.sendMessageToSw({ request: 'cancel', id: this.fileInfo.id });
|
2018-07-10 00:39:06 +02:00
|
|
|
}
|
2018-07-10 02:00:19 +02:00
|
|
|
};
|
2018-07-10 00:39:06 +02:00
|
|
|
|
2018-02-24 20:24:12 +01:00
|
|
|
try {
|
2018-07-05 21:40:49 +02:00
|
|
|
this.state = 'downloading';
|
|
|
|
|
2018-07-07 00:49:50 +02:00
|
|
|
const info = {
|
2018-07-12 01:52:46 +02:00
|
|
|
request: 'init',
|
|
|
|
id: this.fileInfo.id,
|
2018-07-07 00:49:50 +02:00
|
|
|
filename: this.fileInfo.name,
|
2018-07-13 20:13:09 +02:00
|
|
|
type: this.fileInfo.type,
|
2018-07-26 07:26:11 +02:00
|
|
|
manifest: this.fileInfo.manifest,
|
2018-07-12 01:52:46 +02:00
|
|
|
key: this.fileInfo.secretKey,
|
|
|
|
requiresPassword: this.fileInfo.requiresPassword,
|
|
|
|
password: this.fileInfo.password,
|
|
|
|
url: this.fileInfo.url,
|
2018-07-13 00:32:07 +02:00
|
|
|
size: this.fileInfo.size,
|
2018-07-19 22:20:10 +02:00
|
|
|
nonce: this.keychain.nonce,
|
2020-07-27 20:18:52 +02:00
|
|
|
dlToken: this.dlToken,
|
2018-07-12 01:52:46 +02:00
|
|
|
noSave
|
2018-07-07 00:49:50 +02:00
|
|
|
};
|
2018-07-10 00:39:06 +02:00
|
|
|
await this.sendMessageToSw(info);
|
2018-06-29 18:36:08 +02:00
|
|
|
|
2018-11-16 22:33:40 +01:00
|
|
|
onprogress(0);
|
2018-06-21 02:05:33 +02:00
|
|
|
|
2018-07-12 01:52:46 +02:00
|
|
|
if (noSave) {
|
2018-11-19 21:10:57 +01:00
|
|
|
const res = await fetch(getApiUrl(`/api/download/${this.fileInfo.id}`));
|
2018-07-12 01:52:46 +02:00
|
|
|
if (res.status !== 200) {
|
|
|
|
throw new Error(res.status);
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-19 21:10:57 +01:00
|
|
|
const downloadPath = `/api/download/${this.fileInfo.id}`;
|
|
|
|
let downloadUrl = getApiUrl(downloadPath);
|
|
|
|
if (downloadUrl === downloadPath) {
|
2019-08-08 18:52:22 +02:00
|
|
|
downloadUrl = `${location.protocol}//${location.host}${downloadPath}`;
|
2018-11-19 21:10:57 +01:00
|
|
|
}
|
2018-07-07 00:49:50 +02:00
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = downloadUrl;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
2018-07-12 20:22:49 +02:00
|
|
|
}
|
2018-07-10 00:39:06 +02:00
|
|
|
|
2018-07-12 20:22:49 +02:00
|
|
|
let prog = 0;
|
2019-08-08 22:54:02 +02:00
|
|
|
let hangs = 0;
|
2018-07-12 20:22:49 +02:00
|
|
|
while (prog < this.fileInfo.size) {
|
|
|
|
const msg = await this.sendMessageToSw({
|
|
|
|
request: 'progress',
|
|
|
|
id: this.fileInfo.id
|
|
|
|
});
|
2019-08-08 22:54:02 +02:00
|
|
|
if (msg.progress === prog) {
|
|
|
|
hangs++;
|
|
|
|
} else {
|
|
|
|
hangs = 0;
|
|
|
|
}
|
|
|
|
if (hangs > 30) {
|
|
|
|
// TODO: On Chrome we don't get a cancel
|
|
|
|
// signal so one is indistinguishable from
|
|
|
|
// a hang. We may be able to detect
|
|
|
|
// which end is hung in the service worker
|
|
|
|
// to improve on this.
|
|
|
|
const e = new Error('hung download');
|
|
|
|
e.duration = Date.now() - start;
|
|
|
|
e.size = this.fileInfo.size;
|
|
|
|
e.progress = prog;
|
|
|
|
throw e;
|
|
|
|
}
|
2018-07-12 20:22:49 +02:00
|
|
|
prog = msg.progress;
|
2018-11-16 22:33:40 +01:00
|
|
|
onprogress(prog);
|
2018-07-13 20:13:09 +02:00
|
|
|
await delay(1000);
|
2018-02-21 05:31:27 +01:00
|
|
|
}
|
2018-06-29 18:36:08 +02:00
|
|
|
|
2018-07-10 00:39:06 +02:00
|
|
|
this.downloadRequest = null;
|
|
|
|
this.msg = 'downloadFinish';
|
2018-07-31 20:09:18 +02:00
|
|
|
this.emit('complete');
|
2018-07-10 00:39:06 +02:00
|
|
|
this.state = 'complete';
|
2017-08-31 18:43:36 +02:00
|
|
|
} catch (e) {
|
2018-02-24 20:24:12 +01:00
|
|
|
this.downloadRequest = null;
|
2018-07-25 21:29:19 +02:00
|
|
|
if (e === 'cancelled' || e.message === '400') {
|
2018-07-12 20:22:49 +02:00
|
|
|
throw new Error(0);
|
|
|
|
}
|
2017-08-31 18:43:36 +02:00
|
|
|
throw e;
|
|
|
|
}
|
2017-06-02 21:38:05 +02:00
|
|
|
}
|
2018-07-31 20:29:26 +02:00
|
|
|
|
2020-07-27 20:18:52 +02:00
|
|
|
async download({ stream, storage, noSave }) {
|
|
|
|
this.dlToken = storage.getDownloadToken(this.id);
|
|
|
|
if (!this.dlToken) {
|
|
|
|
this.dlToken = await getDownloadToken(this.id, this.keychain);
|
|
|
|
storage.setDownloadToken(this.id, this.dlToken);
|
|
|
|
}
|
|
|
|
if (stream) {
|
|
|
|
await this.downloadStream(noSave);
|
|
|
|
} else {
|
|
|
|
await this.downloadBlob(noSave);
|
2018-07-31 20:29:26 +02:00
|
|
|
}
|
2020-07-27 20:18:52 +02:00
|
|
|
await downloadDone(this.id, this.dlToken);
|
|
|
|
storage.setDownloadToken(this.id);
|
2018-07-31 20:29:26 +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);
|
|
|
|
}
|
|
|
|
});
|
2017-06-02 21:38:05 +02:00
|
|
|
}
|