files_drop.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. (function ($) {
  6. var Drop = {
  7. /** @type {Function} **/
  8. _template: undefined,
  9. /** @type {boolean} */
  10. _uploading: false,
  11. addFileToUpload: function(e, data) {
  12. var errors = [];
  13. var output = this.template();
  14. var filesClient = new OC.Files.Client({
  15. host: OC.getHost(),
  16. port: OC.getPort(),
  17. // note: password not be required, the endpoint
  18. // will recognize previous validation from the session
  19. root: OC.getRootPath() + '/public.php/dav/files/' + $('#sharingToken').val() + '/',
  20. useHTTPS: OC.getProtocol() === 'https',
  21. });
  22. // We only process one file at a time 🤷‍♀️
  23. var name = data.files[0].name;
  24. // removing unwanted characters
  25. name = name.replace(/["'#%`]/gm, '');
  26. try {
  27. // FIXME: not so elegant... need to refactor that method to return a value
  28. Files.isFileNameValid(name);
  29. }
  30. catch (errorMessage) {
  31. OC.Notification.show(errorMessage, {type: 'error'});
  32. return false;
  33. }
  34. var base = OC.getProtocol() + '://' + OC.getHost();
  35. data.url = base + OC.getRootPath() + '/public.php/dav/files/' + $('#sharingToken').val() + '/' + encodeURI(name);
  36. data.multipart = false;
  37. if (!data.headers) {
  38. data.headers = {};
  39. }
  40. if (localStorage.getItem('nick') !== null) {
  41. data.headers['X-NC-Nickname'] = localStorage.getItem('nick')
  42. }
  43. $('#drop-upload-done-indicator').addClass('hidden');
  44. $('#drop-upload-progress-indicator').removeClass('hidden');
  45. $('#drop-uploaded-files').append(output({isUploading: true, name: data.files[0].name}));
  46. data.submit();
  47. return true;
  48. },
  49. updateFileItem: function (fileName, fileItem) {
  50. $('#drop-uploaded-files li[data-name="' + fileName + '"]').replaceWith(fileItem);
  51. },
  52. initialize: function () {
  53. $(document).bind('drop dragover', function (e) {
  54. // Prevent the default browser drop action:
  55. e.preventDefault();
  56. });
  57. var output = this.template();
  58. var self = this;
  59. $('#public-upload').fileupload({
  60. type: 'PUT',
  61. dropZone: $('#public-upload'),
  62. sequentialUploads: true,
  63. start: function(e) {
  64. self._uploading = true;
  65. },
  66. stop: function(e) {
  67. self._uploading = false;
  68. },
  69. add: function(e, data) {
  70. Drop.addFileToUpload(e, data);
  71. $('#drop-upload-status').text(t('files_sharing', 'Waiting…'));
  72. //we return true to keep trying to upload next file even
  73. //if addFileToUpload did not like the previous one
  74. return true;
  75. },
  76. done: function(e, data) {
  77. // Created
  78. var mimeTypeUrl = OC.MimeType.getIconUrl(data.files[0].type);
  79. var fileItem = output({isUploading: false, iconSrc: mimeTypeUrl, name: data.files[0].name});
  80. Drop.updateFileItem(data.files[0].name, fileItem);
  81. },
  82. fail: function(e, data) {
  83. OC.Notification.showTemporary(OC.L10N.translate(
  84. 'files_sharing',
  85. 'Could not upload "{filename}"',
  86. {filename: data.files[0].name}
  87. ));
  88. $('#drop-upload-status').text(t('files_sharing', 'error'));
  89. var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
  90. var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
  91. Drop.updateFileItem(data.files[0].name, fileItem);
  92. },
  93. progressall: function (e, data) {
  94. var progress = parseInt(data.loaded / data.total * 100, 10);
  95. if(progress === 100) {
  96. $('#drop-upload-done-indicator').removeClass('hidden');
  97. $('#drop-upload-progress-indicator').addClass('hidden');
  98. } else {
  99. $('#drop-upload-done-indicator').addClass('hidden');
  100. $('#drop-upload-progress-indicator').removeClass('hidden');
  101. }
  102. },
  103. progress: function (e, data) {
  104. var progress = parseInt(data.loaded / data.total * 100, 10);
  105. if(progress === 100) {
  106. $('#drop-upload-progress-bar').val(100);
  107. $('#drop-upload-status').text(t('files_sharing', 'finished'));
  108. } else {
  109. $('#drop-upload-progress-bar').val(progress);
  110. $('#drop-upload-status').text(progress + '%');
  111. }
  112. },
  113. });
  114. $('#public-upload .button.icon-upload').click(function(e) {
  115. e.preventDefault();
  116. $('#public-upload .emptycontent input').focus().trigger('click');
  117. });
  118. window.onbeforeunload = function() {
  119. return self.confirmBeforeUnload();
  120. }
  121. },
  122. /**
  123. * @returns {Function} from Handlebars
  124. * @private
  125. */
  126. template: function () {
  127. return OCA.Sharing.Templates['files_drop'];
  128. },
  129. confirmBeforeUnload: function() {
  130. if (this._uploading) {
  131. return t('files', 'This will stop your current uploads.')
  132. }
  133. },
  134. };
  135. OCA.FilesSharingDrop = Drop;
  136. window.addEventListener('DOMContentLoaded', function() {
  137. if($('#upload-only-interface').val() === "1") {
  138. $('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
  139. }
  140. OCA.FilesSharingDrop.initialize();
  141. });
  142. })(jQuery);