123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace OCA\Files_Trashbin;
- use OC\Files\Filesystem;
- use OC\Files\Storage\Wrapper\Wrapper;
- use OCA\Files_Trashbin\Events\MoveToTrashEvent;
- use OCA\Files_Trashbin\Trash\ITrashManager;
- use OCP\Encryption\Exceptions\GenericEncryptionException;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\IRootFolder;
- use OCP\Files\Node;
- use OCP\Files\Storage\IStorage;
- use OCP\IUserManager;
- use Psr\Log\LoggerInterface;
- class Storage extends Wrapper {
- private string $mountPoint;
- private IUserManager$userManager;
- private LoggerInterface $logger;
- private IEventDispatcher $eventDispatcher;
- private IRootFolder $rootFolder;
- private ITrashManager $trashManager;
- private bool $trashEnabled = true;
-
- public function __construct(
- $parameters,
- ?ITrashManager $trashManager = null,
- ?IUserManager $userManager = null,
- ?LoggerInterface $logger = null,
- ?IEventDispatcher $eventDispatcher = null,
- ?IRootFolder $rootFolder = null
- ) {
- $this->mountPoint = $parameters['mountPoint'];
- $this->trashManager = $trashManager;
- $this->userManager = $userManager;
- $this->logger = $logger;
- $this->eventDispatcher = $eventDispatcher;
- $this->rootFolder = $rootFolder;
- parent::__construct($parameters);
- }
-
- public function unlink($path) {
- if ($this->trashEnabled) {
- try {
- return $this->doDelete($path, 'unlink');
- } catch (GenericEncryptionException $e) {
-
- $this->logger->info(
- "Can't move file " . $path .
- " to the trash bin, therefore it was deleted right away");
- return $this->storage->unlink($path);
- }
- } else {
- return $this->storage->unlink($path);
- }
- }
-
- public function rmdir($path) {
- if ($this->trashEnabled) {
- return $this->doDelete($path, 'rmdir');
- } else {
- return $this->storage->rmdir($path);
- }
- }
-
- protected function shouldMoveToTrash($path) {
- $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
- $parts = explode('/', $normalized);
- if (count($parts) < 4 || strpos($normalized, '/appdata_') === 0) {
- return false;
- }
-
- $fileId = $this->storage->getCache()->getId($path);
- $owner = $this->storage->getOwner($path);
- if ($owner === false || $this->storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class)) {
- $nodes = $this->rootFolder->getById($fileId);
- } else {
- $nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
- }
- foreach ($nodes as $node) {
- $event = $this->createMoveToTrashEvent($node);
- $this->eventDispatcher->dispatchTyped($event);
- $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
- if ($event->shouldMoveToTrashBin() === false) {
- return false;
- }
- }
- if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
- return true;
- }
- return false;
- }
-
- protected function createMoveToTrashEvent(Node $node) {
- return new MoveToTrashEvent($node);
- }
-
- private function doDelete($path, $method) {
- if (
- !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
- || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
- || $this->shouldMoveToTrash($path) === false
- ) {
- return call_user_func([$this->storage, $method], $path);
- }
-
-
- 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;
- }
- }
-
- public static function setupStorage() {
- $trashManager = \OC::$server->get(ITrashManager::class);
- $userManager = \OC::$server->get(IUserManager::class);
- $logger = \OC::$server->get(LoggerInterface::class);
- $eventDispatcher = \OC::$server->get(IEventDispatcher::class);
- $rootFolder = \OC::$server->get(IRootFolder::class);
- Filesystem::addStorageWrapper(
- 'oc_trashbin',
- function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
- return new Storage(
- ['storage' => $storage, 'mountPoint' => $mountPoint],
- $trashManager,
- $userManager,
- $logger,
- $eventDispatcher,
- $rootFolder,
- );
- },
- 1);
- }
- public function getMountPoint() {
- return $this->mountPoint;
- }
- public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
- $sourceIsTrashbin = $sourceStorage->instanceOfStorage(Storage::class);
- try {
-
-
- if ($sourceIsTrashbin) {
-
- $sourceStorage->disableTrash();
- }
- $result = parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
- if ($sourceIsTrashbin) {
-
- $sourceStorage->enableTrash();
- }
- return $result;
- } catch (\Exception $e) {
- if ($sourceIsTrashbin) {
-
- $sourceStorage->enableTrash();
- }
- throw $e;
- }
- }
- protected function disableTrash() {
- $this->trashEnabled = false;
- }
- protected function enableTrash() {
- $this->trashEnabled = true;
- }
- }
|