You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrashRoot.php 2.7KB

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