VersionCollection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Versions\Sabre;
  26. use OCA\Files_Versions\Versions\IVersion;
  27. use OCA\Files_Versions\Versions\IVersionManager;
  28. use OCP\Files\File;
  29. use OCP\Files\Folder;
  30. use OCP\IUser;
  31. use Sabre\DAV\Exception\Forbidden;
  32. use Sabre\DAV\Exception\NotFound;
  33. use Sabre\DAV\ICollection;
  34. class VersionCollection implements ICollection {
  35. /** @var Folder */
  36. private $userFolder;
  37. /** @var File */
  38. private $file;
  39. /** @var IUser */
  40. private $user;
  41. /** @var IVersionManager */
  42. private $versionManager;
  43. public function __construct(Folder $userFolder, File $file, IUser $user, IVersionManager $versionManager) {
  44. $this->userFolder = $userFolder;
  45. $this->file = $file;
  46. $this->user = $user;
  47. $this->versionManager = $versionManager;
  48. }
  49. public function createFile($name, $data = null) {
  50. throw new Forbidden();
  51. }
  52. public function createDirectory($name) {
  53. throw new Forbidden();
  54. }
  55. public function getChild($name) {
  56. /** @var VersionFile[] $versions */
  57. $versions = $this->getChildren();
  58. foreach ($versions as $version) {
  59. if ($version->getName() === $name) {
  60. return $version;
  61. }
  62. }
  63. throw new NotFound();
  64. }
  65. public function getChildren(): array {
  66. $versions = $this->versionManager->getVersionsForFile($this->user, $this->file);
  67. return array_map(function (IVersion $version) {
  68. return new VersionFile($version, $this->versionManager);
  69. }, $versions);
  70. }
  71. public function childExists($name): bool {
  72. try {
  73. $this->getChild($name);
  74. return true;
  75. } catch (NotFound $e) {
  76. return false;
  77. }
  78. }
  79. public function delete() {
  80. throw new Forbidden();
  81. }
  82. public function getName(): string {
  83. return (string)$this->file->getId();
  84. }
  85. public function setName($name) {
  86. throw new Forbidden();
  87. }
  88. public function getLastModified(): int {
  89. return 0;
  90. }
  91. }