files_drop.js 4.8 KB

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