diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php index 0625265dd052c..03efe4d1e52f8 100644 --- a/core/Controller/AvatarController.php +++ b/core/Controller/AvatarController.php @@ -132,12 +132,13 @@ public function getAvatar($userId, $size) { } try { - $avatar = $this->avatarManager->getAvatar($userId)->getFile($size); + $avatar = $this->avatarManager->getAvatar($userId); + $avatarFile = $avatar->getFile($size); $resp = new FileDisplayResponse( - $avatar, - Http::STATUS_OK, - ['Content-Type' => $avatar->getMimeType() - ]); + $avatarFile, + $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED, + ['Content-Type' => $avatarFile->getMimeType()] + ); } catch (\Exception $e) { $resp = new Http\Response(); $resp->setStatus(Http::STATUS_NOT_FOUND); diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php index 9dbeb4ac7455a..116f8368e710f 100644 --- a/lib/private/Avatar.php +++ b/lib/private/Avatar.php @@ -119,6 +119,15 @@ public function exists() { return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); } + /** + * Check if the avatar of a user is a custom uploaded one + * + * @return bool + */ + public function isCustomAvatar(): bool { + return !$this->folder->fileExists('generated'); + } + /** * sets the users avatar * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar @@ -362,7 +371,7 @@ private function generateAvatar($userDisplayName, $size) { * @param string $font font path * @param int $size font size * @param int $angle - * @return Array + * @return array */ protected function imageTTFCenter($image, string $text, string $font, int $size, $angle = 0): array { // Image width & height diff --git a/lib/public/IAvatar.php b/lib/public/IAvatar.php index 858633570696e..448d5dfc02f6c 100644 --- a/lib/public/IAvatar.php +++ b/lib/public/IAvatar.php @@ -53,6 +53,14 @@ public function get($size = 64); */ public function exists(); + /** + * Check if the avatar of a user is a custom uploaded one + * + * @return bool + * @since 14.0.0 + */ + public function isCustomAvatar(): bool; + /** * sets the users avatar * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 3194d671908ac..3369fa882c8cf 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -34,7 +34,7 @@ function is_uploaded_file($filename) { use OC\AppFramework\Utility\TimeFactory; use OC\Core\Controller\AvatarController; use OCP\AppFramework\Http; -use OCP\Files\Cache\ICache; +use OCP\ICache; use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -143,6 +143,9 @@ public function testGetAvatarNoAvatar() { public function testGetAvatar() { $this->avatarMock->method('getFile')->willReturn($this->avatarFile); $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); + $this->avatarMock->expects($this->once()) + ->method('isCustomAvatar') + ->willReturn(true); $response = $this->avatarController->getAvatar('userId', 32); @@ -153,6 +156,22 @@ public function testGetAvatar() { $this->assertEquals('my etag', $response->getETag()); } + /** + * Fetch the user's avatar + */ + public function testGetGeneratedAvatar() { + $this->avatarMock->method('getFile')->willReturn($this->avatarFile); + $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); + + $response = $this->avatarController->getAvatar('userId', 32); + + $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); + $this->assertArrayHasKey('Content-Type', $response->getHeaders()); + $this->assertEquals('image type', $response->getHeaders()['Content-Type']); + + $this->assertEquals('my etag', $response->getETag()); + } + /** * Fetch the avatar of a non-existing user */