Avatar.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\IConfig;
  36. use OCP\IImage;
  37. use OCP\IL10N;
  38. use OC_Image;
  39. use OCP\ILogger;
  40. /**
  41. * This class gets and sets users avatars.
  42. */
  43. class Avatar implements IAvatar {
  44. /** @var Folder */
  45. private $folder;
  46. /** @var IL10N */
  47. private $l;
  48. /** @var User */
  49. private $user;
  50. /** @var ILogger */
  51. private $logger;
  52. /** @var IConfig */
  53. private $config;
  54. /**
  55. * constructor
  56. *
  57. * @param Folder $folder The folder where the avatars are
  58. * @param IL10N $l
  59. * @param User $user
  60. * @param ILogger $logger
  61. * @param IConfig $config
  62. */
  63. public function __construct(Folder $folder,
  64. IL10N $l,
  65. $user,
  66. ILogger $logger,
  67. IConfig $config) {
  68. $this->folder = $folder;
  69. $this->l = $l;
  70. $this->user = $user;
  71. $this->logger = $logger;
  72. $this->config = $config;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function get ($size = 64) {
  78. try {
  79. $file = $this->getFile($size);
  80. } catch (NotFoundException $e) {
  81. return false;
  82. }
  83. $avatar = new OC_Image();
  84. $avatar->loadFromData($file->getContent());
  85. return $avatar;
  86. }
  87. /**
  88. * Check if an avatar exists for the user
  89. *
  90. * @return bool
  91. */
  92. public function exists() {
  93. return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png');
  94. }
  95. /**
  96. * sets the users avatar
  97. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  98. * @throws \Exception if the provided file is not a jpg or png image
  99. * @throws \Exception if the provided image is not valid
  100. * @throws NotSquareException if the image is not square
  101. * @return void
  102. */
  103. public function set ($data) {
  104. if($data instanceOf IImage) {
  105. $img = $data;
  106. $data = $img->data();
  107. } else {
  108. $img = new OC_Image($data);
  109. }
  110. $type = substr($img->mimeType(), -3);
  111. if ($type === 'peg') {
  112. $type = 'jpg';
  113. }
  114. if ($type !== 'jpg' && $type !== 'png') {
  115. throw new \Exception($this->l->t("Unknown filetype"));
  116. }
  117. if (!$img->valid()) {
  118. throw new \Exception($this->l->t("Invalid image"));
  119. }
  120. if (!($img->height() === $img->width())) {
  121. throw new NotSquareException();
  122. }
  123. $this->remove();
  124. $this->folder->newFile('avatar.'.$type)->putContent($data);
  125. $this->user->triggerChange('avatar');
  126. }
  127. /**
  128. * remove the users avatar
  129. * @return void
  130. */
  131. public function remove () {
  132. $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
  133. $avatars = $this->folder->getDirectoryListing();
  134. $this->config->setUserValue($this->user->getUID(), 'avatar', 'version',
  135. (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1);
  136. foreach ($avatars as $avatar) {
  137. if (preg_match($regex, $avatar->getName())) {
  138. $avatar->delete();
  139. }
  140. }
  141. $this->user->triggerChange('avatar');
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function getFile($size) {
  147. $ext = $this->getExtension();
  148. if ($size === -1) {
  149. $path = 'avatar.' . $ext;
  150. } else {
  151. $path = 'avatar.' . $size . '.' . $ext;
  152. }
  153. try {
  154. $file = $this->folder->get($path);
  155. } catch (NotFoundException $e) {
  156. if ($size <= 0) {
  157. throw new NotFoundException;
  158. }
  159. $avatar = new OC_Image();
  160. /** @var File $file */
  161. $file = $this->folder->get('avatar.' . $ext);
  162. $avatar->loadFromData($file->getContent());
  163. if ($size !== -1) {
  164. $avatar->resize($size);
  165. }
  166. try {
  167. $file = $this->folder->newFile($path);
  168. $file->putContent($avatar->data());
  169. } catch (NotPermittedException $e) {
  170. $this->logger->error('Failed to save avatar for ' . $this->user->getUID());
  171. }
  172. }
  173. return $file;
  174. }
  175. /**
  176. * Get the extension of the avatar. If there is no avatar throw Exception
  177. *
  178. * @return string
  179. * @throws NotFoundException
  180. */
  181. private function getExtension() {
  182. if ($this->folder->nodeExists('avatar.jpg')) {
  183. return 'jpg';
  184. } elseif ($this->folder->nodeExists('avatar.png')) {
  185. return 'png';
  186. }
  187. throw new NotFoundException;
  188. }
  189. }