upload.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var upload = {};
  2. upload.isPrivate = true;
  3. upload.token = localStorage.token;
  4. upload.maxFileSize;
  5. upload.checkIfPublic = function(){
  6. axios.get('/api/check')
  7. .then(function (response) {
  8. upload.isPrivate= response.data.private;
  9. upload.maxFileSize = response.data.maxFileSize;
  10. upload.preparePage();
  11. })
  12. .catch(function (error) {
  13. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  14. console.log(error);
  15. });
  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){
  29. if(reloadOnError === undefined)
  30. reloadOnError = false;
  31. axios.post('/api/tokens/verify', {
  32. type: 'client',
  33. token: token
  34. })
  35. .then(function (response) {
  36. if(response.data.success === false){
  37. swal({
  38. title: "An error ocurred",
  39. text: response.data.description,
  40. type: "error"
  41. }, function(){
  42. if(reloadOnError){
  43. localStorage.removeItem("token");
  44. location.reload();
  45. }
  46. })
  47. return;
  48. }
  49. localStorage.token = token;
  50. upload.token = token;
  51. return upload.prepareUpload();
  52. })
  53. .catch(function (error) {
  54. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  55. console.log(error);
  56. });
  57. }
  58. upload.prepareUpload = function(){
  59. div = document.createElement('div');
  60. div.id = 'dropzone';
  61. div.innerHTML = 'Click here or drag and drop files';
  62. div.style.display = 'flex';
  63. document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize;
  64. document.getElementById('tokenContainer').style.display = 'none';
  65. document.getElementById('uploadContainer').appendChild(div);
  66. upload.prepareDropzone();
  67. }
  68. upload.prepareDropzone = function(){
  69. var previewNode = document.querySelector('#template');
  70. previewNode.id = '';
  71. var previewTemplate = previewNode.parentNode.innerHTML;
  72. previewNode.parentNode.removeChild(previewNode);
  73. var dropzone = new Dropzone('div#dropzone', {
  74. url: '/api/upload',
  75. paramName: 'files[]',
  76. maxFilesize: upload.maxFileSize.slice(0, -2),
  77. parallelUploads: 2,
  78. uploadMultiple: false,
  79. previewsContainer: 'div#uploads',
  80. previewTemplate: previewTemplate,
  81. createImageThumbnails: false,
  82. maxFiles: 1000,
  83. autoProcessQueue: true,
  84. headers: {
  85. 'auth': upload.token
  86. },
  87. init: function() {
  88. this.on('addedfile', function(file) {
  89. myDropzone = this;
  90. document.getElementById('uploads').style.display = 'block';
  91. });
  92. }
  93. });
  94. // Update the total progress bar
  95. dropzone.on('uploadprogress', function(file, progress) {
  96. file.previewElement.querySelector('.progress').setAttribute('value', progress);
  97. file.previewElement.querySelector('.progress').innerHTML = progress + '%';
  98. });
  99. dropzone.on('success', function(file, response) {
  100. // Handle the responseText here. For example, add the text to the preview element:
  101. if(response.success === false){
  102. var span = document.createElement('span');
  103. span.innerHTML = response.description;
  104. file.previewTemplate.querySelector('.link').appendChild(span);
  105. return;
  106. }
  107. a = document.createElement('a');
  108. a.href = response.files[0].url;
  109. a.target = '_blank';
  110. a.innerHTML = response.files[0].url;
  111. file.previewTemplate.querySelector('.link').appendChild(a);
  112. file.previewTemplate.querySelector('.progress').style.display = 'none';
  113. });
  114. }
  115. //Handle image paste event
  116. window.addEventListener('paste', function(event) {
  117. var items = (event.clipboardData || event.originalEvent.clipboardData).items;
  118. for (index in items) {
  119. var item = items[index];
  120. if (item.kind === 'file') {
  121. var blob = item.getAsFile();
  122. console.log(blob.type);
  123. var file = new File([blob], "pasted-image."+blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]);
  124. file.type = blob.type;
  125. console.log(file);
  126. myDropzone.addFile(file);
  127. }
  128. }
  129. });
  130. window.onload = function () {
  131. upload.checkIfPublic();
  132. };