summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/private/avatar.php8
-rw-r--r--lib/private/avatarmanager.php26
-rw-r--r--lib/private/server.php12
-rw-r--r--lib/public/iavatar.php38
-rw-r--r--lib/public/iavatarmanager.php23
-rw-r--r--lib/public/iservercontainer.php6
-rw-r--r--tests/lib/avatar.php15
7 files changed, 125 insertions, 3 deletions
diff --git a/lib/private/avatar.php b/lib/private/avatar.php
index 720740569df..814a9b22bed 100644
--- a/lib/private/avatar.php
+++ b/lib/private/avatar.php
@@ -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"));
diff --git a/lib/private/avatarmanager.php b/lib/private/avatarmanager.php
new file mode 100644
index 00000000000..3ca46868ea6
--- /dev/null
+++ b/lib/private/avatarmanager.php
@@ -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);
+ }
+}
diff --git a/lib/private/server.php b/lib/private/server.php
index 65899f3007e..77c3732a9ca 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -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();
+ });
}
/**
@@ -171,6 +174,15 @@ class Server extends SimpleContainer implements IServerContainer {
}
/**
+ * 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
*
* @return \OCP\Files\Folder
diff --git a/lib/public/iavatar.php b/lib/public/iavatar.php
new file mode 100644
index 00000000000..2cbec0d45c3
--- /dev/null
+++ b/lib/public/iavatar.php
@@ -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();
+}
diff --git a/lib/public/iavatarmanager.php b/lib/public/iavatarmanager.php
new file mode 100644
index 00000000000..9b185ae0467
--- /dev/null
+++ b/lib/public/iavatarmanager.php
@@ -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);
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index 14822817a47..36296a59850 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -154,4 +154,10 @@ interface IServerContainer {
*/
function getDatabaseConnection();
+ /**
+ * @brief Returns an avatar manager, used for avatar functionality
+ * @return \OCP\IAvatarManager
+ */
+ function getAvatarManager();
+
}
diff --git a/tests/lib/avatar.php b/tests/lib/avatar.php
index 6e6faed2d2c..9cc14865a29 100644
--- a/tests/lib/avatar.php
+++ b/tests/lib/avatar.php
@@ -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());
+ }
}