upload.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var upload = {};
  2. upload.isPrivate = true;
  3. upload.token = localStorage.token;
  4. upload.maxFileSize;
  5. upload.checkIfPublic = function(){
  6. var xhr = new XMLHttpRequest();
  7. xhr.onreadystatechange = function() {
  8. if (xhr.readyState == XMLHttpRequest.DONE) {
  9. upload.isPublic = JSON.parse(xhr.responseText).private;
  10. upload.maxFileSize = JSON.parse(xhr.responseText).maxFileSize;
  11. upload.preparePage();
  12. }
  13. }
  14. xhr.open('GET', '/api/check', true);
  15. xhr.send(null);
  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 = false){
  29. var xhr = new XMLHttpRequest();
  30. xhr.onreadystatechange = function() {
  31. if (xhr.readyState == XMLHttpRequest.DONE) {
  32. var json = JSON.parse(xhr.responseText);
  33. if(json.success === false){
  34. swal({
  35. title: "An error ocurred",
  36. text: json.description,
  37. type: "error"
  38. }, function(){
  39. if(reloadOnError){
  40. localStorage.removeItem("token");
  41. location.reload();
  42. }
  43. })
  44. return;
  45. }
  46. localStorage.token = token;
  47. upload.token = token;
  48. return upload.prepareUpload();
  49. }
  50. }
  51. xhr.open('GET', '/api/tokens/verify', true);
  52. xhr.setRequestHeader('type', 'client');
  53. xhr.setRequestHeader('token', token);
  54. xhr.send(null);
  55. }
  56. upload.prepareUpload = function(){
  57. div = document.createElement('div');
  58. div.id = 'dropzone';
  59. div.innerHTML = 'Click here or drag and drop files';
  60. div.style.display = 'flex';
  61. document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize;
  62. document.getElementById('tokenContainer').style.display = 'none';
  63. document.getElementById('uploadContainer').appendChild(div);
  64. upload.prepareDropzone();
  65. }
  66. upload.prepareDropzone = function(){
  67. var previewNode = document.querySelector('#template');
  68. previewNode.id = '';
  69. var previewTemplate = previewNode.parentNode.innerHTML;
  70. previewNode.parentNode.removeChild(previewNode);
  71. var dropzone = new Dropzone('div#dropzone', {
  72. url: '/api/upload',
  73. paramName: 'files[]',
  74. maxFilesize: upload.maxFileSize.slice(0, -2),
  75. parallelUploads: 2,
  76. uploadMultiple: false,
  77. previewsContainer: 'div#uploads',
  78. previewTemplate: previewTemplate,
  79. createImageThumbnails: false,
  80. maxFiles: 1000,
  81. autoProcessQueue: true,
  82. headers: {
  83. 'auth': upload.token
  84. },
  85. init: function() {
  86. this.on('addedfile', function(file) {
  87. document.getElementById('uploads').style.display = 'block';
  88. });
  89. }
  90. });
  91. // Update the total progress bar
  92. dropzone.on('uploadprogress', function(file, progress) {
  93. file.previewElement.querySelector('.progress').setAttribute('value', progress);
  94. file.previewElement.querySelector('.progress').innerHTML = progress + '%';
  95. });
  96. dropzone.on('success', function(file, response) {
  97. // Handle the responseText here. For example, add the text to the preview element:
  98. if(response.success === false){
  99. var span = document.createElement('span');
  100. span.innerHTML = response.description;
  101. file.previewTemplate.querySelector('.link').appendChild(span);
  102. return;
  103. }
  104. a = document.createElement('a');
  105. a.href = response.files[0].url;
  106. a.target = '_blank';
  107. a.innerHTML = response.files[0].url;
  108. file.previewTemplate.querySelector('.link').appendChild(a);
  109. file.previewTemplate.querySelector('.progress').style.display = 'none';
  110. });
  111. }
  112. window.onload = function () {
  113. upload.checkIfPublic();
  114. };