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