2016-06-09 10:05:02 +00: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 ($) {
|
2016-06-11 13:15:37 +00:00
|
|
|
var TEMPLATE =
|
|
|
|
'<li data-toggle="tooltip" title="{{name}}" data-name="{{name}}">' +
|
|
|
|
'{{#if isUploading}}' +
|
|
|
|
'<span class="icon-loading-small"></span> {{name}}' +
|
|
|
|
'{{else}}' +
|
2017-08-18 12:26:15 +00:00
|
|
|
'<img src="{{iconSrc}}"/> {{name}}' +
|
2016-06-11 13:15:37 +00:00
|
|
|
'{{/if}}' +
|
|
|
|
'</li>';
|
2016-06-09 10:05:02 +00:00
|
|
|
var Drop = {
|
2016-06-11 13:15:37 +00:00
|
|
|
/** @type {Function} **/
|
|
|
|
_template: undefined,
|
2017-04-18 14:53:43 +00:00
|
|
|
|
|
|
|
addFileToUpload: function(e, data) {
|
|
|
|
var errors = [];
|
|
|
|
var output = this.template();
|
|
|
|
|
2016-10-22 20:13:18 +00: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'
|
|
|
|
});
|
2017-04-18 14:53:43 +00:00
|
|
|
|
|
|
|
var name = data.files[0].name;
|
|
|
|
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);
|
|
|
|
|
|
|
|
data.multipart = false;
|
|
|
|
|
|
|
|
if (!data.headers) {
|
|
|
|
data.headers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var userName = filesClient.getUserName();
|
|
|
|
var password = filesClient.getPassword();
|
|
|
|
if (userName) {
|
|
|
|
// copy username/password from DAV client
|
|
|
|
data.headers['Authorization'] =
|
|
|
|
'Basic ' + btoa(userName + ':' + (password || ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#drop-upload-done-indicator').addClass('hidden');
|
|
|
|
$('#drop-upload-progress-indicator').removeClass('hidden');
|
2017-08-18 13:18:20 +00:00
|
|
|
|
|
|
|
$('#public-upload ul').append(output({isUploading: true, name: data.files[0].name}));
|
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
data.submit();
|
2017-04-18 14:53:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-08-18 12:26:15 +00:00
|
|
|
updateFileItem: function (fileName, fileItem) {
|
|
|
|
$('#public-upload ul li[data-name="' + fileName + '"]').replaceWith(fileItem);
|
2017-04-27 06:02:40 +00:00
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
},
|
|
|
|
|
2017-04-18 14:53:43 +00:00
|
|
|
initialize: function () {
|
2016-06-09 10:05:02 +00:00
|
|
|
$(document).bind('drop dragover', function (e) {
|
|
|
|
// Prevent the default browser drop action:
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2016-06-11 13:15:37 +00:00
|
|
|
var output = this.template();
|
2016-06-09 15:45:16 +00:00
|
|
|
$('#public-upload').fileupload({
|
2016-10-22 20:13:18 +00:00
|
|
|
type: 'PUT',
|
2016-06-09 15:45:16 +00:00
|
|
|
dropZone: $('#public-upload'),
|
2016-10-22 20:13:18 +00:00
|
|
|
sequentialUploads: true,
|
2016-06-09 15:45:16 +00:00
|
|
|
add: function(e, data) {
|
2017-04-18 14:53:43 +00:00
|
|
|
Drop.addFileToUpload(e, data);
|
|
|
|
//we return true to keep trying to upload next file even
|
|
|
|
//if addFileToUpload did not like the privious one
|
2016-10-22 20:13:18 +00:00
|
|
|
return true;
|
2016-06-09 15:45:16 +00:00
|
|
|
},
|
2016-10-24 13:42:44 +00:00
|
|
|
done: function(e, data) {
|
|
|
|
// Created
|
2017-04-27 05:10:41 +00:00
|
|
|
var mimeTypeUrl = OC.MimeType.getIconUrl(data.files[0].type);
|
2017-08-18 13:14:32 +00:00
|
|
|
var fileItem = output({isUploading: false, iconSrc: mimeTypeUrl, name: data.files[0].name});
|
|
|
|
Drop.updateFileItem(data.files[0].name, fileItem);
|
2017-04-27 05:10:41 +00:00
|
|
|
},
|
2017-08-18 12:08:51 +00:00
|
|
|
fail: function(e, data) {
|
2017-04-27 06:02:40 +00:00
|
|
|
OC.Notification.showTemporary(OC.L10N.translate(
|
|
|
|
'files_sharing',
|
|
|
|
'Could not upload "{filename}"',
|
2017-08-18 13:14:32 +00:00
|
|
|
{filename: data.files[0].name}
|
2017-04-27 06:02:40 +00:00
|
|
|
));
|
2017-08-18 12:26:15 +00:00
|
|
|
var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
|
2017-08-18 13:14:32 +00:00
|
|
|
var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
|
|
|
|
Drop.updateFileItem(data.files[0].name, fileItem);
|
2016-06-10 12:47:40 +00: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');
|
|
|
|
}
|
2016-06-09 10:05:02 +00:00
|
|
|
}
|
|
|
|
});
|
2016-06-09 15:45:16 +00:00
|
|
|
$('#public-upload .button.icon-upload').click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$('#public-upload #emptycontent input').focus().trigger('click');
|
|
|
|
});
|
2016-06-11 13:15:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Function} from Handlebars
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
template: function () {
|
|
|
|
if (!this._template) {
|
|
|
|
this._template = Handlebars.compile(TEMPLATE);
|
|
|
|
}
|
|
|
|
return this._template;
|
2016-06-09 10:05:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-18 14:53:43 +00:00
|
|
|
OCA.FilesSharingDrop = Drop;
|
|
|
|
|
2016-06-09 10:05:02 +00:00
|
|
|
$(document).ready(function() {
|
2017-02-13 23:49:05 +00:00
|
|
|
if($('#upload-only-interface').val() === "1") {
|
2016-06-09 10:05:02 +00:00
|
|
|
$('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:53:43 +00:00
|
|
|
OCA.FilesSharingDrop.initialize();
|
2016-06-09 10:05:02 +00:00
|
|
|
});
|
|
|
|
})(jQuery);
|