1
0

AvatarHome.php 3.0 KB

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