| Total Complexity | 92 |
| Total Lines | 465 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SharedStorage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SharedStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage { |
||
| 52 | |||
| 53 | /** @var \OCP\Share\IShare */ |
||
| 54 | private $superShare; |
||
| 55 | |||
| 56 | /** @var \OCP\Share\IShare[] */ |
||
| 57 | private $groupedShares; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \OC\Files\View |
||
| 61 | */ |
||
| 62 | private $ownerView; |
||
| 63 | |||
| 64 | private $initialized = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ICacheEntry |
||
| 68 | */ |
||
| 69 | private $sourceRootInfo; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | private $user; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \OCP\ILogger |
||
| 76 | */ |
||
| 77 | private $logger; |
||
| 78 | |||
| 79 | /** @var IStorage */ |
||
| 80 | private $nonMaskedStorage; |
||
| 81 | |||
| 82 | private $options; |
||
|
|
|||
| 83 | |||
| 84 | /** @var boolean */ |
||
| 85 | private $sharingDisabledForUser; |
||
| 86 | |||
| 87 | public function __construct($arguments) { |
||
| 88 | $this->ownerView = $arguments['ownerView']; |
||
| 89 | $this->logger = \OC::$server->getLogger(); |
||
| 90 | |||
| 91 | $this->superShare = $arguments['superShare']; |
||
| 92 | $this->groupedShares = $arguments['groupedShares']; |
||
| 93 | |||
| 94 | $this->user = $arguments['user']; |
||
| 95 | if (isset($arguments['sharingDisabledForUser'])) { |
||
| 96 | $this->sharingDisabledForUser = $arguments['sharingDisabledForUser']; |
||
| 97 | } else { |
||
| 98 | $this->sharingDisabledForUser = false; |
||
| 99 | } |
||
| 100 | |||
| 101 | parent::__construct([ |
||
| 102 | 'storage' => null, |
||
| 103 | 'root' => null, |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return ICacheEntry |
||
| 109 | */ |
||
| 110 | private function getSourceRootInfo() { |
||
| 111 | if (is_null($this->sourceRootInfo)) { |
||
| 112 | if (is_null($this->superShare->getNodeCacheEntry())) { |
||
| 113 | $this->init(); |
||
| 114 | $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath); |
||
| 115 | } else { |
||
| 116 | $this->sourceRootInfo = $this->superShare->getNodeCacheEntry(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return $this->sourceRootInfo; |
||
| 120 | } |
||
| 121 | |||
| 122 | private function init() { |
||
| 123 | if ($this->initialized) { |
||
| 124 | return; |
||
| 125 | } |
||
| 126 | $this->initialized = true; |
||
| 127 | try { |
||
| 128 | Filesystem::initMountPoints($this->superShare->getShareOwner()); |
||
| 129 | $storageId = $this->superShare->getNodeCacheEntry() ? $this->superShare->getNodeCacheEntry()->getStorageId() : null; |
||
| 130 | $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId(), $storageId); |
||
| 131 | [$this->nonMaskedStorage, $this->rootPath] = $this->ownerView->resolvePath($sourcePath); |
||
| 132 | $this->storage = new PermissionsMask([ |
||
| 133 | 'storage' => $this->nonMaskedStorage, |
||
| 134 | 'mask' => $this->superShare->getPermissions(), |
||
| 135 | ]); |
||
| 136 | } catch (NotFoundException $e) { |
||
| 137 | // original file not accessible or deleted, set FailedStorage |
||
| 138 | $this->storage = new FailedStorage(['exception' => $e]); |
||
| 139 | $this->cache = new FailedCache(); |
||
| 140 | $this->rootPath = ''; |
||
| 141 | } catch (NoUserException $e) { |
||
| 142 | // sharer user deleted, set FailedStorage |
||
| 143 | $this->storage = new FailedStorage(['exception' => $e]); |
||
| 144 | $this->cache = new FailedCache(); |
||
| 145 | $this->rootPath = ''; |
||
| 146 | } catch (\Exception $e) { |
||
| 147 | $this->storage = new FailedStorage(['exception' => $e]); |
||
| 148 | $this->cache = new FailedCache(); |
||
| 149 | $this->rootPath = ''; |
||
| 150 | $this->logger->logException($e); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!$this->nonMaskedStorage) { |
||
| 154 | $this->nonMaskedStorage = $this->storage; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | public function instanceOfStorage($class) { |
||
| 162 | if ($class === '\OC\Files\Storage\Common') { |
||
| 163 | return true; |
||
| 164 | } |
||
| 165 | if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage', '\OCP\Files\IHomeStorage'])) { |
||
| 166 | return false; |
||
| 167 | } |
||
| 168 | return parent::instanceOfStorage($class); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getShareId() { |
||
| 175 | return $this->superShare->getId(); |
||
| 176 | } |
||
| 177 | |||
| 178 | private function isValid() { |
||
| 179 | return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * get id of the mount point |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getId() { |
||
| 188 | return 'shared::' . $this->getMountPoint(); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the permissions granted for a shared file |
||
| 193 | * |
||
| 194 | * @param string $target Shared target file path |
||
| 195 | * @return int CRUDS permissions granted |
||
| 196 | */ |
||
| 197 | public function getPermissions($target = '') { |
||
| 198 | if (!$this->isValid()) { |
||
| 199 | return 0; |
||
| 200 | } |
||
| 201 | $permissions = parent::getPermissions($target) & $this->superShare->getPermissions(); |
||
| 202 | |||
| 203 | // part files and the mount point always have delete permissions |
||
| 204 | if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') { |
||
| 205 | $permissions |= \OCP\Constants::PERMISSION_DELETE; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->sharingDisabledForUser) { |
||
| 209 | $permissions &= ~\OCP\Constants::PERMISSION_SHARE; |
||
| 210 | } |
||
| 211 | |||
| 212 | return $permissions; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function isCreatable($path) { |
||
| 216 | return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function isReadable($path) { |
||
| 220 | if (!$this->isValid()) { |
||
| 221 | return false; |
||
| 222 | } |
||
| 223 | if (!$this->file_exists($path)) { |
||
| 224 | return false; |
||
| 225 | } |
||
| 226 | /** @var IStorage $storage */ |
||
| 227 | /** @var string $internalPath */ |
||
| 228 | [$storage, $internalPath] = $this->resolvePath($path); |
||
| 229 | return $storage->isReadable($internalPath); |
||
| 230 | } |
||
| 231 | |||
| 232 | public function isUpdatable($path) { |
||
| 233 | return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function isDeletable($path) { |
||
| 237 | return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE); |
||
| 238 | } |
||
| 239 | |||
| 240 | public function isSharable($path) { |
||
| 241 | if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function fopen($path, $mode) { |
||
| 248 | $source = $this->getUnjailedPath($path); |
||
| 249 | switch ($mode) { |
||
| 250 | case 'r+': |
||
| 251 | case 'rb+': |
||
| 252 | case 'w+': |
||
| 253 | case 'wb+': |
||
| 254 | case 'x+': |
||
| 255 | case 'xb+': |
||
| 256 | case 'a+': |
||
| 257 | case 'ab+': |
||
| 258 | case 'w': |
||
| 259 | case 'wb': |
||
| 260 | case 'x': |
||
| 261 | case 'xb': |
||
| 262 | case 'a': |
||
| 263 | case 'ab': |
||
| 264 | $creatable = $this->isCreatable(dirname($path)); |
||
| 265 | $updatable = $this->isUpdatable($path); |
||
| 266 | // if neither permissions given, no need to continue |
||
| 267 | if (!$creatable && !$updatable) { |
||
| 268 | if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
||
| 269 | $updatable = $this->isUpdatable(dirname($path)); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!$updatable) { |
||
| 273 | return false; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | $exists = $this->file_exists($path); |
||
| 278 | // if a file exists, updatable permissions are required |
||
| 279 | if ($exists && !$updatable) { |
||
| 280 | return false; |
||
| 281 | } |
||
| 282 | |||
| 283 | // part file is allowed if !$creatable but the final file is $updatable |
||
| 284 | if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') { |
||
| 285 | if (!$exists && !$creatable) { |
||
| 286 | return false; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | $info = [ |
||
| 291 | 'target' => $this->getMountPoint() . $path, |
||
| 292 | 'source' => $source, |
||
| 293 | 'mode' => $mode, |
||
| 294 | ]; |
||
| 295 | \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info); |
||
| 296 | return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * see http://php.net/manual/en/function.rename.php |
||
| 301 | * |
||
| 302 | * @param string $path1 |
||
| 303 | * @param string $path2 |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function rename($path1, $path2) { |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * return mount point of share, relative to data/user/files |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getMountPoint() { |
||
| 331 | return $this->superShare->getTarget(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $path |
||
| 336 | */ |
||
| 337 | public function setMountPoint($path) { |
||
| 338 | $this->superShare->setTarget($path); |
||
| 339 | |||
| 340 | foreach ($this->groupedShares as $share) { |
||
| 341 | $share->setTarget($path); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * get the user who shared the file |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function getSharedFrom() { |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return \OCP\Share\IShare |
||
| 356 | */ |
||
| 357 | public function getShare() { |
||
| 358 | return $this->superShare; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * return share type, can be "file" or "folder" |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getItemType() { |
||
| 367 | return $this->superShare->getNodeType(); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $path |
||
| 372 | * @param null $storage |
||
| 373 | * @return Cache |
||
| 374 | */ |
||
| 375 | public function getCache($path = '', $storage = null) { |
||
| 376 | if ($this->cache) { |
||
| 377 | return $this->cache; |
||
| 378 | } |
||
| 379 | if (!$storage) { |
||
| 380 | $storage = $this; |
||
| 381 | } |
||
| 382 | $sourceRoot = $this->getSourceRootInfo(); |
||
| 383 | if ($this->storage instanceof FailedStorage) { |
||
| 384 | return new FailedCache(); |
||
| 385 | } |
||
| 386 | |||
| 387 | $this->cache = new \OCA\Files_Sharing\Cache($storage, $sourceRoot, $this->superShare); |
||
| 388 | return $this->cache; |
||
| 389 | } |
||
| 390 | |||
| 391 | public function getScanner($path = '', $storage = null) { |
||
| 396 | } |
||
| 397 | |||
| 398 | public function getOwner($path) { |
||
| 399 | return $this->superShare->getShareOwner(); |
||
| 400 | } |
||
| 401 | |||
| 402 | public function getWatcher($path = '', $storage = null) { |
||
| 403 | // cache updating is handled by the share source |
||
| 404 | return new NullWatcher(); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * unshare complete storage, also the grouped shares |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function unshareStorage() { |
||
| 413 | foreach ($this->groupedShares as $share) { |
||
| 414 | \OC::$server->getShareManager()->deleteFromSelf($share, $this->user); |
||
| 415 | } |
||
| 416 | return true; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $path |
||
| 421 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 422 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 423 | * @throws \OCP\Lock\LockedException |
||
| 424 | */ |
||
| 425 | public function acquireLock($path, $type, ILockingProvider $provider) { |
||
| 426 | /** @var \OCP\Files\Storage $targetStorage */ |
||
| 427 | [$targetStorage, $targetInternalPath] = $this->resolvePath($path); |
||
| 428 | $targetStorage->acquireLock($targetInternalPath, $type, $provider); |
||
| 429 | // lock the parent folders of the owner when locking the share as recipient |
||
| 430 | if ($path === '') { |
||
| 431 | $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
||
| 432 | $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $path |
||
| 438 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 439 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 440 | */ |
||
| 441 | public function releaseLock($path, $type, ILockingProvider $provider) { |
||
| 442 | /** @var \OCP\Files\Storage $targetStorage */ |
||
| 443 | [$targetStorage, $targetInternalPath] = $this->resolvePath($path); |
||
| 444 | $targetStorage->releaseLock($targetInternalPath, $type, $provider); |
||
| 445 | // unlock the parent folders of the owner when unlocking the share as recipient |
||
| 446 | if ($path === '') { |
||
| 447 | $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
||
| 448 | $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $path |
||
| 454 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 455 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 456 | */ |
||
| 457 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 458 | /** @var \OCP\Files\Storage $targetStorage */ |
||
| 459 | [$targetStorage, $targetInternalPath] = $this->resolvePath($path); |
||
| 460 | $targetStorage->changeLock($targetInternalPath, $type, $provider); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return array [ available, last_checked ] |
||
| 465 | */ |
||
| 466 | public function getAvailability() { |
||
| 467 | // shares do not participate in availability logic |
||
| 468 | return [ |
||
| 469 | 'available' => true, |
||
| 470 | 'last_checked' => 0, |
||
| 471 | ]; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param bool $available |
||
| 476 | */ |
||
| 477 | public function setAvailability($available) { |
||
| 478 | // shares do not participate in availability logic |
||
| 479 | } |
||
| 480 | |||
| 481 | public function getSourceStorage() { |
||
| 482 | $this->init(); |
||
| 483 | return $this->nonMaskedStorage; |
||
| 484 | } |
||
| 485 | |||
| 486 | public function getWrapperStorage() { |
||
| 487 | $this->init(); |
||
| 488 | return $this->storage; |
||
| 489 | } |
||
| 490 | |||
| 491 | public function file_get_contents($path) { |
||
| 492 | $info = [ |
||
| 493 | 'target' => $this->getMountPoint() . '/' . $path, |
||
| 494 | 'source' => $this->getUnjailedPath($path), |
||
| 495 | ]; |
||
| 496 | \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); |
||
| 497 | return parent::file_get_contents($path); |
||
| 498 | } |
||
| 499 | |||
| 500 | public function file_put_contents($path, $data) { |
||
| 501 | $info = [ |
||
| 502 | 'target' => $this->getMountPoint() . '/' . $path, |
||
| 503 | 'source' => $this->getUnjailedPath($path), |
||
| 504 | ]; |
||
| 505 | \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); |
||
| 506 | return parent::file_put_contents($path, $data); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function setMountOptions(array $options) { |
||
| 510 | $this->mountOptions = $options; |
||
| 511 | } |
||
| 512 | |||
| 513 | public function getUnjailedPath($path) { |
||
| 516 | } |
||
| 517 | } |
||
| 518 |