You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

files_drop.js 5.1KB

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