home.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var upload = {};
  2. upload.isPrivate = true;
  3. upload.token = localStorage.token;
  4. upload.maxFileSize;
  5. // add the album var to the upload so we can store the album id in there
  6. upload.album;
  7. upload.checkIfPublic = function(){
  8. axios.get('/api/check')
  9. .then(function (response) {
  10. upload.isPrivate= response.data.private;
  11. upload.maxFileSize = response.data.maxFileSize;
  12. upload.preparePage();
  13. })
  14. .catch(function (error) {
  15. swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  16. return console.log(error);
  17. });
  18. }
  19. upload.preparePage = function(){
  20. if(!upload.isPrivate) return upload.prepareUpload();
  21. if(!upload.token) return document.getElementById('loginToUpload').style.display = 'inline-flex';
  22. upload.verifyToken(upload.token, true);
  23. }
  24. upload.verifyToken = function(token, reloadOnError){
  25. if(reloadOnError === undefined)
  26. reloadOnError = false;
  27. axios.post('/api/tokens/verify', {
  28. token: token
  29. })
  30. .then(function (response) {
  31. if(response.data.success === false){
  32. swal({
  33. title: "An error ocurred",
  34. text: response.data.description,
  35. type: "error"
  36. }, function(){
  37. if(reloadOnError){
  38. localStorage.removeItem("token");
  39. location.reload();
  40. }
  41. })
  42. return;
  43. }
  44. localStorage.token = token;
  45. upload.token = token;
  46. return upload.prepareUpload();
  47. })
  48. .catch(function (error) {
  49. swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  50. return console.log(error);
  51. });
  52. }
  53. upload.prepareUpload = function(){
  54. // I think this fits best here because we need to check for a valid token before we can get the albums
  55. if (upload.token) {
  56. var select = document.getElementById('albumSelect');
  57. select.addEventListener('change', function() {
  58. upload.album = select.value;
  59. });
  60. axios.get('/api/albums', { headers: { token: upload.token }})
  61. .then(function(res) {
  62. var albums = res.data.albums;
  63. // if the user doesn't have any albums we don't really need to display
  64. // an album selection
  65. if (albums.length === 0) return;
  66. // loop through the albums and create an option for each album
  67. for (var i = 0; i < albums.length; i++) {
  68. var opt = document.createElement('option');
  69. opt.value = albums[i].id;
  70. opt.innerHTML = albums[i].name;
  71. select.appendChild(opt);
  72. }
  73. // display the album selection
  74. document.getElementById('albumDiv').style.display = 'block';
  75. })
  76. .catch(function(e) {
  77. swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  78. return console.log(e);
  79. })
  80. }
  81. div = document.createElement('div');
  82. div.id = 'dropzone';
  83. div.innerHTML = 'Click here or drag and drop files';
  84. div.style.display = 'flex';
  85. document.getElementById('maxFileSize').innerHTML = 'Maximum upload size per file is ' + upload.maxFileSize;
  86. document.getElementById('loginToUpload').style.display = 'none';
  87. if(upload.token === undefined)
  88. document.getElementById('loginLinkText').innerHTML = 'Create an account and keep track of your uploads';
  89. document.getElementById('uploadContainer').appendChild(div);
  90. upload.prepareDropzone();
  91. }
  92. upload.prepareDropzone = function(){
  93. var previewNode = document.querySelector('#template');
  94. previewNode.id = '';
  95. var previewTemplate = previewNode.parentNode.innerHTML;
  96. previewNode.parentNode.removeChild(previewNode);
  97. var dropzone = new Dropzone('div#dropzone', {
  98. url: '/api/upload',
  99. paramName: 'files[]',
  100. maxFilesize: upload.maxFileSize.slice(0, -2),
  101. parallelUploads: 2,
  102. uploadMultiple: false,
  103. previewsContainer: 'div#uploads',
  104. previewTemplate: previewTemplate,
  105. createImageThumbnails: false,
  106. maxFiles: 1000,
  107. autoProcessQueue: true,
  108. headers: {
  109. 'token': upload.token
  110. },
  111. init: function() {
  112. this.on('addedfile', function(file) {
  113. myDropzone = this;
  114. document.getElementById('uploads').style.display = 'block';
  115. });
  116. // add the selected albumid, if an album is selected, as a header
  117. this.on('sending', function(file, xhr) {
  118. if (upload.album) {
  119. xhr.setRequestHeader('albumid', upload.album)
  120. }
  121. });
  122. }
  123. });
  124. // Update the total progress bar
  125. dropzone.on('uploadprogress', function(file, progress) {
  126. file.previewElement.querySelector('.progress').setAttribute('value', progress);
  127. file.previewElement.querySelector('.progress').innerHTML = progress + '%';
  128. });
  129. dropzone.on('success', function(file, response) {
  130. // Handle the responseText here. For example, add the text to the preview element:
  131. if(response.success === false){
  132. var span = document.createElement('span');
  133. span.innerHTML = response.description;
  134. file.previewTemplate.querySelector('.link').appendChild(span);
  135. return;
  136. }
  137. a = document.createElement('a');
  138. a.href = response.files[0].url;
  139. a.target = '_blank';
  140. a.innerHTML = response.files[0].url;
  141. file.previewTemplate.querySelector('.link').appendChild(a);
  142. file.previewTemplate.querySelector('.progress').style.display = 'none';
  143. });
  144. }
  145. //Handle image paste event
  146. window.addEventListener('paste', function(event) {
  147. var items = (event.clipboardData || event.originalEvent.clipboardData).items;
  148. for (index in items) {
  149. var item = items[index];
  150. if (item.kind === 'file') {
  151. var blob = item.getAsFile();
  152. console.log(blob.type);
  153. var file = new File([blob], "pasted-image."+blob.type.match(/(?:[^\/]*\/)([^;]*)/)[1]);
  154. file.type = blob.type;
  155. console.log(file);
  156. myDropzone.addFile(file);
  157. }
  158. }
  159. });
  160. window.onload = function () {
  161. upload.checkIfPublic();
  162. };