Merge pull request #5040 from owncloud/public_api_avatar_master

Add public API for \OC\Avatar
This commit is contained in:
blizzz 2013-11-22 03:42:28 -08:00
commit 2f73db12bb
7 changed files with 125 additions and 3 deletions

View file

@ -10,7 +10,7 @@
* This class gets and sets users avatars.
*/
class OC_Avatar {
class OC_Avatar implements \OCP\IAvatar {
private $view;
@ -24,7 +24,7 @@ class OC_Avatar {
/**
* @brief get the users avatar
* @param $size integer size in px of the avatar, defaults to 64
* @param $size integer size in px of the avatar, avatars are square, defaults to 64
* @return boolean|\OC_Image containing the avatar or false if there's no image
*/
public function get ($size = 64) {
@ -54,7 +54,9 @@ class OC_Avatar {
$img = new OC_Image($data);
$type = substr($img->mimeType(), -3);
if ($type === 'peg') { $type = 'jpg'; }
if ($type === 'peg') {
$type = 'jpg';
}
if ($type !== 'jpg' && $type !== 'png') {
$l = \OC_L10N::get('lib');
throw new \Exception($l->t("Unknown filetype"));

View file

@ -0,0 +1,26 @@
<?php
/**
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC;
use OCP\IAvatarManager;
/*
* This class implements methods to access Avatar functionality
*/
class AvatarManager implements IAvatarManager {
/**
* @brief return a user specific instance of \OCP\IAvatar
* @see \OCP\IAvatar
* @param $user string the ownCloud user id
* @return \OCP\IAvatar
*/
function getAvatar($user) {
return new \OC_Avatar($user);
}
}

View file

@ -131,6 +131,9 @@ class Server extends SimpleContainer implements IServerContainer {
$this->registerService('ActivityManager', function($c) {
return new ActivityManager();
});
$this->registerService('AvatarManager', function($c) {
return new AvatarManager();
});
}
/**
@ -170,6 +173,15 @@ class Server extends SimpleContainer implements IServerContainer {
return $this->query('TagManager');
}
/**
* Returns the avatar manager, used for avatar functionality
*
* @return \OCP\IAvatarManager
*/
function getAvatarManager() {
return $this->query('AvatarManager');
}
/**
* Returns the root folder of ownCloud's data directory
*

38
lib/public/iavatar.php Normal file
View file

@ -0,0 +1,38 @@
<?php
/**
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCP;
/**
* This class provides avatar functionality
*/
interface IAvatar {
/**
* @brief get the users avatar
* @param $size integer size in px of the avatar, avatars are square, defaults to 64
* @return boolean|\OC_Image containing the avatar or false if there's no image
*/
function get($size = 64);
/**
* @brief sets the users avatar
* @param $data mixed imagedata or path to set a new avatar
* @throws Exception if the provided file is not a jpg or png image
* @throws Exception if the provided image is not valid
* @throws \OCP\NotSquareException if the image is not square
* @return void
*/
function set($data);
/**
* @brief remove the users avatar
* @return void
*/
function remove();
}

View file

@ -0,0 +1,23 @@
<?php
/**
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCP;
/**
* This class provides avatar functionality
*/
interface IAvatarManager {
/**
* @brief return a user specific instance of \OCP\IAvatar
* @see \OCP\IAvatar
* @param $user string the ownCloud user id
* @return \OCP\IAvatar
*/
function getAvatar($user);
}

View file

@ -154,4 +154,10 @@ interface IServerContainer {
*/
function getDatabaseConnection();
/**
* @brief Returns an avatar manager, used for avatar functionality
* @return \OCP\IAvatarManager
*/
function getAvatarManager();
}

View file

@ -22,4 +22,19 @@ class Test_Avatar extends PHPUnit_Framework_TestCase {
$avatar->remove();
$this->assertEquals(false, $avatar->get());
}
public function testAvatarApi() {
$avatarManager = \OC::$server->getAvatarManager();
$avatar = $avatarManager->getAvatar(\OC_User::getUser());
$this->assertEquals(false, $avatar->get());
$expected = new OC_Image(\OC::$SERVERROOT.'/tests/data/testavatar.png');
$expected->resize(64);
$avatar->set($expected->data());
$this->assertEquals($expected->data(), $avatar->get()->data());
$avatar->remove();
$this->assertEquals(false, $avatar->get());
}
}