upload.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. alert(json.description);
  35. if(reloadOnError){
  36. localStorage.removeItem("token");
  37. location.reload();
  38. }
  39. return;
  40. }
  41. localStorage.token = token;
  42. upload.token = token;
  43. return upload.prepareUpload();
  44. }
  45. }
  46. xhr.open('GET', '/api/token/verify', true);
  47. xhr.setRequestHeader('type', 'client');
  48. xhr.setRequestHeader('token', token);
  49. xhr.send(null);
  50. }
  51. upload.prepareUpload = function(){
  52. div = document.createElement('div');
  53. div.id = 'dropzone';
  54. div.innerHTML = 'Click here or drag and drop files';
  55. div.style.display = 'flex';
  56. document.getElementById('tokenContainer').style.display = 'none';
  57. document.getElementById('uploadContainer').appendChild(div);
  58. document.getElementById('panel').style.display = 'block';
  59. upload.prepareDropzone();
  60. }
  61. upload.prepareDropzone = function(){
  62. var previewNode = document.querySelector('#template');
  63. previewNode.id = '';
  64. var previewTemplate = previewNode.parentNode.innerHTML;
  65. previewNode.parentNode.removeChild(previewNode);
  66. var dropzone = new Dropzone('div#dropzone', {
  67. url: '/api/upload',
  68. paramName: 'files[]',
  69. maxFilesize: upload.maxFileSize.slice(0, -2),
  70. parallelUploads: 2,
  71. uploadMultiple: false,
  72. previewsContainer: 'div#uploads',
  73. previewTemplate: previewTemplate,
  74. createImageThumbnails: false,
  75. maxFiles: 1000,
  76. autoProcessQueue: true,
  77. headers: {
  78. 'auth': upload.token
  79. },
  80. init: function() {
  81. this.on('addedfile', function(file) {
  82. document.getElementById('uploads').style.display = 'block';
  83. });
  84. }
  85. });
  86. // Update the total progress bar
  87. dropzone.on('uploadprogress', function(file, progress) {
  88. file.previewElement.querySelector('.progress').style.width = progress + '%';
  89. });
  90. dropzone.on('success', function(file, response) {
  91. // Handle the responseText here. For example, add the text to the preview element:
  92. if(response.success === false){
  93. var span = document.createElement('span');
  94. span.innerHTML = response.description;
  95. file.previewTemplate.querySelector('.link').appendChild(span);
  96. return;
  97. }
  98. a = document.createElement('a');
  99. a.href = response.files[0].url;
  100. a.target = '_blank';
  101. a.innerHTML = response.files[0].url;
  102. file.previewTemplate.querySelector('.link').appendChild(a);
  103. file.previewTemplate.querySelector('.progress').style.display = 'none';
  104. });
  105. }
  106. window.onload = function () {
  107. upload.checkIfPublic();
  108. };