TrashRoot.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Trashbin\Sabre;
  8. use OCA\Files_Trashbin\Trash\ITrashItem;
  9. use OCA\Files_Trashbin\Trash\ITrashManager;
  10. use OCA\Files_Trashbin\Trashbin;
  11. use OCP\Files\FileInfo;
  12. use OCP\IUser;
  13. use Sabre\DAV\Exception\Forbidden;
  14. use Sabre\DAV\Exception\NotFound;
  15. use Sabre\DAV\ICollection;
  16. class TrashRoot implements ICollection {
  17. public function __construct(
  18. private IUser $user,
  19. private ITrashManager $trashManager,
  20. ) {
  21. }
  22. public function delete() {
  23. Trashbin::deleteAll();
  24. foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) {
  25. $this->trashManager->removeItem($trashItem);
  26. }
  27. }
  28. public function getName(): string {
  29. return 'trash';
  30. }
  31. public function setName($name) {
  32. throw new Forbidden('Permission denied to rename this trashbin');
  33. }
  34. public function createFile($name, $data = null) {
  35. throw new Forbidden('Not allowed to create files in the trashbin');
  36. }
  37. public function createDirectory($name) {
  38. throw new Forbidden('Not allowed to create folders in the trashbin');
  39. }
  40. public function getChildren(): array {
  41. $entries = $this->trashManager->listTrashRoot($this->user);
  42. $children = array_map(function (ITrashItem $entry) {
  43. if ($entry->getType() === FileInfo::TYPE_FOLDER) {
  44. return new TrashFolder($this->trashManager, $entry);
  45. }
  46. return new TrashFile($this->trashManager, $entry);
  47. }, $entries);
  48. return $children;
  49. }
  50. public function getChild($name): ITrash {
  51. $entries = $this->getChildren();
  52. foreach ($entries as $entry) {
  53. if ($entry->getName() === $name) {
  54. return $entry;
  55. }
  56. }
  57. throw new NotFound();
  58. }
  59. public function childExists($name): bool {
  60. try {
  61. $this->getChild($name);
  62. return true;
  63. } catch (NotFound $e) {
  64. return false;
  65. }
  66. }
  67. public function getLastModified(): int {
  68. return 0;
  69. }
  70. }