2018-07-05 19:40:49 +00:00
|
|
|
import Keychain from './keychain';
|
2018-07-11 23:52:46 +00:00
|
|
|
import { downloadStream } from './api';
|
2018-07-19 21:46:12 +00:00
|
|
|
import { transformStream } from './streams';
|
2018-07-19 19:11:55 +00:00
|
|
|
import contentDisposition from 'content-disposition';
|
2018-07-11 23:52:46 +00:00
|
|
|
|
|
|
|
let noSave = false;
|
|
|
|
const map = new Map();
|
2018-07-05 19:40:49 +00:00
|
|
|
|
2018-07-06 22:49:50 +00:00
|
|
|
self.addEventListener('install', event => {
|
2018-07-05 19:40:49 +00:00
|
|
|
self.skipWaiting();
|
|
|
|
});
|
|
|
|
|
2018-07-11 23:52:46 +00:00
|
|
|
self.addEventListener('activate', event => {
|
|
|
|
self.clients.claim();
|
|
|
|
});
|
|
|
|
|
2018-07-05 19:40:49 +00:00
|
|
|
async function decryptStream(request) {
|
2018-07-11 23:52:46 +00:00
|
|
|
const id = request.url.split('/')[5];
|
|
|
|
try {
|
|
|
|
const file = map.get(id);
|
2018-07-19 20:20:10 +00:00
|
|
|
const keychain = new Keychain(file.key, file.nonce);
|
2018-07-10 00:00:19 +00:00
|
|
|
|
2018-07-18 23:39:14 +00:00
|
|
|
file.download = downloadStream(id, keychain);
|
2018-07-05 19:40:49 +00:00
|
|
|
|
2018-07-18 23:39:14 +00:00
|
|
|
const body = await file.download.result;
|
2018-07-05 19:40:49 +00:00
|
|
|
|
2018-07-19 21:46:12 +00:00
|
|
|
const readStream = transformStream(body, {
|
2018-07-11 23:52:46 +00:00
|
|
|
transform: (chunk, controller) => {
|
|
|
|
file.progress += chunk.length;
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
}
|
|
|
|
});
|
2018-07-18 23:39:14 +00:00
|
|
|
const decrypted = keychain.decryptStream(readStream);
|
2018-07-09 22:39:06 +00:00
|
|
|
|
2018-07-11 23:52:46 +00:00
|
|
|
const headers = {
|
2018-07-19 19:11:55 +00:00
|
|
|
'Content-Disposition': contentDisposition(file.filename),
|
2018-07-13 18:13:09 +00:00
|
|
|
'Content-Type': file.type,
|
2018-07-12 22:32:07 +00:00
|
|
|
'Content-Length': file.size
|
2018-07-11 23:52:46 +00:00
|
|
|
};
|
2018-07-18 23:39:14 +00:00
|
|
|
return new Response(decrypted, { headers });
|
2018-07-11 23:52:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (noSave) {
|
|
|
|
return new Response(null, { status: e.message });
|
2018-07-06 22:49:50 +00:00
|
|
|
}
|
2018-07-05 19:40:49 +00:00
|
|
|
|
2018-07-11 23:52:46 +00:00
|
|
|
const redirectRes = await fetch(`/download/${id}`);
|
|
|
|
return new Response(redirectRes.body, { status: 302 });
|
2018-07-18 23:39:14 +00:00
|
|
|
} finally {
|
|
|
|
// TODO: need to clean up, but not break progress
|
|
|
|
// map.delete(id)
|
2018-07-11 23:52:46 +00:00
|
|
|
}
|
2018-07-05 19:40:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 22:49:50 +00:00
|
|
|
self.onfetch = event => {
|
2018-07-05 19:40:49 +00:00
|
|
|
const req = event.request.clone();
|
|
|
|
if (req.url.includes('/api/download')) {
|
|
|
|
event.respondWith(decryptStream(req));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-06 22:49:50 +00:00
|
|
|
self.onmessage = event => {
|
2018-07-11 23:52:46 +00:00
|
|
|
if (event.data.request === 'init') {
|
|
|
|
noSave = event.data.noSave;
|
|
|
|
const info = {
|
2018-07-18 23:39:14 +00:00
|
|
|
key: event.data.key,
|
2018-07-19 20:20:10 +00:00
|
|
|
nonce: event.data.nonce,
|
2018-07-11 23:52:46 +00:00
|
|
|
filename: event.data.filename,
|
2018-07-13 18:13:09 +00:00
|
|
|
type: event.data.type,
|
2018-07-12 22:32:07 +00:00
|
|
|
size: event.data.size,
|
2018-07-11 23:52:46 +00:00
|
|
|
progress: 0,
|
|
|
|
cancelled: false
|
|
|
|
};
|
|
|
|
if (event.data.requiresPassword) {
|
2018-07-18 23:39:14 +00:00
|
|
|
info.password = event.data.password;
|
|
|
|
info.url = event.data.url;
|
2018-07-11 23:52:46 +00:00
|
|
|
}
|
|
|
|
map.set(event.data.id, info);
|
|
|
|
|
2018-07-10 00:00:19 +00:00
|
|
|
event.ports[0].postMessage('file info received');
|
2018-07-11 23:52:46 +00:00
|
|
|
} else if (event.data.request === 'progress') {
|
|
|
|
const file = map.get(event.data.id);
|
2018-07-18 23:39:14 +00:00
|
|
|
if (!file) {
|
|
|
|
event.ports[0].postMessage({ progress: 0 });
|
|
|
|
} else if (file.cancelled) {
|
2018-07-10 00:00:19 +00:00
|
|
|
event.ports[0].postMessage({ error: 'cancelled' });
|
2018-07-09 22:39:06 +00:00
|
|
|
} else {
|
2018-07-11 23:52:46 +00:00
|
|
|
event.ports[0].postMessage({ progress: file.progress });
|
2018-07-09 22:39:06 +00:00
|
|
|
}
|
2018-07-11 23:52:46 +00:00
|
|
|
} else if (event.data.request === 'cancel') {
|
|
|
|
const file = map.get(event.data.id);
|
2018-07-18 23:39:14 +00:00
|
|
|
if (file) {
|
|
|
|
file.cancelled = true;
|
|
|
|
if (file.download) {
|
|
|
|
file.download.cancel();
|
|
|
|
}
|
2018-07-09 22:39:06 +00:00
|
|
|
}
|
2018-07-10 00:00:19 +00:00
|
|
|
event.ports[0].postMessage('download cancelled');
|
2018-07-09 22:39:06 +00:00
|
|
|
}
|
2018-07-06 22:49:50 +00:00
|
|
|
};
|