Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the info available if the avatar was uploaded or generated #10481

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion lib/private/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/public/IAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion tests/Core/Controller/AvatarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
*/
Expand Down