diff --git a/lib/CurrentUser.php b/lib/CurrentUser.php index 169225d09..2874b4688 100644 --- a/lib/CurrentUser.php +++ b/lib/CurrentUser.php @@ -18,6 +18,9 @@ class CurrentUser { + private bool $publicShareLoaded = false; + private ?IShare $publicShare = null; + public function __construct( protected readonly IUserSession $userSession, protected readonly IRequest $request, @@ -107,6 +110,15 @@ protected function getCloudIDFromToken(): ?string { } protected function getPublicShare(): ?IShare { + if (!$this->publicShareLoaded) { + $this->publicShare = $this->loadPublicShare(); + $this->publicShareLoaded = true; + } + + return $this->publicShare; + } + + private function loadPublicShare(): ?IShare { if (basename($this->request->getScriptName()) !== 'public.php') { return null; } diff --git a/tests/CurrentUserTest.php b/tests/CurrentUserTest.php index 8fc28dd28..16beeffc7 100644 --- a/tests/CurrentUserTest.php +++ b/tests/CurrentUserTest.php @@ -205,6 +205,34 @@ public function testGetCloudIDFromToken(array $server, array $shareData, ?string $this->assertSame($expected, self::invokePrivate($instance, 'getCloudIDFromToken')); } + public static function dataPublicShareIsLoadedOnce(): array { + return [ + [IShare::TYPE_LINK, true], + [new ShareNotFound(), false], + ]; + } + + #[DataProvider('dataPublicShareIsLoadedOnce')] + public function testPublicShareIsLoadedOnce(int|ShareNotFound $result, bool $expected): void { + $this->request->server = ['PHP_AUTH_USER' => 'token']; + $method = $this->shareManager->expects($this->once()) + ->method('getShareByToken') + ->with('token'); + if ($result instanceof ShareNotFound) { + $method->willThrowException($result); + } else { + $share = $this->createMock(IShare::class); + $share->method('getShareType') + ->willReturn($result); + $method->willReturn($share); + } + + $instance = $this->getInstance(); + $this->assertSame($expected, $instance->isPublicShareToken()); + $this->assertSame($expected, $instance->isPublicShareToken()); + $this->assertNull(self::invokePrivate($instance, 'getCloudIDFromToken')); + } + public function testGetCloudIdFromUser(): void { $user = $this->createMock(IUser::class); $user->method('getCloudId')->willReturn('user@cloud.example.com'); diff --git a/tests/benchmark/profile-prepend.php b/tests/benchmark/profile-prepend.php new file mode 100644 index 000000000..f7b71cb53 --- /dev/null +++ b/tests/benchmark/profile-prepend.php @@ -0,0 +1,34 @@ +setPeriod(0.001); + $benchProfiler->setEventType(EXCIMER_REAL); + $benchProfiler->setMaxDepth(250); + $benchProfiler->start(); + + register_shutdown_function(static function () use ($benchProfiler): void { + $benchProfiler->stop(); + $label = preg_replace('/[^A-Za-z0-9_.-]/', '', (string)$_SERVER['HTTP_X_BENCH_PROFILE']); + $dir = '/tmp/bench-profiles/' . $label; + if (!is_dir($dir)) { + @mkdir($dir, 0777, true); + } + file_put_contents($dir . '/' . uniqid('', true) . '.collapsed', $benchProfiler->getLog()->formatCollapsed()); + }); +}