AvatarHome.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  16. * AvatarHome constructor.
  17. *
  18. * @param array $principalInfo
  19. * @param IAvatarManager $avatarManager
  20. */
  21. public function __construct(
  22. private $principalInfo,
  23. private IAvatarManager $avatarManager,
  24. ) {
  25. }
  26. public function createFile($name, $data = null) {
  27. throw new Forbidden('Permission denied to create a file');
  28. }
  29. public function createDirectory($name) {
  30. throw new Forbidden('Permission denied to create a folder');
  31. }
  32. public function getChild($name) {
  33. $elements = pathinfo($name);
  34. $ext = $elements['extension'] ?? '';
  35. $size = (int)($elements['filename'] ?? '64');
  36. if (!in_array($ext, ['jpeg', 'png'], true)) {
  37. throw new MethodNotAllowed('File format not allowed');
  38. }
  39. if ($size <= 0 || $size > 1024) {
  40. throw new MethodNotAllowed('Invalid image size');
  41. }
  42. $avatar = $this->avatarManager->getAvatar($this->getName());
  43. if (!$avatar->exists()) {
  44. throw new NotFound();
  45. }
  46. return new AvatarNode($size, $ext, $avatar);
  47. }
  48. public function getChildren() {
  49. try {
  50. return [
  51. $this->getChild('96.jpeg')
  52. ];
  53. } catch (NotFound $exception) {
  54. return [];
  55. }
  56. }
  57. public function childExists($name) {
  58. try {
  59. $ret = $this->getChild($name);
  60. return $ret !== null;
  61. } catch (NotFound $ex) {
  62. return false;
  63. } catch (MethodNotAllowed $ex) {
  64. return false;
  65. }
  66. }
  67. public function delete() {
  68. throw new Forbidden('Permission denied to delete this folder');
  69. }
  70. public function getName() {
  71. [,$name] = Uri\split($this->principalInfo['uri']);
  72. return $name;
  73. }
  74. public function setName($name) {
  75. throw new Forbidden('Permission denied to rename this folder');
  76. }
  77. /**
  78. * Returns the last modification time, as a unix timestamp
  79. *
  80. * @return int|null
  81. */
  82. public function getLastModified() {
  83. return null;
  84. }
  85. }