2016-06-09 12:05:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function ($) {
|
2018-10-15 11:54:57 +02:00
|
|
|
|
2016-06-09 12:05:02 +02:00
|
|
|
var Drop = {
|
2016-06-11 15:15:37 +02:00
|
|
|
/** @type {Function} **/
|
|
|
|
_template: undefined,
|
2019-09-03 16:35:34 +02:00
|
|
|
|
|
|
|
/** @type {bool} */
|
|
|
|
_uploading: false,
|
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
addFileToUpload: function(e, data) {
|
|
|
|
var errors = [];
|
|
|
|
var output = this.template();
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2016-10-22 22:13:18 +02:00
|
|
|
var filesClient = new OC.Files.Client({
|
|
|
|
host: OC.getHost(),
|
|
|
|
port: OC.getPort(),
|
|
|
|
userName: $('#sharingToken').val(),
|
|
|
|
// note: password not be required, the endpoint
|
|
|
|
// will recognize previous validation from the session
|
|
|
|
root: OC.getRootPath() + '/public.php/webdav',
|
|
|
|
useHTTPS: OC.getProtocol() === 'https'
|
|
|
|
});
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2018-12-18 09:17:49 +01:00
|
|
|
// We only process one file at a time 🤷♀️
|
2017-04-18 16:53:43 +02:00
|
|
|
var name = data.files[0].name;
|
2018-12-18 09:17:49 +01:00
|
|
|
// removing unwanted characters
|
|
|
|
name = name.replace(/["'#%`]/gm, '');
|
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
try {
|
|
|
|
// FIXME: not so elegant... need to refactor that method to return a value
|
|
|
|
Files.isFileNameValid(name);
|
|
|
|
}
|
|
|
|
catch (errorMessage) {
|
|
|
|
OC.Notification.show(errorMessage, {type: 'error'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var base = OC.getProtocol() + '://' + OC.getHost();
|
|
|
|
data.url = base + OC.getRootPath() + '/public.php/webdav/' + encodeURI(name);
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
data.multipart = false;
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
if (!data.headers) {
|
|
|
|
data.headers = {};
|
|
|
|
}
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
var userName = filesClient.getUserName();
|
|
|
|
var password = filesClient.getPassword();
|
|
|
|
if (userName) {
|
|
|
|
// copy username/password from DAV client
|
|
|
|
data.headers['Authorization'] =
|
|
|
|
'Basic ' + btoa(userName + ':' + (password || ''));
|
|
|
|
}
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
$('#drop-upload-done-indicator').addClass('hidden');
|
|
|
|
$('#drop-upload-progress-indicator').removeClass('hidden');
|
2017-08-18 15:18:20 +02:00
|
|
|
|
2018-12-29 11:53:10 +01:00
|
|
|
$('#drop-uploaded-files').append(output({isUploading: true, name: data.files[0].name}));
|
2017-08-18 15:18:20 +02:00
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
data.submit();
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
return true;
|
|
|
|
},
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-08-18 14:26:15 +02:00
|
|
|
updateFileItem: function (fileName, fileItem) {
|
2018-12-29 11:53:10 +01:00
|
|
|
$('#drop-uploaded-files li[data-name="' + fileName + '"]').replaceWith(fileItem);
|
2017-04-27 08:02:40 +02:00
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
},
|
2019-09-03 16:35:34 +02:00
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
initialize: function () {
|
2016-06-09 12:05:02 +02:00
|
|
|
$(document).bind('drop dragover', function (e) {
|
|
|
|
// Prevent the default browser drop action:
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2016-06-11 15:15:37 +02:00
|
|
|
var output = this.template();
|
2019-09-03 16:35:34 +02:00
|
|
|
var self = this;
|
2016-06-09 17:45:16 +02:00
|
|
|
$('#public-upload').fileupload({
|
2016-10-22 22:13:18 +02:00
|
|
|
type: 'PUT',
|
2016-06-09 17:45:16 +02:00
|
|
|
dropZone: $('#public-upload'),
|
2016-10-22 22:13:18 +02:00
|
|
|
sequentialUploads: true,
|
2019-09-03 16:35:34 +02:00
|
|
|
start: function(e) {
|
|
|
|
self._uploading = true;
|
|
|
|
},
|
|
|
|
stop: function(e) {
|
|
|
|
self._uploading = false;
|
|
|
|
},
|
2016-06-09 17:45:16 +02:00
|
|
|
add: function(e, data) {
|
2017-04-18 16:53:43 +02:00
|
|
|
Drop.addFileToUpload(e, data);
|
2020-04-04 23:09:12 +02:00
|
|
|
$('#drop-upload-status').text(t('files_sharing', 'Waiting…'));
|
2017-04-18 16:53:43 +02:00
|
|
|
//we return true to keep trying to upload next file even
|
|
|
|
//if addFileToUpload did not like the privious one
|
2016-10-22 22:13:18 +02:00
|
|
|
return true;
|
2016-06-09 17:45:16 +02:00
|
|
|
},
|
2016-10-24 15:42:44 +02:00
|
|
|
done: function(e, data) {
|
|
|
|
// Created
|
2017-04-27 07:10:41 +02:00
|
|
|
var mimeTypeUrl = OC.MimeType.getIconUrl(data.files[0].type);
|
2017-08-18 15:14:32 +02:00
|
|
|
var fileItem = output({isUploading: false, iconSrc: mimeTypeUrl, name: data.files[0].name});
|
|
|
|
Drop.updateFileItem(data.files[0].name, fileItem);
|
2017-04-27 07:10:41 +02:00
|
|
|
},
|
2017-08-18 14:08:51 +02:00
|
|
|
fail: function(e, data) {
|
2017-04-27 08:02:40 +02:00
|
|
|
OC.Notification.showTemporary(OC.L10N.translate(
|
|
|
|
'files_sharing',
|
|
|
|
'Could not upload "{filename}"',
|
2017-08-18 15:14:32 +02:00
|
|
|
{filename: data.files[0].name}
|
2017-04-27 08:02:40 +02:00
|
|
|
));
|
2020-03-31 19:47:22 +02:00
|
|
|
$('#drop-upload-status').text(t('files_sharing', 'error'));
|
2017-08-18 14:26:15 +02:00
|
|
|
var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
|
2017-08-18 15:14:32 +02:00
|
|
|
var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
|
|
|
|
Drop.updateFileItem(data.files[0].name, fileItem);
|
2016-06-10 14:47:40 +02:00
|
|
|
},
|
|
|
|
progressall: function (e, data) {
|
|
|
|
var progress = parseInt(data.loaded / data.total * 100, 10);
|
|
|
|
if(progress === 100) {
|
|
|
|
$('#drop-upload-done-indicator').removeClass('hidden');
|
|
|
|
$('#drop-upload-progress-indicator').addClass('hidden');
|
|
|
|
} else {
|
|
|
|
$('#drop-upload-done-indicator').addClass('hidden');
|
|
|
|
$('#drop-upload-progress-indicator').removeClass('hidden');
|
|
|
|
}
|
2020-03-31 16:21:28 +02:00
|
|
|
},
|
|
|
|
progress: function (e, data) {
|
|
|
|
var progress = parseInt(data.loaded / data.total * 100, 10);
|
|
|
|
if(progress === 100) {
|
2020-03-31 20:09:24 +02:00
|
|
|
$('#drop-upload-progress-bar').val(100);
|
|
|
|
$('#drop-upload-status').text(t('files_sharing', 'finished'));
|
|
|
|
} else {
|
|
|
|
$('#drop-upload-progress-bar').val(progress);
|
|
|
|
$('#drop-upload-status').text(progress + '%');
|
|
|
|
}
|
2020-03-31 16:21:28 +02:00
|
|
|
},
|
2016-06-09 12:05:02 +02:00
|
|
|
});
|
2016-06-09 17:45:16 +02:00
|
|
|
$('#public-upload .button.icon-upload').click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$('#public-upload #emptycontent input').focus().trigger('click');
|
|
|
|
});
|
2019-09-03 16:35:34 +02:00
|
|
|
window.onbeforeunload = function() {
|
|
|
|
return self.confirmBeforeUnload();
|
|
|
|
}
|
2016-06-11 15:15:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Function} from Handlebars
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
template: function () {
|
2018-10-15 11:54:57 +02:00
|
|
|
return OCA.Sharing.Templates['files_drop'];
|
2019-09-03 16:35:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
confirmBeforeUnload: function() {
|
|
|
|
if (this._uploading) {
|
|
|
|
return t('files', 'This will stop your current uploads.')
|
|
|
|
}
|
|
|
|
},
|
2016-06-09 12:05:02 +02:00
|
|
|
};
|
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
OCA.FilesSharingDrop = Drop;
|
|
|
|
|
2020-07-20 12:30:35 +02:00
|
|
|
window.addEventListener('DOMContentLoaded', function() {
|
2017-02-14 00:49:05 +01:00
|
|
|
if($('#upload-only-interface').val() === "1") {
|
2016-06-09 12:05:02 +02:00
|
|
|
$('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:53:43 +02:00
|
|
|
OCA.FilesSharingDrop.initialize();
|
2016-06-09 12:05:02 +02:00
|
|
|
});
|
|
|
|
})(jQuery);
|