]> source.dussan.org Git - nextcloud-server.git/commitdiff
[Avatars] Add function to get the Node of the avatar
authorRoeland Jago Douma <rullzer@owncloud.com>
Wed, 16 Dec 2015 19:26:00 +0000 (20:26 +0100)
committerRoeland Jago Douma <rullzer@owncloud.com>
Wed, 16 Dec 2015 19:29:02 +0000 (20:29 +0100)
Since we usually just get the avatar and stream the content to the users
there is no need to first create an image in memory.

lib/private/avatar.php
lib/public/iavatar.php
tests/lib/avatartest.php

index 37a813f3ff8f3dcef3405efbed44c5118249cfaa..c87facd25da698740ce1ff1a319356a927228645 100644 (file)
@@ -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;
+       }
 }
index fc29212a599b1bc6489cd3449f73ae2c1d9251cd..3d92d00b83d0c3343ba7b158f05c20d0523392f9 100644 (file)
@@ -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);
 }
index 49e8be98c83e13c2850ba95c9aecd9db2b96c768..3d77a282a7d4db88a498d4595381025f005f75a6 100644 (file)
@@ -60,12 +60,25 @@ class AvatarTest extends \Test\TestCase {
 
                $file = $this->getMock('\OCP\Files\File');
                $file->method('getContent')->willReturn($expected->data());
-               $this->folder->method('get')->with('avatar.png')->willReturn($file);
+
+               $this->folder->method('get')
+                       ->will($this->returnCallback(
+                               function($path) use ($file) {
+                                       if ($path === 'avatar.png') {
+                                               return $file;
+                                       } else {
+                                               throw new \OCP\Files\NotFoundException;
+                                       }
+                               }
+                       ));
 
                $newFile = $this->getMock('\OCP\Files\File');
                $newFile->expects($this->once())
                        ->method('putContent')
                        ->with($expected2->data());
+               $newFile->expects($this->once())
+                       ->method('getContent')
+                       ->willReturn($expected2->data());
                $this->folder->expects($this->once())
                        ->method('newFile')
                        ->with('avatar.32.png')