nextcloud/apps/files_trashbin/lib/Storage.php

214 lines
5.8 KiB
PHP
Raw Normal View History

2015-01-14 23:06:26 +03:00
<?php
/**
2016-07-21 17:49:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
2016-05-26 20:56:05 +03:00
* @author Björn Schießle <bjoern@schiessle.org>
* @author Joas Schilling <coding@schilljs.com>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2015-03-26 13:44:34 +03:00
* @author Vincent Petry <pvince81@owncloud.com>
2015-01-14 23:06:26 +03:00
*
2015-03-26 13:44:34 +03:00
* @license AGPL-3.0
2015-01-14 23:06:26 +03:00
*
2015-03-26 13:44:34 +03:00
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2015-02-23 13:28:53 +03:00
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
2015-01-14 23:06:26 +03:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-03-26 13:44:34 +03:00
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2015-01-14 23:06:26 +03:00
*
*/
2015-01-14 23:06:26 +03:00
namespace OCA\Files_Trashbin;
use OC\Files\Filesystem;
2015-01-14 23:06:26 +03:00
use OC\Files\Storage\Wrapper\Wrapper;
use OC\Files\View;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\ILogger;
2015-07-07 19:38:58 +03:00
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2015-01-14 23:06:26 +03:00
class Storage extends Wrapper {
/** @var IMountPoint */
2015-01-14 23:06:26 +03:00
private $mountPoint;
2015-07-07 19:38:58 +03:00
/** @var IUserManager */
private $userManager;
/** @var ILogger */
private $logger;
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var IRootFolder */
private $rootFolder;
/** @var ITrashManager */
private $trashManager;
/**
* Storage constructor.
*
* @param array $parameters
* @param ITrashManager $trashManager
* @param IUserManager|null $userManager
* @param ILogger|null $logger
* @param EventDispatcherInterface|null $eventDispatcher
* @param IRootFolder|null $rootFolder
*/
public function __construct(
$parameters,
ITrashManager $trashManager = null,
IUserManager $userManager = null,
ILogger $logger = null,
EventDispatcherInterface $eventDispatcher = null,
IRootFolder $rootFolder = null
) {
2015-01-14 23:06:26 +03:00
$this->mountPoint = $parameters['mountPoint'];
$this->trashManager = $trashManager;
2015-07-07 19:38:58 +03:00
$this->userManager = $userManager;
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
$this->rootFolder = $rootFolder;
2015-01-14 23:06:26 +03:00
parent::__construct($parameters);
}
/**
* Deletes the given file by moving it into the trashbin.
*
* @param string $path path of file or folder to delete
*
* @return bool true if the operation succeeded, false otherwise
*/
2015-01-14 23:06:26 +03:00
public function unlink($path) {
try {
return $this->doDelete($path, 'unlink');
} catch (GenericEncryptionException $e) {
// in case of a encryption exception we delete the file right away
$this->logger->info(
"Can't move file" . $path .
"to the trash bin, therefore it was deleted right away");
return $this->storage->unlink($path);
}
}
/**
* Deletes the given folder by moving it into the trashbin.
*
* @param string $path path of folder to delete
*
* @return bool true if the operation succeeded, false otherwise
*/
public function rmdir($path) {
return $this->doDelete($path, 'rmdir');
}
2015-07-07 19:38:58 +03:00
/**
* check if it is a file located in data/user/files only files in the
* 'files' directory should be moved to the trash
*
* @param $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;
}
}
2015-07-07 19:38:58 +03:00
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
$parts = explode('/', $normalized);
if (count($parts) < 4) {
return false;
}
if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
2015-07-07 19:38:58 +03:00
return true;
}
return false;
}
/**
* get move to trash event
*
* @param Node $node
* @return MoveToTrashEvent
*/
protected function createMoveToTrashEvent(Node $node) {
return new MoveToTrashEvent($node);
}
/**
* Run the delete operation with the given method
*
* @param string $path path of file or folder to delete
* @param string $method either "unlink" or "rmdir"
*
* @return bool true if the operation succeeded, false otherwise
*/
private function doDelete($path, $method) {
if (
!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
2015-07-07 19:38:58 +03:00
|| $this->shouldMoveToTrash($path) === false
) {
return call_user_func([$this->storage, $method], $path);
}
// check permissions before we continue, this is especially important for
// shared files
if (!$this->isDeletable($path)) {
return false;
}
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
if (!$isMovedToTrash) {
return call_user_func([$this->storage, $method], $path);
} else {
return true;
2015-01-14 23:06:26 +03:00
}
}
/**
* Setup the storate wrapper callback
*/
public static function setupStorage() {
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
2015-07-07 19:38:58 +03:00
return new \OCA\Files_Trashbin\Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
\OC::$server->query(ITrashManager::class),
\OC::$server->getUserManager(),
\OC::$server->getLogger(),
\OC::$server->getEventDispatcher(),
\OC::$server->getLazyRootFolder()
2015-07-07 19:38:58 +03:00
);
}, 1);
2015-01-14 23:06:26 +03:00
}
public function getMountPoint() {
return $this->mountPoint;
}
2015-01-14 23:06:26 +03:00
}