filesafe/public/js/upload.js

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-01-14 09:16:47 +00:00
window.onload = function () {
2017-01-15 06:15:08 +00:00
var USINGTOKEN;
2017-01-14 22:24:32 +00:00
var maxSize = '512';
2017-01-15 06:15:08 +00:00
// First check to see if the service is using token or not
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
USINGTOKEN = JSON.parse(xhr.responseText).token;
prepareTokenThing();
}
}
xhr.open('GET', '/api/check', true);
xhr.send(null);
function prepareTokenThing(){
if(!USINGTOKEN) return getInfo();
if(!localStorage.token){
document.getElementById('tokenContainer').style.display = 'flex'
document.getElementById('tokenSubmit').addEventListener('click', function(){
getInfo(document.getElementById('token').value)
2017-01-15 06:15:08 +00:00
});
}else{
getInfo(localStorage.token);
}
2017-01-14 22:24:32 +00:00
}
function prepareDropzone(){
var previewNode = document.querySelector('#template');
previewNode.id = '';
2017-01-14 22:24:32 +00:00
var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);
var dropzone = new Dropzone('div#dropzone', {
url: '/api/upload',
2017-01-16 08:35:36 +00:00
paramName: 'files[]',
2017-01-14 22:24:32 +00:00
maxFilesize: maxSize,
parallelUploads: 2,
uploadMultiple: false,
previewsContainer: 'div#uploads',
previewTemplate: previewTemplate,
createImageThumbnails: false,
maxFiles: 1000,
2017-01-14 22:40:50 +00:00
autoProcessQueue: true,
headers: {
'auth': localStorage.token
2017-01-14 22:45:02 +00:00
},
init: function() {
this.on('addedfile', function(file) {
2017-01-14 22:45:02 +00:00
document.getElementById('uploads').style.display = 'block';
});
}
2017-01-14 22:24:32 +00:00
});
// Update the total progress bar
dropzone.on('uploadprogress', function(file, progress) {
file.previewElement.querySelector('.progress').style.width = progress + '%';
2017-01-14 22:24:32 +00:00
});
2017-01-14 09:16:47 +00:00
dropzone.on('success', function(file, response) {
2017-01-14 22:24:32 +00:00
// 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
a = document.createElement('a');
a.href = response.files[0].url;
2017-01-14 22:24:32 +00:00
a.target = '_blank';
a.innerHTML = response.files[0].url;
file.previewTemplate.querySelector('.link').appendChild(a);
file.previewTemplate.querySelector('.progress').style.display = 'none';
2017-01-14 22:24:32 +00:00
});
}
function getInfo(token) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if(xhr.responseText !== 'not-authorized'){
div = document.createElement('div');
div.id = 'dropzone';
div.innerHTML = 'Click here or drag and drop files';
div.style.display = 'flex';
document.getElementById('btnGithub').style.display = 'none';
document.getElementById('tokenContainer').style.display = 'none';
document.getElementById('uploadContainer').appendChild(div);
2017-01-15 06:15:08 +00:00
document.getElementById('panel').style.display = 'block';
if(xhr.responseText.maxFileSize) maxSize = JSON.parse(xhr.responseText).maxFileSize;
2017-01-14 22:24:32 +00:00
if(token) localStorage.token = token;
2017-01-15 06:15:08 +00:00
2017-01-14 22:24:32 +00:00
prepareDropzone();
}
}
}
xhr.open('GET', '/api/info', true);
2017-01-15 06:15:08 +00:00
if(token !== undefined)
xhr.setRequestHeader('auth', token);
2017-01-14 22:24:32 +00:00
xhr.send(null);
}
2017-01-14 09:16:47 +00:00
};