summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2015-12-01 22:08:42 +0100
committerRoeland Jago Douma <rullzer@owncloud.com>2015-12-01 22:15:43 +0100
commitb00db2c9334874c6062a143de2a6b76b44dca35d (patch)
treedb13d5d23114468e978786cc52031dc31c312584 /lib
parent8931ba4a0d574a05cf4415f2556bdd3ee0388ba9 (diff)
downloadnextcloud-server-b00db2c9334874c6062a143de2a6b76b44dca35d.tar.gz
nextcloud-server-b00db2c9334874c6062a143de2a6b76b44dca35d.zip
DI in avatar code
* DI in avatar code * Use the node API * More unit tests * Unit tests no longer require DB
Diffstat (limited to 'lib')
-rw-r--r--lib/private/avatar.php61
-rw-r--r--lib/private/avatarmanager.php27
-rw-r--r--lib/private/helper.php4
-rw-r--r--lib/private/server.php8
4 files changed, 68 insertions, 32 deletions
diff --git a/lib/private/avatar.php b/lib/private/avatar.php
index 90a8b8c26f9..872da35f947 100644
--- a/lib/private/avatar.php
+++ b/lib/private/avatar.php
@@ -29,7 +29,9 @@
namespace OC;
-use OC\Files\Filesystem;
+use OCP\Files\Folder;
+use OCP\Files\File;
+use OCP\IL10N;
use OC_Image;
/**
@@ -37,19 +39,21 @@ use OC_Image;
*/
class Avatar implements \OCP\IAvatar {
- /** @var Files\View */
- private $view;
+ /** @var Folder */
+ private $folder;
+
+ /** @var IL10N */
+ private $l;
/**
* constructor
- * @param string $user user to do avatar-management with
- * @throws \Exception In case the username is potentially dangerous
+ *
+ * @param Folder $folder The folder where the avatars are
+ * @param IL10N $l
*/
- public function __construct ($user) {
- if(!Filesystem::isValidPath($user)) {
- throw new \Exception('Username may not contain slashes');
- }
- $this->view = new \OC\Files\View('/'.$user);
+ public function __construct (Folder $folder, IL10N $l) {
+ $this->folder = $folder;
+ $this->l = $l;
}
/**
@@ -58,21 +62,25 @@ class Avatar implements \OCP\IAvatar {
* @return boolean|\OCP\IImage containing the avatar or false if there's no image
*/
public function get ($size = 64) {
- if ($this->view->file_exists('avatar.jpg')) {
+ if ($this->folder->nodeExists('avatar.jpg')) {
$ext = 'jpg';
- } elseif ($this->view->file_exists('avatar.png')) {
+ } elseif ($this->folder->nodeExists('avatar.png')) {
$ext = 'png';
} else {
return false;
}
$avatar = new OC_Image();
- if ($this->view->file_exists('avatar.' . $size . '.' . $ext)) {
- $avatar->loadFromData($this->view->file_get_contents('avatar.' . $size . '.' . $ext));
+ if ($this->folder->nodeExists('avatar.' . $size . '.' . $ext)) {
+ /** @var File $node */
+ $node = $this->folder->get('avatar.' . $size . '.' . $ext);
+ $avatar->loadFromData($node->getContent());
} else {
- $avatar->loadFromData($this->view->file_get_contents('avatar.' . $ext));
+ /** @var File $node */
+ $node = $this->folder->get('avatar.' . $ext);
+ $avatar->loadFromData($node->getContent());
$avatar->resize($size);
- $this->view->file_put_contents('avatar.' . $size . '.' . $ext, $avatar->data());
+ $this->folder->newFile('avatar.' . $size . '.' . $ext)->putContent($avatar->data());
}
return $avatar;
}
@@ -83,7 +91,7 @@ class Avatar implements \OCP\IAvatar {
* @return bool
*/
public function exists() {
- return $this->view->file_exists('avatar.jpg') || $this->view->file_exists('avatar.png');
+ return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png');
}
/**
@@ -107,22 +115,19 @@ class Avatar implements \OCP\IAvatar {
$type = 'jpg';
}
if ($type !== 'jpg' && $type !== 'png') {
- $l = \OC::$server->getL10N('lib');
- throw new \Exception($l->t("Unknown filetype"));
+ throw new \Exception($this->l->t("Unknown filetype"));
}
if (!$img->valid()) {
- $l = \OC::$server->getL10N('lib');
- throw new \Exception($l->t("Invalid image"));
+ throw new \Exception($this->l->t("Invalid image"));
}
if (!($img->height() === $img->width())) {
throw new \OC\NotSquareException();
}
- $this->view->unlink('avatar.jpg');
- $this->view->unlink('avatar.png');
- $this->view->file_put_contents('avatar.'.$type, $data);
+ $this->remove();
+ $this->folder->newFile('avatar.'.$type)->putContent($data);
}
/**
@@ -130,7 +135,11 @@ class Avatar implements \OCP\IAvatar {
* @return void
*/
public function remove () {
- $this->view->unlink('avatar.jpg');
- $this->view->unlink('avatar.png');
+ try {
+ $this->folder->get('avatar.jpg')->delete();
+ } catch (\OCP\Files\NotFoundException $e) {}
+ try {
+ $this->folder->get('avatar.png')->delete();
+ } catch (\OCP\Files\NotFoundException $e) {}
}
}
diff --git a/lib/private/avatarmanager.php b/lib/private/avatarmanager.php
index 143874b6be3..6969b386e15 100644
--- a/lib/private/avatarmanager.php
+++ b/lib/private/avatarmanager.php
@@ -27,13 +27,33 @@
namespace OC;
use OCP\IAvatarManager;
-use OC\Avatar;
+use OCP\IUserManager;
+use OCP\Files\IRootFolder;
+use OCP\IL10N;
/**
* This class implements methods to access Avatar functionality
*/
class AvatarManager implements IAvatarManager {
+ /** @var IUserManager */
+ private $userManager;
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ /** @var IL10N */
+ private $l;
+
+ public function __construct(
+ IUserManager $userManager,
+ IRootFolder $rootFolder,
+ IL10N $l) {
+ $this->userManager = $userManager;
+ $this->rootFolder = $rootFolder;
+ $this->l = $l;
+ }
+
/**
* return a user specific instance of \OCP\IAvatar
* @see \OCP\IAvatar
@@ -42,6 +62,9 @@ class AvatarManager implements IAvatarManager {
* @throws \Exception In case the username is potentially dangerous
*/
public function getAvatar($user) {
- return new Avatar($user);
+ if (!$this->userManager->userExists($user)) {
+ throw new \Exception('user does not exist');
+ }
+ return new Avatar($this->rootFolder->getUserFolder($user)->getParent(), $this->l);
}
}
diff --git a/lib/private/helper.php b/lib/private/helper.php
index fb8e38cc26e..3cad77e6667 100644
--- a/lib/private/helper.php
+++ b/lib/private/helper.php
@@ -196,10 +196,10 @@ class OC_Helper {
* shows whether the user has an avatar
* @param string $user username
* @return bool avatar set or not
+ * @deprecated 9.0.0 Use \OC::$server->getAvatarManager()->getAvatar($user)->exists();
**/
public static function userAvatarSet($user) {
- $avatar = new \OC\Avatar($user);
- return $avatar->exists();
+ return \OC::$server->getAvatarManager()->getAvatar($user)->exists();
}
/**
diff --git a/lib/private/server.php b/lib/private/server.php
index fe0d9dcf55d..6692e6f6bbf 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -291,8 +291,12 @@ class Server extends SimpleContainer implements IServerContainer {
$c->getConfig()
);
});
- $this->registerService('AvatarManager', function ($c) {
- return new AvatarManager();
+ $this->registerService('AvatarManager', function (Server $c) {
+ return new AvatarManager(
+ $c->getUserManager(),
+ $c->getRootFolder(),
+ $c->getL10N('lib')
+ );
});
$this->registerService('Logger', function (Server $c) {
$logClass = $c->query('AllConfig')->getSystemValue('log_type', 'owncloud');