AvatarNode.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\IAvatar;
  9. use Sabre\DAV\File;
  10. class AvatarNode extends File {
  11. /**
  12. * AvatarNode constructor.
  13. *
  14. * @param integer $size
  15. * @param string $ext
  16. * @param IAvatar $avatar
  17. */
  18. public function __construct(
  19. private $size,
  20. private $ext,
  21. private $avatar,
  22. ) {
  23. }
  24. /**
  25. * Returns the name of the node.
  26. *
  27. * This is used to generate the url.
  28. *
  29. * @return string
  30. */
  31. public function getName() {
  32. return "$this->size.$this->ext";
  33. }
  34. public function get() {
  35. $image = $this->avatar->get($this->size);
  36. $res = $image->resource();
  37. ob_start();
  38. if ($this->ext === 'png') {
  39. imagepng($res);
  40. } else {
  41. imagejpeg($res);
  42. }
  43. return ob_get_clean();
  44. }
  45. /**
  46. * Returns the mime-type for a file
  47. *
  48. * If null is returned, we'll assume application/octet-stream
  49. *
  50. * @return string|null
  51. */
  52. public function getContentType() {
  53. if ($this->ext === 'png') {
  54. return 'image/png';
  55. }
  56. return 'image/jpeg';
  57. }
  58. public function getETag() {
  59. return $this->avatar->getFile($this->size)->getEtag();
  60. }
  61. public function getLastModified() {
  62. return $this->avatar->getFile($this->size)->getMTime();
  63. }
  64. }