1
0

TrashRoot.php 2.0 KB

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