2017-06-02 19:38:05 +00:00
|
|
|
const FileReceiver = require('./fileReceiver');
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let download = () => {
|
2017-06-02 19:38:05 +00:00
|
|
|
const fileReceiver = new FileReceiver();
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let li = document.createElement('li');
|
|
|
|
let name = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(name);
|
2017-06-01 22:12:30 +00:00
|
|
|
let progress = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(progress);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
document.getElementById('downloaded_files').appendChild(li);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
fileReceiver.on('progress', percentComplete => {
|
|
|
|
progress.innerText = `Progress: ${percentComplete}%`;
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
if (percentComplete === 100) {
|
2017-06-01 22:12:30 +00:00
|
|
|
let finished = document.createElement('p');
|
|
|
|
finished.innerText = 'Your download has finished.';
|
2017-06-02 19:38:05 +00:00
|
|
|
li.appendChild(finished);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let close = document.createElement('button');
|
|
|
|
close.innerText = 'Ok';
|
|
|
|
close.addEventListener('click', () => {
|
2017-06-02 19:38:05 +00:00
|
|
|
document.getElementById('downloaded_files').removeChild(li);
|
2017-06-01 22:10:00 +00:00
|
|
|
});
|
2017-06-02 19:38:05 +00:00
|
|
|
li.appendChild(close);
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
2017-06-02 19:38:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fileReceiver.download().then(([decrypted, fname]) => {
|
|
|
|
name.innerText = fname;
|
|
|
|
let dataView = new DataView(decrypted);
|
|
|
|
let blob = new Blob([dataView]);
|
|
|
|
let downloadUrl = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.href = downloadUrl;
|
|
|
|
a.download = fname;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
2017-06-02 19:49:56 +00:00
|
|
|
});
|
|
|
|
};
|
2017-06-02 19:38:05 +00:00
|
|
|
|
|
|
|
window.download = download;
|