upload.js 3.4 KB

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