upload.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var upload = {};
  2. upload.isPrivate = true;
  3. upload.token = localStorage.token;
  4. upload.maxFileSize;
  5. upload.checkIfPublic = function(){
  6. axios.get('/api/check')
  7. .then(function (response) {
  8. upload.isPrivate= response.data.private;
  9. upload.maxFileSize = response.data.maxFileSize;
  10. upload.preparePage();
  11. })
  12. .catch(function (error) {
  13. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  14. console.log(error);
  15. });
  16. }
  17. upload.preparePage = function(){
  18. if(!upload.isPrivate) return upload.prepareUpload();
  19. if(!upload.token){
  20. document.getElementById('tokenSubmit').addEventListener('click', function(){
  21. upload.verifyToken(document.getElementById('token').value)
  22. });
  23. document.getElementById('tokenContainer').style.display = 'flex';
  24. return;
  25. }
  26. upload.verifyToken(upload.token, true);
  27. }
  28. upload.verifyToken = function(token, reloadOnError){
  29. if(reloadOnError === undefined)
  30. reloadOnError = false;
  31. axios.post('/api/tokens/verify', {
  32. type: 'client',
  33. token: token
  34. })
  35. .then(function (response) {
  36. if(response.data.success === false){
  37. swal({
  38. title: "An error ocurred",
  39. text: response.data.description,
  40. type: "error"
  41. }, function(){
  42. if(reloadOnError){
  43. localStorage.removeItem("token");
  44. location.reload();
  45. }
  46. })
  47. return;
  48. }
  49. localStorage.token = token;
  50. upload.token = token;
  51. return upload.prepareUpload();
  52. })
  53. .catch(function (error) {
  54. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  55. console.log(error);
  56. });
  57. }
  58. upload.prepareUpload = function(){
  59. div = document.createElement('div');
  60. div.id = 'dropzone';
  61. div.innerHTML = 'Click here or drag and drop files';
  62. div.style.display = 'flex';
  63. document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize;
  64. document.getElementById('tokenContainer').style.display = 'none';
  65. document.getElementById('uploadContainer').appendChild(div);
  66. upload.prepareDropzone();
  67. }
  68. upload.prepareDropzone = function(){
  69. var previewNode = document.querySelector('#template');
  70. previewNode.id = '';
  71. var previewTemplate = previewNode.parentNode.innerHTML;
  72. previewNode.parentNode.removeChild(previewNode);
  73. var dropzone = new Dropzone('div#dropzone', {
  74. url: '/api/upload',
  75. paramName: 'files[]',
  76. maxFilesize: upload.maxFileSize.slice(0, -2),
  77. parallelUploads: 2,
  78. uploadMultiple: false,
  79. previewsContainer: 'div#uploads',
  80. previewTemplate: previewTemplate,
  81. createImageThumbnails: false,
  82. maxFiles: 1000,
  83. autoProcessQueue: true,
  84. headers: {
  85. 'auth': upload.token
  86. },
  87. init: function() {
  88. this.on('addedfile', function(file) {
  89. document.getElementById('uploads').style.display = 'block';
  90. });
  91. }
  92. });
  93. // Update the total progress bar
  94. dropzone.on('uploadprogress', function(file, progress) {
  95. file.previewElement.querySelector('.progress').setAttribute('value', progress);
  96. file.previewElement.querySelector('.progress').innerHTML = progress + '%';
  97. });
  98. dropzone.on('success', function(file, response) {
  99. // Handle the responseText here. For example, add the text to the preview element:
  100. if(response.success === false){
  101. var span = document.createElement('span');
  102. span.innerHTML = response.description;
  103. file.previewTemplate.querySelector('.link').appendChild(span);
  104. return;
  105. }
  106. a = document.createElement('a');
  107. a.href = response.files[0].url;
  108. a.target = '_blank';
  109. a.innerHTML = response.files[0].url;
  110. file.previewTemplate.querySelector('.link').appendChild(a);
  111. file.previewTemplate.querySelector('.progress').style.display = 'none';
  112. });
  113. }
  114. window.onload = function () {
  115. upload.checkIfPublic();
  116. };