upload.js 3.2 KB

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