AvatarHome.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Avatars;
  8. use OCP\IAvatarManager;
  9. use Sabre\DAV\Exception\Forbidden;
  10. use Sabre\DAV\Exception\MethodNotAllowed;
  11. use Sabre\DAV\Exception\NotFound;
  12. use Sabre\DAV\ICollection;
  13. use Sabre\Uri;
  14. class AvatarHome implements ICollection {
  15. /** @var array */
  16. private $principalInfo;
  17. /** @var IAvatarManager */
  18. private $avatarManager;
  19. /**
  20. * AvatarHome constructor.
  21. *
  22. * @param array $principalInfo
  23. * @param IAvatarManager $avatarManager
  24. */
  25. public function __construct($principalInfo, IAvatarManager $avatarManager) {
  26. $this->principalInfo = $principalInfo;
  27. $this->avatarManager = $avatarManager;
  28. }
  29. public function createFile($name, $data = null) {
  30. throw new Forbidden('Permission denied to create a file');
  31. }
  32. public function createDirectory($name) {
  33. throw new Forbidden('Permission denied to create a folder');
  34. }
  35. public function getChild($name) {
  36. $elements = pathinfo($name);
  37. $ext = $elements['extension'] ?? '';
  38. $size = (int)($elements['filename'] ?? '64');
  39. if (!in_array($ext, ['jpeg', 'png'], true)) {
  40. throw new MethodNotAllowed('File format not allowed');
  41. }
  42. if ($size <= 0 || $size > 1024) {
  43. throw new MethodNotAllowed('Invalid image size');
  44. }
  45. $avatar = $this->avatarManager->getAvatar($this->getName());
  46. if (!$avatar->exists()) {
  47. throw new NotFound();
  48. }
  49. return new AvatarNode($size, $ext, $avatar);
  50. }
  51. public function getChildren() {
  52. try {
  53. return [
  54. $this->getChild('96.jpeg')
  55. ];
  56. } catch (NotFound $exception) {
  57. return [];
  58. }
  59. }
  60. public function childExists($name) {
  61. try {
  62. $ret = $this->getChild($name);
  63. return $ret !== null;
  64. } catch (NotFound $ex) {
  65. return false;
  66. } catch (MethodNotAllowed $ex) {
  67. return false;
  68. }
  69. }
  70. public function delete() {
  71. throw new Forbidden('Permission denied to delete this folder');
  72. }
  73. public function getName() {
  74. [,$name] = Uri\split($this->principalInfo['uri']);
  75. return $name;
  76. }
  77. public function setName($name) {
  78. throw new Forbidden('Permission denied to rename this folder');
  79. }
  80. /**
  81. * Returns the last modification time, as a unix timestamp
  82. *
  83. * @return int|null
  84. */
  85. public function getLastModified() {
  86. return null;
  87. }
  88. }