| Conditions | 22 |
| Paths | 966 |
| Total Lines | 216 |
| Code Lines | 150 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 301 | public function showShare($path = ''): TemplateResponse { |
||
| 302 | \OC_User::setIncognitoMode(true); |
||
| 303 | |||
| 304 | // Check whether share exists |
||
| 305 | try { |
||
| 306 | $share = $this->shareManager->getShareByToken($this->getToken()); |
||
| 307 | } catch (ShareNotFound $e) { |
||
| 308 | $this->emitAccessShareHook($this->getToken(), 404, 'Share not found'); |
||
| 309 | throw new NotFoundException(); |
||
| 310 | } |
||
| 311 | |||
| 312 | if (!$this->validateShare($share)) { |
||
| 313 | throw new NotFoundException(); |
||
| 314 | } |
||
| 315 | |||
| 316 | $shareNode = $share->getNode(); |
||
| 317 | |||
| 318 | // We can't get the path of a file share |
||
| 319 | try { |
||
| 320 | if ($shareNode instanceof \OCP\Files\File && $path !== '') { |
||
| 321 | $this->emitAccessShareHook($share, 404, 'Share not found'); |
||
| 322 | throw new NotFoundException(); |
||
| 323 | } |
||
| 324 | } catch (\Exception $e) { |
||
| 325 | $this->emitAccessShareHook($share, 404, 'Share not found'); |
||
| 326 | throw $e; |
||
| 327 | } |
||
| 328 | |||
| 329 | $shareTmpl = []; |
||
| 330 | $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
||
| 331 | $shareTmpl['owner'] = $share->getShareOwner(); |
||
| 332 | $shareTmpl['filename'] = $shareNode->getName(); |
||
| 333 | $shareTmpl['directory_path'] = $share->getTarget(); |
||
| 334 | $shareTmpl['note'] = $share->getNote(); |
||
| 335 | $shareTmpl['mimetype'] = $shareNode->getMimetype(); |
||
| 336 | $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($shareNode->getMimetype()); |
||
| 337 | $shareTmpl['dirToken'] = $this->getToken(); |
||
| 338 | $shareTmpl['sharingToken'] = $this->getToken(); |
||
| 339 | $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
||
| 340 | $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
||
| 341 | $shareTmpl['dir'] = ''; |
||
| 342 | $shareTmpl['nonHumanFileSize'] = $shareNode->getSize(); |
||
| 343 | $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($shareNode->getSize()); |
||
| 344 | $shareTmpl['hideDownload'] = $share->getHideDownload(); |
||
| 345 | |||
| 346 | $hideFileList = false; |
||
| 347 | |||
| 348 | if ($shareNode instanceof \OCP\Files\Folder) { |
||
| 349 | |||
| 350 | $shareIsFolder = true; |
||
| 351 | |||
| 352 | try { |
||
| 353 | $folderNode = $shareNode->get($path); |
||
| 354 | } catch (\OCP\Files\NotFoundException $e) { |
||
| 355 | $this->emitAccessShareHook($share, 404, 'Share not found'); |
||
| 356 | throw new NotFoundException(); |
||
| 357 | } |
||
| 358 | |||
| 359 | $shareTmpl['dir'] = $shareNode->getRelativePath($folderNode->getPath()); |
||
| 360 | |||
| 361 | /* |
||
| 362 | * The OC_Util methods require a view. This just uses the node API |
||
| 363 | */ |
||
| 364 | $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
||
| 365 | if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
||
| 366 | $freeSpace = max($freeSpace, 0); |
||
| 367 | } else { |
||
| 368 | $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
||
| 369 | } |
||
| 370 | |||
| 371 | $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ); |
||
| 372 | $maxUploadFilesize = $freeSpace; |
||
| 373 | |||
| 374 | $folder = new Template('files', 'list', ''); |
||
| 375 | |||
| 376 | $folder->assign('dir', $shareNode->getRelativePath($folderNode->getPath())); |
||
| 377 | $folder->assign('dirToken', $this->getToken()); |
||
| 378 | $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
||
| 379 | $folder->assign('isPublic', true); |
||
| 380 | $folder->assign('hideFileList', $hideFileList); |
||
| 381 | $folder->assign('publicUploadEnabled', 'no'); |
||
| 382 | // default to list view |
||
| 383 | $folder->assign('showgridview', false); |
||
| 384 | $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
||
| 385 | $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
||
| 386 | $folder->assign('freeSpace', $freeSpace); |
||
| 387 | $folder->assign('usedSpacePercent', 0); |
||
| 388 | $folder->assign('trash', false); |
||
| 389 | $shareTmpl['folder'] = $folder->fetchPage(); |
||
| 390 | } else { |
||
| 391 | $shareIsFolder = false; |
||
| 392 | } |
||
| 393 | |||
| 394 | // default to list view |
||
| 395 | $shareTmpl['showgridview'] = false; |
||
| 396 | |||
| 397 | $shareTmpl['hideFileList'] = $hideFileList; |
||
| 398 | $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
||
| 399 | $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $this->getToken()]); |
||
| 400 | $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $this->getToken()]); |
||
| 401 | $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
||
| 402 | $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
||
| 403 | $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
||
| 404 | $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
||
| 405 | $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
||
| 406 | $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
||
| 407 | |||
| 408 | if ($shareTmpl['previewSupported']) { |
||
| 409 | $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
||
| 410 | ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]); |
||
| 411 | $ogPreview = $shareTmpl['previewImage']; |
||
| 412 | |||
| 413 | // We just have direct previews for image files |
||
| 414 | if ($shareNode->getMimePart() === 'image') { |
||
| 415 | $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $this->getToken()]); |
||
| 416 | |||
| 417 | $ogPreview = $shareTmpl['previewURL']; |
||
| 418 | |||
| 419 | //Whatapp is kind of picky about their size requirements |
||
| 420 | if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
||
| 421 | $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
||
| 422 | 'token' => $this->getToken(), |
||
| 423 | 'x' => 256, |
||
| 424 | 'y' => 256, |
||
| 425 | 'a' => true, |
||
| 426 | ]); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } else { |
||
| 430 | $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
||
| 431 | $ogPreview = $shareTmpl['previewImage']; |
||
| 432 | } |
||
| 433 | |||
| 434 | // Load files we need |
||
| 435 | \OCP\Util::addScript('files', 'semaphore'); |
||
| 436 | \OCP\Util::addScript('files', 'file-upload'); |
||
| 437 | \OCP\Util::addStyle('files_sharing', 'publicView'); |
||
| 438 | \OCP\Util::addScript('files_sharing', 'public'); |
||
| 439 | \OCP\Util::addScript('files_sharing', 'templates'); |
||
| 440 | \OCP\Util::addScript('files', 'fileactions'); |
||
| 441 | \OCP\Util::addScript('files', 'fileactionsmenu'); |
||
| 442 | \OCP\Util::addScript('files', 'jquery.fileupload'); |
||
| 443 | \OCP\Util::addScript('files_sharing', 'files_drop'); |
||
| 444 | |||
| 445 | if (isset($shareTmpl['folder'])) { |
||
| 446 | // JS required for folders |
||
| 447 | \OCP\Util::addStyle('files', 'merged'); |
||
| 448 | \OCP\Util::addScript('files', 'filesummary'); |
||
| 449 | \OCP\Util::addScript('files', 'templates'); |
||
| 450 | \OCP\Util::addScript('files', 'breadcrumb'); |
||
| 451 | \OCP\Util::addScript('files', 'fileinfomodel'); |
||
| 452 | \OCP\Util::addScript('files', 'newfilemenu'); |
||
| 453 | \OCP\Util::addScript('files', 'files'); |
||
| 454 | \OCP\Util::addScript('files', 'filemultiselectmenu'); |
||
| 455 | \OCP\Util::addScript('files', 'filelist'); |
||
| 456 | \OCP\Util::addScript('files', 'keyboardshortcuts'); |
||
| 457 | \OCP\Util::addScript('files', 'operationprogressbar'); |
||
| 458 | |||
| 459 | // Load Viewer scripts |
||
| 460 | if (class_exists(LoadViewer::class)) { |
||
| 461 | $this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer()); |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | // OpenGraph Support: http://ogp.me/ |
||
| 466 | \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
||
| 467 | \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
||
| 468 | \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
||
| 469 | \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
||
| 470 | \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
||
| 471 | \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); |
||
| 472 | |||
| 473 | $event = new GenericEvent(null, ['share' => $share]); |
||
| 474 | $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts', $event); |
||
| 475 | |||
| 476 | $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
||
| 477 | $csp->addAllowedFrameDomain('\'self\''); |
||
| 478 | |||
| 479 | $response = new PublicTemplateResponse($this->appName, 'public', $shareTmpl); |
||
| 480 | $response->setHeaderTitle($shareTmpl['filename']); |
||
| 481 | $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['displayName']])); |
||
| 482 | |||
| 483 | $isNoneFileDropFolder = $shareIsFolder === false || $share->getPermissions() !== \OCP\Constants::PERMISSION_CREATE; |
||
| 484 | |||
| 485 | if ($isNoneFileDropFolder && !$share->getHideDownload()) { |
||
| 486 | \OCP\Util::addScript('files_sharing', 'public_note'); |
||
| 487 | |||
| 488 | $downloadWhite = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
||
| 489 | $downloadAllWhite = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
||
| 490 | $download = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
||
| 491 | $downloadAll = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
||
| 492 | $directLink = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']); |
||
| 493 | $externalShare = new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $shareTmpl['owner'], $shareTmpl['displayName'], $shareTmpl['filename']); |
||
| 494 | |||
| 495 | $responseComposer = []; |
||
| 496 | |||
| 497 | if ($shareIsFolder) { |
||
| 498 | $responseComposer[] = $downloadAllWhite; |
||
| 499 | $responseComposer[] = $downloadAll; |
||
| 500 | } else { |
||
| 501 | $responseComposer[] = $downloadWhite; |
||
| 502 | $responseComposer[] = $download; |
||
| 503 | } |
||
| 504 | $responseComposer[] = $directLink; |
||
| 505 | if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
||
| 506 | $responseComposer[] = $externalShare; |
||
| 507 | } |
||
| 508 | |||
| 509 | $response->setHeaderActions($responseComposer); |
||
| 510 | } |
||
| 511 | |||
| 512 | $response->setContentSecurityPolicy($csp); |
||
| 513 | |||
| 514 | $this->emitAccessShareHook($share); |
||
| 515 | |||
| 516 | return $response; |
||
| 517 | } |
||
| 732 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths