AvatarHome.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Avatars;
  26. use OCP\IAvatarManager;
  27. use Sabre\DAV\Exception\Forbidden;
  28. use Sabre\DAV\Exception\MethodNotAllowed;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\ICollection;
  31. use Sabre\Uri;
  32. class AvatarHome implements ICollection {
  33. /** @var array */
  34. private $principalInfo;
  35. /** @var IAvatarManager */
  36. private $avatarManager;
  37. /**
  38. * AvatarHome constructor.
  39. *
  40. * @param array $principalInfo
  41. * @param IAvatarManager $avatarManager
  42. */
  43. public function __construct($principalInfo, IAvatarManager $avatarManager) {
  44. $this->principalInfo = $principalInfo;
  45. $this->avatarManager = $avatarManager;
  46. }
  47. public function createFile($name, $data = null) {
  48. throw new Forbidden('Permission denied to create a file');
  49. }
  50. public function createDirectory($name) {
  51. throw new Forbidden('Permission denied to create a folder');
  52. }
  53. public function getChild($name) {
  54. $elements = pathinfo($name);
  55. $ext = $elements['extension'] ?? '';
  56. $size = (int)($elements['filename'] ?? '64');
  57. if (!in_array($ext, ['jpeg', 'png'], true)) {
  58. throw new MethodNotAllowed('File format not allowed');
  59. }
  60. if ($size <= 0 || $size > 1024) {
  61. throw new MethodNotAllowed('Invalid image size');
  62. }
  63. $avatar = $this->avatarManager->getAvatar($this->getName());
  64. if (!$avatar->exists()) {
  65. throw new NotFound();
  66. }
  67. return new AvatarNode($size, $ext, $avatar);
  68. }
  69. public function getChildren() {
  70. try {
  71. return [
  72. $this->getChild('96.jpeg')
  73. ];
  74. } catch (NotFound $exception) {
  75. return [];
  76. }
  77. }
  78. public function childExists($name) {
  79. try {
  80. $ret = $this->getChild($name);
  81. return $ret !== null;
  82. } catch (NotFound $ex) {
  83. return false;
  84. } catch (MethodNotAllowed $ex) {
  85. return false;
  86. }
  87. }
  88. public function delete() {
  89. throw new Forbidden('Permission denied to delete this folder');
  90. }
  91. public function getName() {
  92. [,$name] = Uri\split($this->principalInfo['uri']);
  93. return $name;
  94. }
  95. public function setName($name) {
  96. throw new Forbidden('Permission denied to rename this folder');
  97. }
  98. /**
  99. * Returns the last modification time, as a unix timestamp
  100. *
  101. * @return int|null
  102. */
  103. public function getLastModified() {
  104. return null;
  105. }
  106. }