1
0

files_drop.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $('#drop-upload-done-indicator').addClass('hidden');
  41. $('#drop-upload-progress-indicator').removeClass('hidden');
  42. $('#drop-uploaded-files').append(output({isUploading: true, name: data.files[0].name}));
  43. data.submit();
  44. return true;
  45. },
  46. updateFileItem: function (fileName, fileItem) {
  47. $('#drop-uploaded-files li[data-name="' + fileName + '"]').replaceWith(fileItem);
  48. },
  49. initialize: function () {
  50. $(document).bind('drop dragover', function (e) {
  51. // Prevent the default browser drop action:
  52. e.preventDefault();
  53. });
  54. var output = this.template();
  55. var self = this;
  56. $('#public-upload').fileupload({
  57. type: 'PUT',
  58. dropZone: $('#public-upload'),
  59. sequentialUploads: true,
  60. start: function(e) {
  61. self._uploading = true;
  62. },
  63. stop: function(e) {
  64. self._uploading = false;
  65. },
  66. add: function(e, data) {
  67. Drop.addFileToUpload(e, data);
  68. $('#drop-upload-status').text(t('files_sharing', 'Waiting…'));
  69. //we return true to keep trying to upload next file even
  70. //if addFileToUpload did not like the previous one
  71. return true;
  72. },
  73. done: function(e, data) {
  74. // Created
  75. var mimeTypeUrl = OC.MimeType.getIconUrl(data.files[0].type);
  76. var fileItem = output({isUploading: false, iconSrc: mimeTypeUrl, name: data.files[0].name});
  77. Drop.updateFileItem(data.files[0].name, fileItem);
  78. },
  79. fail: function(e, data) {
  80. OC.Notification.showTemporary(OC.L10N.translate(
  81. 'files_sharing',
  82. 'Could not upload "{filename}"',
  83. {filename: data.files[0].name}
  84. ));
  85. $('#drop-upload-status').text(t('files_sharing', 'error'));
  86. var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
  87. var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
  88. Drop.updateFileItem(data.files[0].name, fileItem);
  89. },
  90. progressall: function (e, data) {
  91. var progress = parseInt(data.loaded / data.total * 100, 10);
  92. if(progress === 100) {
  93. $('#drop-upload-done-indicator').removeClass('hidden');
  94. $('#drop-upload-progress-indicator').addClass('hidden');
  95. } else {
  96. $('#drop-upload-done-indicator').addClass('hidden');
  97. $('#drop-upload-progress-indicator').removeClass('hidden');
  98. }
  99. },
  100. progress: function (e, data) {
  101. var progress = parseInt(data.loaded / data.total * 100, 10);
  102. if(progress === 100) {
  103. $('#drop-upload-progress-bar').val(100);
  104. $('#drop-upload-status').text(t('files_sharing', 'finished'));
  105. } else {
  106. $('#drop-upload-progress-bar').val(progress);
  107. $('#drop-upload-status').text(progress + '%');
  108. }
  109. },
  110. });
  111. $('#public-upload .button.icon-upload').click(function(e) {
  112. e.preventDefault();
  113. $('#public-upload .emptycontent input').focus().trigger('click');
  114. });
  115. window.onbeforeunload = function() {
  116. return self.confirmBeforeUnload();
  117. }
  118. },
  119. /**
  120. * @returns {Function} from Handlebars
  121. * @private
  122. */
  123. template: function () {
  124. return OCA.Sharing.Templates['files_drop'];
  125. },
  126. confirmBeforeUnload: function() {
  127. if (this._uploading) {
  128. return t('files', 'This will stop your current uploads.')
  129. }
  130. },
  131. };
  132. OCA.FilesSharingDrop = Drop;
  133. window.addEventListener('DOMContentLoaded', function() {
  134. if($('#upload-only-interface').val() === "1") {
  135. $('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
  136. }
  137. OCA.FilesSharingDrop.initialize();
  138. });
  139. })(jQuery);