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