1
0

AvatarNode.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Avatars;
  24. use OCP\IAvatar;
  25. use Sabre\DAV\File;
  26. class AvatarNode extends File {
  27. private $ext;
  28. private $size;
  29. private $avatar;
  30. /**
  31. * AvatarNode constructor.
  32. *
  33. * @param integer $size
  34. * @param string $ext
  35. * @param IAvatar $avatar
  36. */
  37. public function __construct($size, $ext, $avatar) {
  38. $this->size = $size;
  39. $this->ext = $ext;
  40. $this->avatar = $avatar;
  41. }
  42. /**
  43. * Returns the name of the node.
  44. *
  45. * This is used to generate the url.
  46. *
  47. * @return string
  48. */
  49. public function getName() {
  50. return "$this->size.$this->ext";
  51. }
  52. public function get() {
  53. $image = $this->avatar->get($this->size);
  54. $res = $image->resource();
  55. ob_start();
  56. if ($this->ext === 'png') {
  57. imagepng($res);
  58. } else {
  59. imagejpeg($res);
  60. }
  61. return ob_get_clean();
  62. }
  63. /**
  64. * Returns the mime-type for a file
  65. *
  66. * If null is returned, we'll assume application/octet-stream
  67. *
  68. * @return string|null
  69. */
  70. public function getContentType() {
  71. if ($this->ext === 'png') {
  72. return 'image/png';
  73. }
  74. return 'image/jpeg';
  75. }
  76. public function getETag() {
  77. return $this->avatar->getFile($this->size)->getEtag();
  78. }
  79. public function getLastModified() {
  80. $timestamp = $this->avatar->getFile($this->size)->getMTime();
  81. if (!empty($timestamp)) {
  82. return (int)$timestamp;
  83. }
  84. return $timestamp;
  85. }
  86. }