TrashRoot.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Trashbin\Sabre;
  25. use OCA\Files_Trashbin\Trash\ITrashItem;
  26. use OCA\Files_Trashbin\Trash\ITrashManager;
  27. use OCP\Files\FileInfo;
  28. use OCP\IUser;
  29. use Sabre\DAV\Exception\Forbidden;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\ICollection;
  32. class TrashRoot implements ICollection {
  33. /** @var IUser */
  34. private $user;
  35. /** @var ITrashManager */
  36. private $trashManager;
  37. public function __construct(IUser $user, ITrashManager $trashManager) {
  38. $this->user = $user;
  39. $this->trashManager = $trashManager;
  40. }
  41. public function delete() {
  42. \OCA\Files_Trashbin\Trashbin::deleteAll();
  43. }
  44. public function getName(): string {
  45. return 'trash';
  46. }
  47. public function setName($name) {
  48. throw new Forbidden('Permission denied to rename this trashbin');
  49. }
  50. public function createFile($name, $data = null) {
  51. throw new Forbidden('Not allowed to create files in the trashbin');
  52. }
  53. public function createDirectory($name) {
  54. throw new Forbidden('Not allowed to create folders in the trashbin');
  55. }
  56. public function getChildren(): array {
  57. $entries = $this->trashManager->listTrashRoot($this->user);
  58. $children = array_map(function (ITrashItem $entry) {
  59. if ($entry->getType() === FileInfo::TYPE_FOLDER) {
  60. return new TrashFolder($this->trashManager, $entry);
  61. }
  62. return new TrashFile($this->trashManager, $entry);
  63. }, $entries);
  64. return $children;
  65. }
  66. public function getChild($name): ITrash {
  67. $entries = $this->getChildren();
  68. foreach ($entries as $entry) {
  69. if ($entry->getName() === $name) {
  70. return $entry;
  71. }
  72. }
  73. throw new NotFound();
  74. }
  75. public function childExists($name): bool {
  76. try {
  77. $this->getChild($name);
  78. return true;
  79. } catch (NotFound $e) {
  80. return false;
  81. }
  82. }
  83. public function getLastModified(): int {
  84. return 0;
  85. }
  86. }