upload.js 4.1 KB

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