Merge pull request #10481 from nextcloud/feature/noid/make-info-available-if-the-avatar-was-uploaded

Make the info available if the avatar was uploaded or generated
This commit is contained in:
Roeland Jago Douma 2018-08-01 22:54:38 +02:00 committed by GitHub
commit fae7e516b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 7 deletions

View File

@ -132,12 +132,13 @@ class AvatarController extends Controller {
} }
try { try {
$avatar = $this->avatarManager->getAvatar($userId)->getFile($size); $avatar = $this->avatarManager->getAvatar($userId);
$avatarFile = $avatar->getFile($size);
$resp = new FileDisplayResponse( $resp = new FileDisplayResponse(
$avatar, $avatarFile,
Http::STATUS_OK, $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
['Content-Type' => $avatar->getMimeType() ['Content-Type' => $avatarFile->getMimeType()]
]); );
} catch (\Exception $e) { } catch (\Exception $e) {
$resp = new Http\Response(); $resp = new Http\Response();
$resp->setStatus(Http::STATUS_NOT_FOUND); $resp->setStatus(Http::STATUS_NOT_FOUND);

View File

@ -119,6 +119,15 @@ class Avatar implements IAvatar {
return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); 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 * sets the users avatar
* @param IImage|resource|string $data An image object, imagedata or path to set a new avatar * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
@ -362,7 +371,7 @@ class Avatar implements IAvatar {
* @param string $font font path * @param string $font font path
* @param int $size font size * @param int $size font size
* @param int $angle * @param int $angle
* @return Array * @return array
*/ */
protected function imageTTFCenter($image, string $text, string $font, int $size, $angle = 0): array { protected function imageTTFCenter($image, string $text, string $font, int $size, $angle = 0): array {
// Image width & height // Image width & height

View File

@ -53,6 +53,14 @@ interface IAvatar {
*/ */
public function exists(); 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 * sets the users avatar
* @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar

View File

@ -34,7 +34,7 @@ namespace Tests\Core\Controller;
use OC\AppFramework\Utility\TimeFactory; use OC\AppFramework\Utility\TimeFactory;
use OC\Core\Controller\AvatarController; use OC\Core\Controller\AvatarController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\Files\Cache\ICache; use OCP\ICache;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -143,6 +143,9 @@ class AvatarControllerTest extends \Test\TestCase {
public function testGetAvatar() { public function testGetAvatar() {
$this->avatarMock->method('getFile')->willReturn($this->avatarFile); $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
$this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock); $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
$this->avatarMock->expects($this->once())
->method('isCustomAvatar')
->willReturn(true);
$response = $this->avatarController->getAvatar('userId', 32); $response = $this->avatarController->getAvatar('userId', 32);
@ -153,6 +156,22 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertEquals('my etag', $response->getETag()); $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 * Fetch the avatar of a non-existing user
*/ */