upload.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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).private;
  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('tokenSubmit').addEventListener('click', function(){
  18. getInfo(document.getElementById('token').value)
  19. });
  20. return document.getElementById('tokenContainer').style.display = 'flex';
  21. }
  22. getInfo(localStorage.token);
  23. }
  24. function prepareDropzone(){
  25. var previewNode = document.querySelector('#template');
  26. previewNode.id = '';
  27. var previewTemplate = previewNode.parentNode.innerHTML;
  28. previewNode.parentNode.removeChild(previewNode);
  29. var dropzone = new Dropzone('div#dropzone', {
  30. url: '/api/upload',
  31. paramName: 'files[]',
  32. maxFilesize: maxSize,
  33. parallelUploads: 2,
  34. uploadMultiple: false,
  35. previewsContainer: 'div#uploads',
  36. previewTemplate: previewTemplate,
  37. createImageThumbnails: false,
  38. maxFiles: 1000,
  39. autoProcessQueue: true,
  40. headers: {
  41. 'auth': localStorage.token
  42. },
  43. init: function() {
  44. this.on('addedfile', function(file) {
  45. document.getElementById('uploads').style.display = 'block';
  46. });
  47. }
  48. });
  49. // Update the total progress bar
  50. dropzone.on('uploadprogress', function(file, progress) {
  51. file.previewElement.querySelector('.progress').style.width = progress + '%';
  52. });
  53. dropzone.on('success', function(file, response) {
  54. // Handle the responseText here. For example, add the text to the preview element:
  55. if(response.success === false){
  56. var span = document.createElement('span');
  57. span.innerHTML = response.description;
  58. file.previewTemplate.querySelector('.link').appendChild(span);
  59. return;
  60. }
  61. a = document.createElement('a');
  62. a.href = response.files[0].url;
  63. a.target = '_blank';
  64. a.innerHTML = response.files[0].url;
  65. file.previewTemplate.querySelector('.link').appendChild(a);
  66. file.previewTemplate.querySelector('.progress').style.display = 'none';
  67. });
  68. }
  69. function getInfo(token) {
  70. var xhr = new XMLHttpRequest();
  71. xhr.onreadystatechange = function() {
  72. if (xhr.readyState == XMLHttpRequest.DONE) {
  73. if(xhr.responseText === 'not-authorized')
  74. return notAuthorized();
  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. xhr.open('GET', '/api/info', true);
  89. if(token !== undefined)
  90. xhr.setRequestHeader('auth', token);
  91. xhr.send(null);
  92. }
  93. function notAuthorized() {
  94. localStorage.removeItem("token");
  95. location.reload();
  96. }
  97. };