summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-17 14:43:21 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-17 14:43:21 +0100
commite3ed42135dca2d0fce1a46d9e52dd630dadd0e9f (patch)
treec5b70c9970639dc2b603cf5ca389e30e67fd3409 /lib
parent358b84c21d31fa67dfde75047deb5a66f8c65360 (diff)
parent3e80f142693fd0d78ff6f4fcd8fd36dd1651e209 (diff)
downloadnextcloud-server-e3ed42135dca2d0fce1a46d9e52dd630dadd0e9f.tar.gz
nextcloud-server-e3ed42135dca2d0fce1a46d9e52dd630dadd0e9f.zip
Merge pull request #21240 from owncloud/avatar_speedup
Avatar speedup
Diffstat (limited to 'lib')
-rw-r--r--lib/private/avatar.php69
-rw-r--r--lib/public/iavatar.php11
2 files changed, 62 insertions, 18 deletions
diff --git a/lib/private/avatar.php b/lib/private/avatar.php
index 37a813f3ff8..c87facd25da 100644
--- a/lib/private/avatar.php
+++ b/lib/private/avatar.php
@@ -31,6 +31,7 @@ namespace OC;
use OCP\Files\Folder;
use OCP\Files\File;
+use OCP\Files\NotFoundException;
use OCP\IL10N;
use OC_Image;
@@ -62,28 +63,14 @@ 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->folder->nodeExists('avatar.jpg')) {
- $ext = 'jpg';
- } elseif ($this->folder->nodeExists('avatar.png')) {
- $ext = 'png';
- } else {
+ try {
+ $file = $this->getFile($size);
+ } catch (NotFoundException $e) {
return false;
}
$avatar = new OC_Image();
- if ($this->folder->nodeExists('avatar.' . $size . '.' . $ext)) {
- /** @var File $node */
- $node = $this->folder->get('avatar.' . $size . '.' . $ext);
- $avatar->loadFromData($node->getContent());
- } else {
- /** @var File $node */
- $node = $this->folder->get('avatar.' . $ext);
- $avatar->loadFromData($node->getContent());
- if ($size > 0) {
- $avatar->resize($size);
- }
- $this->folder->newFile('avatar.' . $size . '.' . $ext)->putContent($avatar->data());
- }
+ $avatar->loadFromData($file->getContent());
return $avatar;
}
@@ -144,4 +131,50 @@ class Avatar implements \OCP\IAvatar {
$this->folder->get('avatar.png')->delete();
} catch (\OCP\Files\NotFoundException $e) {}
}
+
+ /**
+ * Get the File of an avatar of size $size.
+ *
+ * @param int $size
+ * @return File
+ * @throws NotFoundException
+ */
+ public function getFile($size) {
+ $ext = $this->getExtention();
+
+ $path = 'avatar.' . $size . '.' . $ext;
+
+ try {
+ $file = $this->folder->get($path);
+ } catch (NotFoundException $e) {
+ if ($size <= 0) {
+ throw new NotFoundException;
+ }
+
+ $avatar = new OC_Image();
+ /** @var File $file */
+ $file = $this->folder->get('avatar.' . $ext);
+ $avatar->loadFromData($file->getContent());
+ $avatar->resize($size);
+ $file = $this->folder->newFile($path);
+ $file->putContent($avatar->data());
+ }
+
+ return $file;
+ }
+
+ /**
+ * Get the extention of the avatar. If there is no avatar throw Exception
+ *
+ * @return string
+ * @throws NotFoundException
+ */
+ private function getExtention() {
+ if ($this->folder->nodeExists('avatar.jpg')) {
+ return 'jpg';
+ } elseif ($this->folder->nodeExists('avatar.png')) {
+ return 'png';
+ }
+ throw new NotFoundException;
+ }
}
diff --git a/lib/public/iavatar.php b/lib/public/iavatar.php
index fc29212a599..3d92d00b83d 100644
--- a/lib/public/iavatar.php
+++ b/lib/public/iavatar.php
@@ -24,6 +24,8 @@
*/
namespace OCP;
+use OCP\Files\File;
+use OCP\Files\NotFoundException;
/**
* This class provides avatar functionality
@@ -64,4 +66,13 @@ interface IAvatar {
* @since 6.0.0
*/
public function remove();
+
+ /**
+ * Get the file of the avatar
+ * @param int $size
+ * @return File
+ * @throws NotFoundException
+ * @since 9.0.0
+ */
+ public function getFile($size);
}