summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib
diff options
context:
space:
mode:
authorBjoern Schiessle <bjoern@schiessle.org>2017-09-14 13:59:09 +0200
committerBjoern Schiessle <bjoern@schiessle.org>2017-09-15 11:32:45 +0200
commit6e69881512ad072508f13983ad9c145b48855ce7 (patch)
tree7f79d8f68dbc209d0aa458b4bad47fe34777df30 /apps/files_trashbin/lib
parent113fd47f3005d9f05a6dc3d42b73bbb7a6339d00 (diff)
downloadnextcloud-server-6e69881512ad072508f13983ad9c145b48855ce7.tar.gz
nextcloud-server-6e69881512ad072508f13983ad9c145b48855ce7.zip
allow apps to control the trash bin
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
Diffstat (limited to 'apps/files_trashbin/lib')
-rw-r--r--apps/files_trashbin/lib/Events/MoveToTrashEvent.php73
-rw-r--r--apps/files_trashbin/lib/Storage.php46
2 files changed, 117 insertions, 2 deletions
diff --git a/apps/files_trashbin/lib/Events/MoveToTrashEvent.php b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php
new file mode 100644
index 00000000000..185f39f63cb
--- /dev/null
+++ b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Files_Trashbin\Events;
+
+
+use OCP\Files\Node;
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Class MoveToTrashEvent
+ *
+ * Event to allow other apps to disable the trash bin for specific files
+ *
+ * @package OCA\Files_Trashbin\Events
+ */
+class MoveToTrashEvent extends Event {
+
+ /** @var bool */
+ private $moveToTrashBin;
+
+ /** @var Node */
+ private $node;
+
+ public function __construct(Node $node) {
+ $this->moveToTrashBin = true;
+ $this->node = $node;
+ }
+
+ /**
+ * get Node which will be deleted
+ *
+ * @return Node
+ */
+ public function getNode() {
+ return $this->node;
+ }
+
+ /**
+ * disable trash bin for this operation
+ */
+ public function disableTrashBin() {
+ $this->moveToTrashBin = false;
+ }
+
+ /**
+ * should the file be moved to the trash bin?
+ *
+ * @return bool
+ */
+ public function shouldMoveToTrashBin() {
+ return $this->moveToTrashBin;
+ }
+}
diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php
index fdc7081b137..cb36b4604c1 100644
--- a/apps/files_trashbin/lib/Storage.php
+++ b/apps/files_trashbin/lib/Storage.php
@@ -28,9 +28,13 @@ namespace OCA\Files_Trashbin;
use OC\Files\Filesystem;
use OC\Files\Storage\Wrapper\Wrapper;
use OC\Files\View;
+use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCP\Encryption\Exceptions\GenericEncryptionException;
+use OCP\Files\IRootFolder;
+use OCP\Files\Node;
use OCP\ILogger;
use OCP\IUserManager;
+use Symfony\Component\EventDispatcher\EventDispatcher;
class Storage extends Wrapper {
@@ -60,18 +64,31 @@ class Storage extends Wrapper {
/** @var ILogger */
private $logger;
+ /** @var EventDispatcher */
+ private $eventDispatcher;
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
/**
* Storage constructor.
*
* @param array $parameters
* @param IUserManager|null $userManager
+ * @param ILogger|null $logger
+ * @param EventDispatcher|null $eventDispatcher
+ * @param IRootFolder|null $rootFolder
*/
public function __construct($parameters,
IUserManager $userManager = null,
- ILogger $logger = null) {
+ ILogger $logger = null,
+ EventDispatcher $eventDispatcher = null,
+ IRootFolder $rootFolder = null) {
$this->mountPoint = $parameters['mountPoint'];
$this->userManager = $userManager;
$this->logger = $logger;
+ $this->eventDispatcher = $eventDispatcher;
+ $this->rootFolder = $rootFolder;
parent::__construct($parameters);
}
@@ -200,6 +217,18 @@ class Storage extends Wrapper {
* @return bool
*/
protected function shouldMoveToTrash($path){
+
+ // check if there is a app which want to disable the trash bin for this file
+ $fileId = $this->storage->getCache()->getId($path);
+ $nodes = $this->rootFolder->getById($fileId);
+ foreach ($nodes as $node) {
+ $event = $this->createMoveToTrashEvent($node);
+ $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
+ if ($event->shouldMoveToTrashBin() === false) {
+ return false;
+ }
+ }
+
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
$parts = explode('/', $normalized);
if (count($parts) < 4) {
@@ -214,6 +243,17 @@ class Storage extends Wrapper {
}
/**
+ * get move to trash event
+ *
+ * @param Node $node
+ * @return MoveToTrashEvent
+ */
+ protected function createMoveToTrashEvent(Node $node) {
+ $event = new MoveToTrashEvent($node);
+ return $event;
+ }
+
+ /**
* Run the delete operation with the given method
*
* @param string $path path of file or folder to delete
@@ -269,7 +309,9 @@ class Storage extends Wrapper {
return new \OCA\Files_Trashbin\Storage(
array('storage' => $storage, 'mountPoint' => $mountPoint),
\OC::$server->getUserManager(),
- \OC::$server->getLogger()
+ \OC::$server->getLogger(),
+ \OC::$server->getEventDispatcher(),
+ \OC::$server->getLazyRootFolder()
);
}, 1);
}