files_drop.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. addFileToUpload: function(e, data) {
  15. var errors = [];
  16. var output = this.template();
  17. var filesClient = new OC.Files.Client({
  18. host: OC.getHost(),
  19. port: OC.getPort(),
  20. userName: $('#sharingToken').val(),
  21. // note: password not be required, the endpoint
  22. // will recognize previous validation from the session
  23. root: OC.getRootPath() + '/public.php/webdav',
  24. useHTTPS: OC.getProtocol() === 'https'
  25. });
  26. // We only process one file at a time 🤷‍♀️
  27. var name = data.files[0].name;
  28. // removing unwanted characters
  29. name = name.replace(/["'#%`]/gm, '');
  30. try {
  31. // FIXME: not so elegant... need to refactor that method to return a value
  32. Files.isFileNameValid(name);
  33. }
  34. catch (errorMessage) {
  35. OC.Notification.show(errorMessage, {type: 'error'});
  36. return false;
  37. }
  38. var base = OC.getProtocol() + '://' + OC.getHost();
  39. data.url = base + OC.getRootPath() + '/public.php/webdav/' + encodeURI(name);
  40. data.multipart = false;
  41. if (!data.headers) {
  42. data.headers = {};
  43. }
  44. var userName = filesClient.getUserName();
  45. var password = filesClient.getPassword();
  46. if (userName) {
  47. // copy username/password from DAV client
  48. data.headers['Authorization'] =
  49. 'Basic ' + btoa(userName + ':' + (password || ''));
  50. }
  51. $('#drop-upload-done-indicator').addClass('hidden');
  52. $('#drop-upload-progress-indicator').removeClass('hidden');
  53. $('#drop-uploaded-files').append(output({isUploading: true, name: data.files[0].name}));
  54. $('[data-toggle="tooltip"]').tooltip();
  55. data.submit();
  56. return true;
  57. },
  58. updateFileItem: function (fileName, fileItem) {
  59. $('#drop-uploaded-files li[data-name="' + fileName + '"]').replaceWith(fileItem);
  60. $('[data-toggle="tooltip"]').tooltip();
  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. $('#public-upload').fileupload({
  69. type: 'PUT',
  70. dropZone: $('#public-upload'),
  71. sequentialUploads: true,
  72. add: function(e, data) {
  73. Drop.addFileToUpload(e, data);
  74. //we return true to keep trying to upload next file even
  75. //if addFileToUpload did not like the privious 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. var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
  91. var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
  92. Drop.updateFileItem(data.files[0].name, fileItem);
  93. },
  94. progressall: function (e, data) {
  95. var progress = parseInt(data.loaded / data.total * 100, 10);
  96. if(progress === 100) {
  97. $('#drop-upload-done-indicator').removeClass('hidden');
  98. $('#drop-upload-progress-indicator').addClass('hidden');
  99. } else {
  100. $('#drop-upload-done-indicator').addClass('hidden');
  101. $('#drop-upload-progress-indicator').removeClass('hidden');
  102. }
  103. }
  104. });
  105. $('#public-upload .button.icon-upload').click(function(e) {
  106. e.preventDefault();
  107. $('#public-upload #emptycontent input').focus().trigger('click');
  108. });
  109. },
  110. /**
  111. * @returns {Function} from Handlebars
  112. * @private
  113. */
  114. template: function () {
  115. return OCA.Sharing.Templates['files_drop'];
  116. }
  117. };
  118. OCA.FilesSharingDrop = Drop;
  119. $(document).ready(function() {
  120. if($('#upload-only-interface').val() === "1") {
  121. $('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
  122. }
  123. OCA.FilesSharingDrop.initialize();
  124. });
  125. })(jQuery);