2017-06-02 03:59:27 +00:00
|
|
|
const FileSender = require('./fileSender');
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let onChange = event => {
|
2017-06-02 03:59:27 +00:00
|
|
|
const file = event.target.files[0];
|
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-02 03:59:27 +00:00
|
|
|
name.innerText = file.name;
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(name);
|
2017-06-01 22:12:30 +00:00
|
|
|
|
|
|
|
let link = document.createElement('a');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(link);
|
|
|
|
|
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('uploaded_files').appendChild(li);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-02 03:59:27 +00:00
|
|
|
const fileSender = new FileSender(file);
|
|
|
|
fileSender.on('progress', percentComplete => {
|
|
|
|
progress.innerText = `Progress: ${percentComplete}%`;
|
|
|
|
});
|
|
|
|
fileSender.upload().then(info => {
|
|
|
|
const url = `${window.location.origin}/${info.fileId}/#${info.secretKey}`;
|
|
|
|
localStorage.setItem(info.fileId, info.deleteToken);
|
|
|
|
link.innerText = url;
|
|
|
|
link.setAttribute('href', url);
|
|
|
|
let btn = document.createElement('button');
|
|
|
|
btn.innerText = 'Delete from server';
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
FileSender.delete(
|
|
|
|
info.fileId,
|
|
|
|
localStorage.getItem(info.fileId)
|
|
|
|
).then(() => {
|
|
|
|
document.getElementById('uploaded_files').removeChild(li);
|
|
|
|
localStorage.removeItem(info.fileId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
li.appendChild(btn);
|
|
|
|
});
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-02 03:59:27 +00:00
|
|
|
window.onChange = onChange;
|