Skip to content
Merged
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions apps/files_trashbin/lib/Events/MoveToTrashEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <[email protected]>
*
* @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;
}
}
46 changes: 44 additions & 2 deletions apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
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 {

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -200,6 +217,18 @@ public function rmdir($path) {
* @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) {
Expand All @@ -213,6 +242,17 @@ protected function shouldMoveToTrash($path){
return false;
}

/**
* 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
*
Expand Down Expand Up @@ -269,7 +309,9 @@ public static function setupStorage() {
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);
}
Expand Down
52 changes: 41 additions & 11 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@

use OC\Files\Storage\Temporary;
use OC\Files\Filesystem;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCA\Files_Trashbin\Storage;
use OCP\Files\Cache\ICache;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\ILogger;
use Symfony\Component\EventDispatcher\EventDispatcher;

/**
* Class Storage
Expand Down Expand Up @@ -522,19 +528,40 @@ public function testSingleStorageDeleteFolderFail() {
/**
* @dataProvider dataTestShouldMoveToTrash
*/
public function testShouldMoveToTrash($mountPoint, $path, $userExists, $expected) {
public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisablesTrash, $expected) {
$fileID = 1;
$cache = $this->getMock(ICache::class);
$cache->expects($this->any())->method('getId')->willReturn($fileID);
$tmpStorage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
->disableOriginalConstructor()->getMock();
->disableOriginalConstructor()->getMock($cache);
$tmpStorage->expects($this->any())->method('getCache')->willReturn($cache);
$userManager = $this->getMockBuilder('OCP\IUserManager')
->disableOriginalConstructor()->getMock();
$userManager->expects($this->any())
->method('userExists')->willReturn($userExists);
$logger = $this->getMockBuilder(ILogger::class)->getMock();
$storage = new \OCA\Files_Trashbin\Storage(
['mountPoint' => $mountPoint, 'storage' => $tmpStorage],
$userManager,
$logger
);
$eventDispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()->getMock();
$rootFolder = $this->getMock(IRootFolder::class);
$node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock();
$event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock();
$event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash);

$rootFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]);

$storage = $this->getMockBuilder(Storage::class)
->setConstructorArgs(
[
['mountPoint' => $mountPoint, 'storage' => $tmpStorage],
$userManager,
$logger,
$eventDispatcher,
$rootFolder
]
)->setMethods(['createMoveToTrashEvent'])->getMock();

$storage->expects($this->any())->method('createMoveToTrashEvent')->with($node)
->willReturn($event);

$this->assertSame($expected,
$this->invokePrivate($storage, 'shouldMoveToTrash', [$path])
Expand All @@ -544,10 +571,13 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $expected

public function dataTestShouldMoveToTrash() {
return [
['/schiesbn/', '/files/test.txt', true, true],
['/schiesbn/', '/files/test.txt', false, false],
['/schiesbn/', '/test.txt', true, false],
['/schiesbn/', '/test.txt', false, false],
['/schiesbn/', '/files/test.txt', true, false, true],
['/schiesbn/', '/files/test.txt', false, false, false],
['/schiesbn/', '/test.txt', true, false, false],
['/schiesbn/', '/test.txt', false, false, false],
// other apps disables the trashbin
['/schiesbn/', '/files/test.txt', true, true, false],
['/schiesbn/', '/files/test.txt', false, true, false],
];
}

Expand Down