AvatarNode.php 1.5 KB

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