Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Warn user before closing when uploading a file on a upload only link
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Sep 3, 2019
commit f12a99e5e2434110a9f9ce5d84dfd4956f1a9411
41 changes: 30 additions & 11 deletions apps/files_sharing/js/files_drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
var Drop = {
/** @type {Function} **/
_template: undefined,


/** @type {bool} */
_uploading: false,

addFileToUpload: function(e, data) {
var errors = [];
var output = this.template();

var filesClient = new OC.Files.Client({
host: OC.getHost(),
port: OC.getPort(),
Expand All @@ -27,7 +30,7 @@
root: OC.getRootPath() + '/public.php/webdav',
useHTTPS: OC.getProtocol() === 'https'
});

// We only process one file at a time 🤷‍♀️
var name = data.files[0].name;
// removing unwanted characters
Expand All @@ -43,46 +46,53 @@
}
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');

$('#drop-uploaded-files').append(output({isUploading: true, name: data.files[0].name}));
$('[data-toggle="tooltip"]').tooltip();
data.submit();

return true;
},

updateFileItem: function (fileName, fileItem) {
$('#drop-uploaded-files li[data-name="' + fileName + '"]').replaceWith(fileItem);
$('[data-toggle="tooltip"]').tooltip();
},

initialize: function () {
$(document).bind('drop dragover', function (e) {
// Prevent the default browser drop action:
e.preventDefault();
});
var output = this.template();
var self = this;
$('#public-upload').fileupload({
type: 'PUT',
dropZone: $('#public-upload'),
sequentialUploads: true,
start: function(e) {
self._uploading = true;
},
stop: function(e) {
self._uploading = false;
},
add: function(e, data) {
Drop.addFileToUpload(e, data);
//we return true to keep trying to upload next file even
Expand Down Expand Up @@ -120,6 +130,9 @@
e.preventDefault();
$('#public-upload #emptycontent input').focus().trigger('click');
});
window.onbeforeunload = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Syntax

Typically, it is better to use window.addEventListener() and the beforeunload event, instead of onbeforeunload.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes! Especially since we remove inline scripts with csp :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So merge or?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno. @juliushaertl which one should we use? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong oppinion, this code is mainly a copy of what we use here:

window.onbeforeunload = function() {

So I'd say lets get it in and we can cleanup if we might refactor at some point, as I don't see any downside of that code for now

return self.confirmBeforeUnload();
}
},

/**
Expand All @@ -128,7 +141,13 @@
*/
template: function () {
return OCA.Sharing.Templates['files_drop'];
}
},

confirmBeforeUnload: function() {
if (this._uploading) {
return t('files', 'This will stop your current uploads.')
}
},
};

OCA.FilesSharingDrop = Drop;
Expand Down