diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-01 22:08:42 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-01 22:15:43 +0100 |
commit | b00db2c9334874c6062a143de2a6b76b44dca35d (patch) | |
tree | db13d5d23114468e978786cc52031dc31c312584 /lib/private/avatar.php | |
parent | 8931ba4a0d574a05cf4415f2556bdd3ee0388ba9 (diff) | |
download | nextcloud-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/private/avatar.php')
-rw-r--r-- | lib/private/avatar.php | 61 |
1 files changed, 35 insertions, 26 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) {} } } |