@@ -41,173 +41,173 @@ |
||
| 41 | 41 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 42 | 42 | |
| 43 | 43 | class Storage extends Wrapper { |
| 44 | - /** @var IMountPoint */ |
|
| 45 | - private $mountPoint; |
|
| 46 | - |
|
| 47 | - /** @var IUserManager */ |
|
| 48 | - private $userManager; |
|
| 49 | - |
|
| 50 | - /** @var ILogger */ |
|
| 51 | - private $logger; |
|
| 52 | - |
|
| 53 | - /** @var EventDispatcherInterface */ |
|
| 54 | - private $eventDispatcher; |
|
| 55 | - |
|
| 56 | - /** @var IRootFolder */ |
|
| 57 | - private $rootFolder; |
|
| 58 | - |
|
| 59 | - /** @var ITrashManager */ |
|
| 60 | - private $trashManager; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Storage constructor. |
|
| 64 | - * |
|
| 65 | - * @param array $parameters |
|
| 66 | - * @param ITrashManager $trashManager |
|
| 67 | - * @param IUserManager|null $userManager |
|
| 68 | - * @param ILogger|null $logger |
|
| 69 | - * @param EventDispatcherInterface|null $eventDispatcher |
|
| 70 | - * @param IRootFolder|null $rootFolder |
|
| 71 | - */ |
|
| 72 | - public function __construct( |
|
| 73 | - $parameters, |
|
| 74 | - ITrashManager $trashManager = null, |
|
| 75 | - IUserManager $userManager = null, |
|
| 76 | - ILogger $logger = null, |
|
| 77 | - EventDispatcherInterface $eventDispatcher = null, |
|
| 78 | - IRootFolder $rootFolder = null |
|
| 79 | - ) { |
|
| 80 | - $this->mountPoint = $parameters['mountPoint']; |
|
| 81 | - $this->trashManager = $trashManager; |
|
| 82 | - $this->userManager = $userManager; |
|
| 83 | - $this->logger = $logger; |
|
| 84 | - $this->eventDispatcher = $eventDispatcher; |
|
| 85 | - $this->rootFolder = $rootFolder; |
|
| 86 | - parent::__construct($parameters); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Deletes the given file by moving it into the trashbin. |
|
| 91 | - * |
|
| 92 | - * @param string $path path of file or folder to delete |
|
| 93 | - * |
|
| 94 | - * @return bool true if the operation succeeded, false otherwise |
|
| 95 | - */ |
|
| 96 | - public function unlink($path) { |
|
| 97 | - try { |
|
| 98 | - return $this->doDelete($path, 'unlink'); |
|
| 99 | - } catch (GenericEncryptionException $e) { |
|
| 100 | - // in case of a encryption exception we delete the file right away |
|
| 101 | - $this->logger->info( |
|
| 102 | - "Can't move file" . $path . |
|
| 103 | - "to the trash bin, therefore it was deleted right away"); |
|
| 104 | - |
|
| 105 | - return $this->storage->unlink($path); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Deletes the given folder by moving it into the trashbin. |
|
| 111 | - * |
|
| 112 | - * @param string $path path of folder to delete |
|
| 113 | - * |
|
| 114 | - * @return bool true if the operation succeeded, false otherwise |
|
| 115 | - */ |
|
| 116 | - public function rmdir($path) { |
|
| 117 | - return $this->doDelete($path, 'rmdir'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * check if it is a file located in data/user/files only files in the |
|
| 122 | - * 'files' directory should be moved to the trash |
|
| 123 | - * |
|
| 124 | - * @param $path |
|
| 125 | - * @return bool |
|
| 126 | - */ |
|
| 127 | - protected function shouldMoveToTrash($path) { |
|
| 128 | - |
|
| 129 | - // check if there is a app which want to disable the trash bin for this file |
|
| 130 | - $fileId = $this->storage->getCache()->getId($path); |
|
| 131 | - $nodes = $this->rootFolder->getById($fileId); |
|
| 132 | - foreach ($nodes as $node) { |
|
| 133 | - $event = $this->createMoveToTrashEvent($node); |
|
| 134 | - $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); |
|
| 135 | - if ($event->shouldMoveToTrashBin() === false) { |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 141 | - $parts = explode('/', $normalized); |
|
| 142 | - if (count($parts) < 4) { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * get move to trash event |
|
| 155 | - * |
|
| 156 | - * @param Node $node |
|
| 157 | - * @return MoveToTrashEvent |
|
| 158 | - */ |
|
| 159 | - protected function createMoveToTrashEvent(Node $node) { |
|
| 160 | - return new MoveToTrashEvent($node); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Run the delete operation with the given method |
|
| 165 | - * |
|
| 166 | - * @param string $path path of file or folder to delete |
|
| 167 | - * @param string $method either "unlink" or "rmdir" |
|
| 168 | - * |
|
| 169 | - * @return bool true if the operation succeeded, false otherwise |
|
| 170 | - */ |
|
| 171 | - private function doDelete($path, $method) { |
|
| 172 | - if ( |
|
| 173 | - !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin') |
|
| 174 | - || (pathinfo($path, PATHINFO_EXTENSION) === 'part') |
|
| 175 | - || $this->shouldMoveToTrash($path) === false |
|
| 176 | - ) { |
|
| 177 | - return call_user_func([$this->storage, $method], $path); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // check permissions before we continue, this is especially important for |
|
| 181 | - // shared files |
|
| 182 | - if (!$this->isDeletable($path)) { |
|
| 183 | - return false; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $isMovedToTrash = $this->trashManager->moveToTrash($this, $path); |
|
| 187 | - if (!$isMovedToTrash) { |
|
| 188 | - return call_user_func([$this->storage, $method], $path); |
|
| 189 | - } else { |
|
| 190 | - return true; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Setup the storate wrapper callback |
|
| 196 | - */ |
|
| 197 | - public static function setupStorage() { |
|
| 198 | - \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { |
|
| 199 | - return new \OCA\Files_Trashbin\Storage( |
|
| 200 | - ['storage' => $storage, 'mountPoint' => $mountPoint], |
|
| 201 | - \OC::$server->query(ITrashManager::class), |
|
| 202 | - \OC::$server->getUserManager(), |
|
| 203 | - \OC::$server->getLogger(), |
|
| 204 | - \OC::$server->getEventDispatcher(), |
|
| 205 | - \OC::$server->getLazyRootFolder() |
|
| 206 | - ); |
|
| 207 | - }, 1); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - public function getMountPoint() { |
|
| 211 | - return $this->mountPoint; |
|
| 212 | - } |
|
| 44 | + /** @var IMountPoint */ |
|
| 45 | + private $mountPoint; |
|
| 46 | + |
|
| 47 | + /** @var IUserManager */ |
|
| 48 | + private $userManager; |
|
| 49 | + |
|
| 50 | + /** @var ILogger */ |
|
| 51 | + private $logger; |
|
| 52 | + |
|
| 53 | + /** @var EventDispatcherInterface */ |
|
| 54 | + private $eventDispatcher; |
|
| 55 | + |
|
| 56 | + /** @var IRootFolder */ |
|
| 57 | + private $rootFolder; |
|
| 58 | + |
|
| 59 | + /** @var ITrashManager */ |
|
| 60 | + private $trashManager; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Storage constructor. |
|
| 64 | + * |
|
| 65 | + * @param array $parameters |
|
| 66 | + * @param ITrashManager $trashManager |
|
| 67 | + * @param IUserManager|null $userManager |
|
| 68 | + * @param ILogger|null $logger |
|
| 69 | + * @param EventDispatcherInterface|null $eventDispatcher |
|
| 70 | + * @param IRootFolder|null $rootFolder |
|
| 71 | + */ |
|
| 72 | + public function __construct( |
|
| 73 | + $parameters, |
|
| 74 | + ITrashManager $trashManager = null, |
|
| 75 | + IUserManager $userManager = null, |
|
| 76 | + ILogger $logger = null, |
|
| 77 | + EventDispatcherInterface $eventDispatcher = null, |
|
| 78 | + IRootFolder $rootFolder = null |
|
| 79 | + ) { |
|
| 80 | + $this->mountPoint = $parameters['mountPoint']; |
|
| 81 | + $this->trashManager = $trashManager; |
|
| 82 | + $this->userManager = $userManager; |
|
| 83 | + $this->logger = $logger; |
|
| 84 | + $this->eventDispatcher = $eventDispatcher; |
|
| 85 | + $this->rootFolder = $rootFolder; |
|
| 86 | + parent::__construct($parameters); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Deletes the given file by moving it into the trashbin. |
|
| 91 | + * |
|
| 92 | + * @param string $path path of file or folder to delete |
|
| 93 | + * |
|
| 94 | + * @return bool true if the operation succeeded, false otherwise |
|
| 95 | + */ |
|
| 96 | + public function unlink($path) { |
|
| 97 | + try { |
|
| 98 | + return $this->doDelete($path, 'unlink'); |
|
| 99 | + } catch (GenericEncryptionException $e) { |
|
| 100 | + // in case of a encryption exception we delete the file right away |
|
| 101 | + $this->logger->info( |
|
| 102 | + "Can't move file" . $path . |
|
| 103 | + "to the trash bin, therefore it was deleted right away"); |
|
| 104 | + |
|
| 105 | + return $this->storage->unlink($path); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Deletes the given folder by moving it into the trashbin. |
|
| 111 | + * |
|
| 112 | + * @param string $path path of folder to delete |
|
| 113 | + * |
|
| 114 | + * @return bool true if the operation succeeded, false otherwise |
|
| 115 | + */ |
|
| 116 | + public function rmdir($path) { |
|
| 117 | + return $this->doDelete($path, 'rmdir'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * check if it is a file located in data/user/files only files in the |
|
| 122 | + * 'files' directory should be moved to the trash |
|
| 123 | + * |
|
| 124 | + * @param $path |
|
| 125 | + * @return bool |
|
| 126 | + */ |
|
| 127 | + protected function shouldMoveToTrash($path) { |
|
| 128 | + |
|
| 129 | + // check if there is a app which want to disable the trash bin for this file |
|
| 130 | + $fileId = $this->storage->getCache()->getId($path); |
|
| 131 | + $nodes = $this->rootFolder->getById($fileId); |
|
| 132 | + foreach ($nodes as $node) { |
|
| 133 | + $event = $this->createMoveToTrashEvent($node); |
|
| 134 | + $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); |
|
| 135 | + if ($event->shouldMoveToTrashBin() === false) { |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 141 | + $parts = explode('/', $normalized); |
|
| 142 | + if (count($parts) < 4) { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * get move to trash event |
|
| 155 | + * |
|
| 156 | + * @param Node $node |
|
| 157 | + * @return MoveToTrashEvent |
|
| 158 | + */ |
|
| 159 | + protected function createMoveToTrashEvent(Node $node) { |
|
| 160 | + return new MoveToTrashEvent($node); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Run the delete operation with the given method |
|
| 165 | + * |
|
| 166 | + * @param string $path path of file or folder to delete |
|
| 167 | + * @param string $method either "unlink" or "rmdir" |
|
| 168 | + * |
|
| 169 | + * @return bool true if the operation succeeded, false otherwise |
|
| 170 | + */ |
|
| 171 | + private function doDelete($path, $method) { |
|
| 172 | + if ( |
|
| 173 | + !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin') |
|
| 174 | + || (pathinfo($path, PATHINFO_EXTENSION) === 'part') |
|
| 175 | + || $this->shouldMoveToTrash($path) === false |
|
| 176 | + ) { |
|
| 177 | + return call_user_func([$this->storage, $method], $path); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // check permissions before we continue, this is especially important for |
|
| 181 | + // shared files |
|
| 182 | + if (!$this->isDeletable($path)) { |
|
| 183 | + return false; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $isMovedToTrash = $this->trashManager->moveToTrash($this, $path); |
|
| 187 | + if (!$isMovedToTrash) { |
|
| 188 | + return call_user_func([$this->storage, $method], $path); |
|
| 189 | + } else { |
|
| 190 | + return true; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Setup the storate wrapper callback |
|
| 196 | + */ |
|
| 197 | + public static function setupStorage() { |
|
| 198 | + \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { |
|
| 199 | + return new \OCA\Files_Trashbin\Storage( |
|
| 200 | + ['storage' => $storage, 'mountPoint' => $mountPoint], |
|
| 201 | + \OC::$server->query(ITrashManager::class), |
|
| 202 | + \OC::$server->getUserManager(), |
|
| 203 | + \OC::$server->getLogger(), |
|
| 204 | + \OC::$server->getEventDispatcher(), |
|
| 205 | + \OC::$server->getLazyRootFolder() |
|
| 206 | + ); |
|
| 207 | + }, 1); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + public function getMountPoint() { |
|
| 211 | + return $this->mountPoint; |
|
| 212 | + } |
|
| 213 | 213 | } |