1
0

files_drop.js 5.0 KB

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