diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-02-03 17:38:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-03 17:38:29 +0100 |
commit | 20f1971266c380ce2a15bf4b8a9d2a4f5f4e1545 (patch) | |
tree | 69e34adce4cbba86dc4934888f94d09213d6b82c | |
parent | eb1927040f8e059bd0a291f4a9db41b2ef48acf2 (diff) | |
parent | 3d0b5c1ff9725b9babb8c92d0549613b64b6296e (diff) | |
download | nextcloud-server-20f1971266c380ce2a15bf4b8a9d2a4f5f4e1545.tar.gz nextcloud-server-20f1971266c380ce2a15bf4b8a9d2a4f5f4e1545.zip |
Merge pull request #30624 from nextcloud/theming/background-image
Avoid file system access on checking if an image exists
-rw-r--r-- | apps/theming/lib/ImageManager.php | 9 | ||||
-rw-r--r-- | apps/theming/tests/ImageManagerTest.php | 18 |
2 files changed, 11 insertions, 16 deletions
diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index 762461e7a1a..6f9892af10e 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -78,10 +78,8 @@ class ImageManager { public function getImageUrl(string $key, bool $useSvg = true): string { $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); - try { - $image = $this->getImage($key, $useSvg); + if ($this->hasImage($key)) { return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; - } catch (NotFoundException $e) { } switch ($key) { @@ -132,6 +130,11 @@ class ImageManager { return $folder->getFile($key); } + public function hasImage(string $key): bool { + $mimeSetting = $this->config->getAppValue('theming', $key . 'Mime', ''); + return $mimeSetting !== ''; + } + /** * @return array<string, array{mime: string, url: string}> */ diff --git a/apps/theming/tests/ImageManagerTest.php b/apps/theming/tests/ImageManagerTest.php index 71752e99a2c..10faf6c1da1 100644 --- a/apps/theming/tests/ImageManagerTest.php +++ b/apps/theming/tests/ImageManagerTest.php @@ -136,7 +136,6 @@ class ImageManagerTest extends TestCase { ['theming', 'logoMime', ''] ) ->willReturn(0); - $this->mockGetImage('logo', $file); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->willReturn('url-to-image'); @@ -148,9 +147,9 @@ class ImageManagerTest extends TestCase { ->method('getAppValue') ->withConsecutive( ['theming', 'cachebuster', '0'], - ['theming', 'logoMime', false] + ['theming', 'logoMime', ''] ) - ->willReturnOnConsecutiveCalls(0, false); + ->willReturnOnConsecutiveCalls(0, ''); $this->urlGenerator->expects($this->once()) ->method('imagePath') ->with('core', 'logo/logo.png') @@ -167,15 +166,8 @@ class ImageManagerTest extends TestCase { ['theming', 'cachebuster', '0'], ['theming', 'logoMime', ''] ) - ->willReturn(0); - $this->mockGetImage('logo', $file); - $this->urlGenerator->expects($this->at(0)) - ->method('getBaseUrl') - ->willReturn('baseurl'); - $this->urlGenerator->expects($this->at(1)) - ->method('getAbsoluteUrl') - ->willReturn('url-to-image-absolute?v=0'); - $this->urlGenerator->expects($this->at(2)) + ->willReturnOnConsecutiveCalls(0, 0); + $this->urlGenerator->expects($this->any()) ->method('getAbsoluteUrl') ->willReturn('url-to-image-absolute?v=0'); $this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo', false)); @@ -207,7 +199,7 @@ class ImageManagerTest extends TestCase { ->method('getAppValue') ->with('theming', 'cachebuster', '0') ->willReturn('0'); - $this->appData->expects($this->at(0)) + $this->appData->expects($this->once()) ->method('getFolder') ->with('0') ->willReturn($folder); |