fox-send/frontend/src/download.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-06-02 19:38:05 +00:00
const FileReceiver = require('./fileReceiver');
let download = () => {
2017-06-02 19:38:05 +00:00
const fileReceiver = new FileReceiver();
2017-06-01 22:12:30 +00:00
let li = document.createElement('li');
let name = document.createElement('p');
li.appendChild(name);
2017-06-01 22:12:30 +00:00
let progress = document.createElement('p');
li.appendChild(progress);
2017-06-01 22:12:30 +00:00
document.getElementById('downloaded_files').appendChild(li);
2017-06-02 19:38:05 +00:00
fileReceiver.on('progress', percentComplete => {
progress.innerText = `Progress: ${percentComplete}%`;
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: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-02 19:38:05 +00:00
li.appendChild(close);
}
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:38:05 +00:00
window.download = download;