filesafe/public/js/upload.js

140 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-01-17 19:54:25 +00:00
var upload = {};
2017-01-14 09:16:47 +00:00
2017-01-17 19:54:25 +00:00
upload.isPrivate = true;
upload.token = localStorage.token;
2017-01-17 22:35:18 +00:00
upload.maxFileSize;
2017-01-14 22:24:32 +00:00
2017-01-17 19:54:25 +00:00
upload.checkIfPublic = function(){
2017-01-15 06:15:08 +00:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
2017-01-17 19:54:25 +00:00
upload.isPublic = JSON.parse(xhr.responseText).private;
2017-01-17 22:35:18 +00:00
upload.maxFileSize = JSON.parse(xhr.responseText).maxFileSize;
2017-01-17 19:54:25 +00:00
upload.preparePage();
2017-01-15 06:15:08 +00:00
}
}
xhr.open('GET', '/api/check', true);
xhr.send(null);
2017-01-17 19:54:25 +00:00
}
2017-01-15 06:15:08 +00:00
2017-01-17 19:54:25 +00:00
upload.preparePage = function(){
if(!upload.isPrivate) return upload.prepareUpload();
if(!upload.token){
document.getElementById('tokenSubmit').addEventListener('click', function(){
upload.verifyToken(document.getElementById('token').value)
2017-01-14 22:24:32 +00:00
});
2017-01-17 19:54:25 +00:00
document.getElementById('tokenContainer').style.display = 'flex';
return;
}
upload.verifyToken(upload.token, true);
}
2017-01-14 09:16:47 +00:00
2017-01-17 19:54:25 +00:00
upload.verifyToken = function(token, reloadOnError = false){
var xhr = new XMLHttpRequest();
2017-01-17 19:54:25 +00:00
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var json = JSON.parse(xhr.responseText);
if(json.success === false){
2017-01-18 05:40:14 +00:00
swal({
title: "An error ocurred",
text: json.description,
type: "error"
}, function(){
if(reloadOnError){
localStorage.removeItem("token");
location.reload();
}
})
return;
}
2017-01-17 19:54:25 +00:00
localStorage.token = token;
upload.token = token;
return upload.prepareUpload();
2017-01-14 22:24:32 +00:00
2017-01-17 19:54:25 +00:00
}
2017-01-14 22:24:32 +00:00
}
xhr.open('GET', '/api/tokens/verify', true);
2017-01-17 19:54:25 +00:00
xhr.setRequestHeader('type', 'client');
xhr.setRequestHeader('token', token);
xhr.send(null);
}
upload.prepareUpload = function(){
div = document.createElement('div');
div.id = 'dropzone';
div.innerHTML = 'Click here or drag and drop files';
div.style.display = 'flex';
document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize;
2017-01-17 19:54:25 +00:00
document.getElementById('tokenContainer').style.display = 'none';
document.getElementById('uploadContainer').appendChild(div);
upload.prepareDropzone();
}
upload.prepareDropzone = function(){
var previewNode = document.querySelector('#template');
previewNode.id = '';
var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);
var dropzone = new Dropzone('div#dropzone', {
url: '/api/upload',
paramName: 'files[]',
2017-01-17 22:35:18 +00:00
maxFilesize: upload.maxFileSize.slice(0, -2),
2017-01-17 19:54:25 +00:00
parallelUploads: 2,
uploadMultiple: false,
previewsContainer: 'div#uploads',
previewTemplate: previewTemplate,
createImageThumbnails: false,
maxFiles: 1000,
autoProcessQueue: true,
headers: {
2017-01-17 22:35:18 +00:00
'auth': upload.token
2017-01-17 19:54:25 +00:00
},
init: function() {
this.on('addedfile', function(file) {
document.getElementById('uploads').style.display = 'block';
});
2017-01-17 22:35:18 +00:00
}
2017-01-17 19:54:25 +00:00
});
// Update the total progress bar
dropzone.on('uploadprogress', function(file, progress) {
file.previewElement.querySelector('.progress').style.width = progress + '%';
});
dropzone.on('success', function(file, response) {
// Handle the responseText here. For example, add the text to the preview element:
if(response.success === false){
var span = document.createElement('span');
span.innerHTML = response.description;
file.previewTemplate.querySelector('.link').appendChild(span);
return;
2017-01-14 22:24:32 +00:00
}
2017-01-15 06:15:08 +00:00
2017-01-17 19:54:25 +00:00
a = document.createElement('a');
a.href = response.files[0].url;
a.target = '_blank';
a.innerHTML = response.files[0].url;
file.previewTemplate.querySelector('.link').appendChild(a);
file.previewTemplate.querySelector('.progress').style.display = 'none';
});
2017-01-15 06:15:08 +00:00
2017-01-17 19:54:25 +00:00
}
2017-01-17 03:37:54 +00:00
2017-01-17 19:54:25 +00:00
window.onload = function () {
upload.checkIfPublic();
2017-01-14 09:16:47 +00:00
};