Merge pull request #21885 from owncloud/add-size-minus-zero-to-avatar
Adding support of -1 as size to be passed into get and getFile
This commit is contained in:
commit
fe08700b59
|
@ -58,9 +58,7 @@ class Avatar implements \OCP\IAvatar {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the users avatar
|
* @inheritdoc
|
||||||
* @param int $size size in px of the avatar, avatars are square, defaults to 64
|
|
||||||
* @return boolean|\OCP\IImage containing the avatar or false if there's no image
|
|
||||||
*/
|
*/
|
||||||
public function get ($size = 64) {
|
public function get ($size = 64) {
|
||||||
try {
|
try {
|
||||||
|
@ -135,16 +133,16 @@ class Avatar implements \OCP\IAvatar {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the File of an avatar of size $size.
|
* @inheritdoc
|
||||||
*
|
|
||||||
* @param int $size
|
|
||||||
* @return File
|
|
||||||
* @throws NotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function getFile($size) {
|
public function getFile($size) {
|
||||||
$ext = $this->getExtention();
|
$ext = $this->getExtention();
|
||||||
|
|
||||||
|
if ($size === -1) {
|
||||||
|
$path = 'avatar.' . $ext;
|
||||||
|
} else {
|
||||||
$path = 'avatar.' . $size . '.' . $ext;
|
$path = 'avatar.' . $size . '.' . $ext;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$file = $this->folder->get($path);
|
$file = $this->folder->get($path);
|
||||||
|
@ -157,7 +155,9 @@ class Avatar implements \OCP\IAvatar {
|
||||||
/** @var File $file */
|
/** @var File $file */
|
||||||
$file = $this->folder->get('avatar.' . $ext);
|
$file = $this->folder->get('avatar.' . $ext);
|
||||||
$avatar->loadFromData($file->getContent());
|
$avatar->loadFromData($file->getContent());
|
||||||
|
if ($size !== -1) {
|
||||||
$avatar->resize($size);
|
$avatar->resize($size);
|
||||||
|
}
|
||||||
$file = $this->folder->newFile($path);
|
$file = $this->folder->newFile($path);
|
||||||
$file->putContent($avatar->data());
|
$file->putContent($avatar->data());
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,9 @@ interface IAvatar {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the users avatar
|
* get the users avatar
|
||||||
* @param int $size size in px of the avatar, avatars are square, defaults to 64
|
* @param int $size size in px of the avatar, avatars are square, defaults to 64, -1 can be used to not scale the image
|
||||||
* @return boolean|\OCP\IImage containing the avatar or false if there's no image
|
* @return boolean|\OCP\IImage containing the avatar or false if there's no image
|
||||||
* @since 6.0.0
|
* @since 6.0.0 - size of -1 was added in 9.0.0
|
||||||
*/
|
*/
|
||||||
public function get($size = 64);
|
public function get($size = 64);
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ interface IAvatar {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the file of the avatar
|
* Get the file of the avatar
|
||||||
* @param int $size
|
* @param int $size -1 can be used to not scale the image
|
||||||
* @return File
|
* @return File
|
||||||
* @throws NotFoundException
|
* @throws NotFoundException
|
||||||
* @since 9.0.0
|
* @since 9.0.0
|
||||||
|
|
|
@ -7,11 +7,10 @@
|
||||||
* See the COPYING-README file.
|
* See the COPYING-README file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use OC\Avatar;
|
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
|
|
||||||
class AvatarTest extends \Test\TestCase {
|
class AvatarTest extends \Test\TestCase {
|
||||||
/** @var Folder */
|
/** @var Folder | PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $folder;
|
private $folder;
|
||||||
|
|
||||||
/** @var \OC\Avatar */
|
/** @var \OC\Avatar */
|
||||||
|
@ -24,7 +23,6 @@ class AvatarTest extends \Test\TestCase {
|
||||||
$l = $this->getMock('\OCP\IL10N');
|
$l = $this->getMock('\OCP\IL10N');
|
||||||
$l->method('t')->will($this->returnArgument(0));
|
$l->method('t')->will($this->returnArgument(0));
|
||||||
$this->avatar = new \OC\Avatar($this->folder, $l);
|
$this->avatar = new \OC\Avatar($this->folder, $l);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetNoAvatar() {
|
public function testGetNoAvatar() {
|
||||||
|
@ -47,6 +45,21 @@ class AvatarTest extends \Test\TestCase {
|
||||||
$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
|
$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetAvatarSizeMinusOne() {
|
||||||
|
$this->folder->method('nodeExists')
|
||||||
|
->will($this->returnValueMap([
|
||||||
|
['avatar.jpg', true],
|
||||||
|
]));
|
||||||
|
|
||||||
|
$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
|
||||||
|
|
||||||
|
$file = $this->getMock('\OCP\Files\File');
|
||||||
|
$file->method('getContent')->willReturn($expected->data());
|
||||||
|
$this->folder->method('get')->with('avatar.jpg')->willReturn($file);
|
||||||
|
|
||||||
|
$this->assertEquals($expected->data(), $this->avatar->get(-1)->data());
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetAvatarNoSizeMatch() {
|
public function testGetAvatarNoSizeMatch() {
|
||||||
$this->folder->method('nodeExists')
|
$this->folder->method('nodeExists')
|
||||||
->will($this->returnValueMap([
|
->will($this->returnValueMap([
|
||||||
|
|
Loading…
Reference in New Issue