upload.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. a = document.createElement('a');
  57. a.href = response.url;
  58. a.target = '_blank';
  59. a.innerHTML = response.url;
  60. file.previewTemplate.querySelector(".progress").style.display = 'none';
  61. file.previewTemplate.querySelector(".link").appendChild(a);
  62. });
  63. }
  64. function getInfo(token) {
  65. var xhr = new XMLHttpRequest();
  66. xhr.onreadystatechange = function() {
  67. if (xhr.readyState == XMLHttpRequest.DONE) {
  68. if(xhr.responseText !== 'not-authorized'){
  69. div = document.createElement('div');
  70. div.id = 'dropzone';
  71. div.innerHTML = 'Click here or drag and drop files';
  72. div.style.display = 'flex';
  73. document.getElementById('btnGithub').style.display = 'none';
  74. document.getElementById('tokenContainer').style.display = 'none';
  75. document.getElementById('uploadContainer').appendChild(div);
  76. document.getElementById('panel').style.display = 'block';
  77. if(xhr.responseText.maxFileSize) maxSize = JSON.parse(xhr.responseText).maxFileSize;
  78. if(token) localStorage.token = token;
  79. prepareDropzone();
  80. }
  81. }
  82. }
  83. xhr.open('GET', '/api/info', true);
  84. if(token !== undefined)
  85. xhr.setRequestHeader('auth', token);
  86. xhr.send(null);
  87. }
  88. };