avatar.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OC\User\User;
  30. use OCP\Files\Folder;
  31. use OCP\Files\File;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\NotPermittedException;
  34. use OCP\IAvatar;
  35. use OCP\IImage;
  36. use OCP\IL10N;
  37. use OC_Image;
  38. use OCP\ILogger;
  39. /**
  40. * This class gets and sets users avatars.
  41. */
  42. class Avatar implements IAvatar {
  43. /** @var Folder */
  44. private $folder;
  45. /** @var IL10N */
  46. private $l;
  47. /** @var User */
  48. private $user;
  49. /** @var ILogger */
  50. private $logger;
  51. /**
  52. * constructor
  53. *
  54. * @param Folder $folder The folder where the avatars are
  55. * @param IL10N $l
  56. * @param User $user
  57. * @param ILogger $logger
  58. */
  59. public function __construct (Folder $folder, IL10N $l, $user, ILogger $logger) {
  60. $this->folder = $folder;
  61. $this->l = $l;
  62. $this->user = $user;
  63. $this->logger = $logger;
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function get ($size = 64) {
  69. try {
  70. $file = $this->getFile($size);
  71. } catch (NotFoundException $e) {
  72. return false;
  73. }
  74. $avatar = new OC_Image();
  75. $avatar->loadFromData($file->getContent());
  76. return $avatar;
  77. }
  78. /**
  79. * Check if an avatar exists for the user
  80. *
  81. * @return bool
  82. */
  83. public function exists() {
  84. return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png');
  85. }
  86. /**
  87. * sets the users avatar
  88. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  89. * @throws \Exception if the provided file is not a jpg or png image
  90. * @throws \Exception if the provided image is not valid
  91. * @throws NotSquareException if the image is not square
  92. * @return void
  93. */
  94. public function set ($data) {
  95. if($data instanceOf IImage) {
  96. $img = $data;
  97. $data = $img->data();
  98. } else {
  99. $img = new OC_Image($data);
  100. }
  101. $type = substr($img->mimeType(), -3);
  102. if ($type === 'peg') {
  103. $type = 'jpg';
  104. }
  105. if ($type !== 'jpg' && $type !== 'png') {
  106. throw new \Exception($this->l->t("Unknown filetype"));
  107. }
  108. if (!$img->valid()) {
  109. throw new \Exception($this->l->t("Invalid image"));
  110. }
  111. if (!($img->height() === $img->width())) {
  112. throw new NotSquareException();
  113. }
  114. $this->remove();
  115. $this->folder->newFile('avatar.'.$type)->putContent($data);
  116. $this->user->triggerChange('avatar');
  117. }
  118. /**
  119. * remove the users avatar
  120. * @return void
  121. */
  122. public function remove () {
  123. $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
  124. $avatars = $this->folder->getDirectoryListing();
  125. foreach ($avatars as $avatar) {
  126. if (preg_match($regex, $avatar->getName())) {
  127. $avatar->delete();
  128. }
  129. }
  130. $this->user->triggerChange('avatar');
  131. }
  132. /**
  133. * @inheritdoc
  134. */
  135. public function getFile($size) {
  136. $ext = $this->getExtension();
  137. if ($size === -1) {
  138. $path = 'avatar.' . $ext;
  139. } else {
  140. $path = 'avatar.' . $size . '.' . $ext;
  141. }
  142. try {
  143. $file = $this->folder->get($path);
  144. } catch (NotFoundException $e) {
  145. if ($size <= 0) {
  146. throw new NotFoundException;
  147. }
  148. $avatar = new OC_Image();
  149. /** @var File $file */
  150. $file = $this->folder->get('avatar.' . $ext);
  151. $avatar->loadFromData($file->getContent());
  152. if ($size !== -1) {
  153. $avatar->resize($size);
  154. }
  155. try {
  156. $file = $this->folder->newFile($path);
  157. $file->putContent($avatar->data());
  158. } catch (NotPermittedException $e) {
  159. $this->logger->error('Failed to save avatar for ' . $this->user->getUID());
  160. }
  161. }
  162. return $file;
  163. }
  164. /**
  165. * Get the extension of the avatar. If there is no avatar throw Exception
  166. *
  167. * @return string
  168. * @throws NotFoundException
  169. */
  170. private function getExtension() {
  171. if ($this->folder->nodeExists('avatar.jpg')) {
  172. return 'jpg';
  173. } elseif ($this->folder->nodeExists('avatar.png')) {
  174. return 'png';
  175. }
  176. throw new NotFoundException;
  177. }
  178. }