From 0c6e154b5044592122ad3c03bdd607e186c2f24e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 4 Sep 2018 11:33:14 +0200 Subject: [PATCH] use dav trash api for removing from trashbin Signed-off-by: Robin Appelman --- apps/files_trashbin/js/app.js | 60 +++++++++++++--------- apps/files_trashbin/js/filelist.js | 80 ++++++++++++------------------ 2 files changed, 67 insertions(+), 73 deletions(-) diff --git a/apps/files_trashbin/js/app.js b/apps/files_trashbin/js/app.js index 7cdc157fe4..78a1c02f78 100644 --- a/apps/files_trashbin/js/app.js +++ b/apps/files_trashbin/js/app.js @@ -17,12 +17,21 @@ OCA.Trashbin = {}; */ OCA.Trashbin.App = { _initialized: false, + /** @type {OC.Files.Client} */ + client: null, - initialize: function($el) { + initialize: function ($el) { if (this._initialized) { return; } this._initialized = true; + + this.client = new OC.Files.Client({ + host: OC.getHost(), + port: OC.getPort(), + root: OC.linkToRemoteBase('dav') + '/trashbin/' + OC.getCurrentUser().uid, + useHTTPS: OC.getProtocol() === 'https' + }); var urlParams = OC.Util.History.parseUrlQuery(); this.fileList = new OCA.Trashbin.FileList( $('#app-content-trashbin'), { @@ -31,22 +40,24 @@ OCA.Trashbin.App = { scrollTo: urlParams.scrollto, config: OCA.Files.App.getFilesConfig(), multiSelectMenu: [ - { - name: 'restore', - displayName: t('files', 'Restore'), - iconClass: 'icon-history', - }, - { - name: 'delete', - displayName: t('files', 'Delete'), - iconClass: 'icon-delete', - } - ] + { + name: 'restore', + displayName: t('files', 'Restore'), + iconClass: 'icon-history', + }, + { + name: 'delete', + displayName: t('files', 'Delete'), + iconClass: 'icon-delete', + } + ], + client: this.client } ); }, - _createFileActions: function() { + _createFileActions: function () { + var client = this.client; var fileActions = new OCA.Files.FileActions(); fileActions.register('dir', 'Open', OC.PERMISSION_READ, '', function (filename, context) { var dir = context.fileList.getCurrentDirectory(); @@ -62,7 +73,7 @@ OCA.Trashbin.App = { mime: 'all', permissions: OC.PERMISSION_READ, iconClass: 'icon-history', - actionHandler: function(filename, context) { + actionHandler: function (filename, context) { var fileList = context.fileList; var tr = fileList.findFileEl(filename); var deleteAction = tr.children("td.date").children(".action.delete"); @@ -82,33 +93,34 @@ OCA.Trashbin.App = { mime: 'all', permissions: OC.PERMISSION_READ, iconClass: 'icon-delete', - render: function(actionSpec, isDefault, context) { + render: function (actionSpec, isDefault, context) { var $actionLink = fileActions._makeActionLink(actionSpec, context); $actionLink.attr('original-title', t('files_trashbin', 'Delete permanently')); $actionLink.children('img').attr('alt', t('files_trashbin', 'Delete permanently')); context.$file.find('td:last').append($actionLink); return $actionLink; }, - actionHandler: function(filename, context) { + actionHandler: function (filename, context) { var fileList = context.fileList; $('.tipsy').remove(); var tr = fileList.findFileEl(filename); var deleteAction = tr.children("td.date").children(".action.delete"); deleteAction.removeClass('icon-delete').addClass('icon-loading-small'); - $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), { - files: JSON.stringify([filename]), - dir: fileList.getCurrentDirectory() - }, - _.bind(fileList._removeCallback, fileList) - ); + client.remove('trash/' + filename) + .then( + fileList._removeCallback.bind(fileList, [filename]), + function () { + OC.Notification.show(t('files_trashbin', 'Error while removing file from trashbin')); + } + ); } }); return fileActions; } }; -$(document).ready(function() { - $('#app-content-trashbin').one('show', function() { +$(document).ready(function () { + $('#app-content-trashbin').one('show', function () { var App = OCA.Trashbin.App; App.initialize($('#app-content-trashbin')); // force breadcrumb init diff --git a/apps/files_trashbin/js/filelist.js b/apps/files_trashbin/js/filelist.js index 543ff53f0b..8df1accb26 100644 --- a/apps/files_trashbin/js/filelist.js +++ b/apps/files_trashbin/js/filelist.js @@ -38,6 +38,7 @@ * @param [options] map of options */ var FileList = function($el, options) { + this.client = options.client; this.initialize($el, options); }; FileList.prototype = _.extend({}, OCA.Files.FileList.prototype, @@ -51,12 +52,6 @@ * @private */ initialize: function() { - this.client = new OC.Files.Client({ - host: OC.getHost(), - port: OC.getPort(), - root: OC.linkToRemoteBase('dav') + '/trashbin/' + OC.getCurrentUser().uid, - useHTTPS: OC.getProtocol() === 'https' - }); this.client.addFileInfoParser(function(response, data) { var props = response.propStat[0].properties; return { @@ -143,15 +138,10 @@ this.$el.find('#filestable th').toggleClass('hidden', !exists); }, - _removeCallback: function(result) { - if (result.status !== 'success') { - OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); - } - - var files = result.data.success; + _removeCallback: function(files) { var $el; for (var i = 0; i < files.length; i++) { - $el = this.remove(OC.basename(files[i].filename), {updateSummary: false}); + $el = this.remove(OC.basename(files[i]), {updateSummary: false}); this.fileSummary.remove({type: $el.attr('data-type'), size: $el.attr('data-size')}); } this.fileSummary.update(); @@ -208,50 +198,42 @@ event.preventDefault(); var self = this; var allFiles = this.$el.find('.select-all').is(':checked'); - var files = []; - var params = {}; - if (allFiles) { - params = { - allfiles: true, - dir: this.getCurrentDirectory() - }; - } - else { - files = _.pluck(this.getSelectedFiles(), 'name'); - params = { - files: JSON.stringify(files), - dir: this.getCurrentDirectory() - }; + var files = _.pluck(this.getSelectedFiles(), 'name'); + for (var i = 0; i < files.length; i++) { + var tr = this.findFileEl(files[i]); + this.showFileBusyState(tr, true); } - this.fileMultiSelectMenu.toggleLoading('delete', true); if (allFiles) { - this.showMask(); - } - else { - for (var i = 0; i < files.length; i++) { - var deleteAction = this.findFileEl(files[i]).children("td.date").children(".action.delete"); - deleteAction.removeClass('icon-delete').addClass('icon-loading-small'); - } - } - - $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), - params, - function(result) { - if (allFiles) { - if (result.status !== 'success') { - OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); - } + return this.client.remove('trash/' + this.getCurrentDirectory()) + .then( + function() { self.hideMask(); - // simply remove all files self.setFiles([]); + }, + function() { + OC.Notification.show(t('files_trashbin', 'Error while emptying trashbin')); } - else { - self._removeCallback(result); - } + ); + } else { + this.fileMultiSelectMenu.toggleLoading('delete', true); + var deletePromises = files.map(function(file) { + return self.client.remove('trash/' + self.getCurrentDirectory() + '/' + file) + .then( + function() { + self._removeCallback([file]); + } + ); + }); + return Promise.all(deletePromises).then( + function() { self.fileMultiSelectMenu.toggleLoading('delete', false); + }, + function() { + OC.Notification.show(t('files_trashbin', 'Error while removing files from trashbin')); } - ); + ); + } }, _onClickFile: function(event) {