filesafe/public/js/home.js

196 lines
5.5 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-04-23 13:19:31 +00:00
// add the album var to the upload so we can store the album id in there
upload.album;
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) {
2017-04-27 07:45:15 +00:00
swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
return 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) {
2017-04-27 07:45:15 +00:00
swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
return console.log(error);
});
2017-01-14 22:24:32 +00:00
2017-01-17 19:54:25 +00:00
}
upload.prepareUpload = function(){
2017-04-23 13:19:31 +00:00
// I think this fits best here because we need to check for a valid token before we can get the albums
if (upload.token) {
2017-04-27 07:47:08 +00:00
var select = document.getElementById('albumSelect');
2017-04-23 13:19:31 +00:00
2017-04-27 07:37:30 +00:00
select.addEventListener('change', function() {
upload.album = select.value;
});
axios.get('/api/albums', { headers: { token: upload.token }})
2017-04-23 13:19:31 +00:00
.then(function(res) {
var albums = res.data.albums;
2017-04-23 13:19:31 +00:00
// if the user doesn't have any albums we don't really need to display
// an album selection
if (albums.length === 0) return;
// loop through the albums and create an option for each album
for (var i = 0; i < albums.length; i++) {
var opt = document.createElement('option');
opt.value = albums[i].id;
opt.innerHTML = albums[i].name;
select.appendChild(opt);
}
2017-04-23 13:19:31 +00:00
// display the album selection
document.getElementById('albumDiv').style.display = 'block';
})
2017-04-23 13:19:31 +00:00
.catch(function(e) {
2017-04-27 07:45:15 +00:00
swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
return 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-04-23 13:19:31 +00:00
// add the selected albumid, if an album is selected, as a header
this.on('sending', function(file, xhr) {
if (upload.album) {
xhr.setRequestHeader('albumid', upload.album)
}
});
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-04-23 13:19:31 +00:00
};