home.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if(upload.token === undefined)
  59. document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads';
  60. document.getElementById('uploadContainer').appendChild(div);
  61. upload.prepareDropzone();
  62. }
  63. upload.prepareDropzone = function(){
  64. var previewNode = document.querySelector('#template');
  65. previewNode.id = '';
  66. var previewTemplate = previewNode.parentNode.innerHTML;
  67. previewNode.parentNode.removeChild(previewNode);
  68. var dropzone = new Dropzone('div#dropzone', {
  69. url: '/api/upload',
  70. paramName: 'files[]',
  71. maxFilesize: upload.maxFileSize.slice(0, -2),
  72. parallelUploads: 2,
  73. uploadMultiple: false,
  74. previewsContainer: 'div#uploads',
  75. previewTemplate: previewTemplate,
  76. createImageThumbnails: false,
  77. maxFiles: 1000,
  78. autoProcessQueue: true,
  79. headers: {
  80. 'token': upload.token
  81. },
  82. init: function() {
  83. this.on('addedfile', function(file) {
  84. myDropzone = this;
  85. document.getElementById('uploads').style.display = 'block';
  86. });
  87. }
  88. });
  89. // Update the total progress bar
  90. dropzone.on('uploadprogress', function(file, progress) {
  91. file.previewElement.querySelector('.progress').setAttribute('value', progress);
  92. file.previewElement.querySelector('.progress').innerHTML = progress + '%';
  93. });
  94. dropzone.on('success', function(file, response) {
  95. // Handle the responseText here. For example, add the text to the preview element:
  96. if(response.success === false){
  97. var span = document.createElement('span');
  98. span.innerHTML = response.description;
  99. file.previewTemplate.querySelector('.link').appendChild(span);
  100. return;
  101. }
  102. a = document.createElement('a');
  103. a.href = response.files[0].url;
  104. a.target = '_blank';
  105. a.innerHTML = response.files[0].url;
  106. file.previewTemplate.querySelector('.link').appendChild(a);
  107. file.previewTemplate.querySelector('.progress').style.display = 'none';
  108. });
  109. }
  110. //Handle image paste event
  111. window.addEventListener('paste', function(event) {
  112. var items = (event.clipboardData || event.originalEvent.clipboardData).items;
  113. for (index in items) {
  114. var item = items[index];
  115. if (item.kind === 'file') {
  116. var blob = item.getAsFile();
  117. console.log(blob.type);
  118. var file = new File([blob], "pasted-image."+blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]);
  119. file.type = blob.type;
  120. console.log(file);
  121. myDropzone.addFile(file);
  122. }
  123. }
  124. });
  125. window.onload = function () {
  126. upload.checkIfPublic();
  127. };