filesafe/public/js/home.js

178 lines
4.7 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(){
axios.get('/api/check')
.then(function (response) {
2017-01-21 19:22:34 +00:00
upload.isPrivate= response.data.private;
upload.maxFileSize = response.data.maxFileSize;
upload.preparePage();
})
.catch(function (error) {
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
console.log(error);
});
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) return document.getElementById('loginToUpload').style.display = 'inline-flex';
2017-01-17 19:54:25 +00:00
upload.verifyToken(upload.token, true);
}
2017-01-14 09:16:47 +00:00
2017-01-22 21:01:39 +00:00
upload.verifyToken = function(token, reloadOnError){
if(reloadOnError === undefined)
reloadOnError = false;
axios.post('/api/tokens/verify', {
token: token
})
.then(function (response) {
2017-01-18 05:40:14 +00:00
if(response.data.success === false){
swal({
title: "An error ocurred",
text: response.data.description,
type: "error"
}, function(){
if(reloadOnError){
localStorage.removeItem("token");
location.reload();
}
})
return;
}
2017-01-18 05:40:14 +00:00
localStorage.token = token;
upload.token = token;
return upload.prepareUpload();
})
.catch(function (error) {
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
console.log(error);
});
2017-01-14 22:24:32 +00:00
2017-01-17 19:54:25 +00:00
}
upload.prepareUpload = function(){
if (upload.token) {
const select = document.querySelector('select');
axios.get('/api/albums', { headers: { token: upload.token }})
.then((res) => {
console.log(res);
let albums = res.data.albums;
if (albums.length === 0) return;
for (let i = 0; i < albums.length; i++) {
let opt = document.createElement('option');
opt.value = albums[i].id;
opt.innerHTML = albums[i].name;
select.appendChild(opt);
}
document.getElementById('albumDiv').style.display = 'block';
})
.catch((e) => {
console.log(e);
})
}
2017-01-17 19:54:25 +00:00
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;
document.getElementById('loginToUpload').style.display = 'none';
2017-01-31 06:37:17 +00:00
if(upload.token === undefined)
document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads';
2017-01-17 19:54:25 +00:00
document.getElementById('uploadContainer').appendChild(div);
2017-01-17 19:54:25 +00:00
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-30 07:41:44 +00:00
'token': upload.token
2017-01-17 19:54:25 +00:00
},
init: function() {
this.on('addedfile', function(file) {
2017-01-30 06:25:34 +00:00
myDropzone = this;
2017-01-17 19:54:25 +00:00
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) {
2017-01-19 01:08:59 +00:00
file.previewElement.querySelector('.progress').setAttribute('value', progress);
file.previewElement.querySelector('.progress').innerHTML = progress + '%';
2017-01-17 19:54:25 +00:00
});
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-30 06:25:34 +00:00
//Handle image paste event
window.addEventListener('paste', function(event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
var blob = item.getAsFile();
console.log(blob.type);
var file = new File([blob], "pasted-image."+blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]);
file.type = blob.type;
console.log(file);
myDropzone.addFile(file);
}
}
});
2017-01-17 19:54:25 +00:00
window.onload = function () {
upload.checkIfPublic();
2017-01-14 09:16:47 +00:00
};