1
0

TrashRoot.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\Sabre;
  27. use OCA\Files_Trashbin\Trash\ITrashItem;
  28. use OCA\Files_Trashbin\Trash\ITrashManager;
  29. use OCP\Files\FileInfo;
  30. use OCP\IUser;
  31. use Sabre\DAV\Exception\Forbidden;
  32. use Sabre\DAV\Exception\NotFound;
  33. use Sabre\DAV\ICollection;
  34. class TrashRoot implements ICollection {
  35. /** @var IUser */
  36. private $user;
  37. /** @var ITrashManager */
  38. private $trashManager;
  39. public function __construct(IUser $user, ITrashManager $trashManager) {
  40. $this->user = $user;
  41. $this->trashManager = $trashManager;
  42. }
  43. public function delete() {
  44. \OCA\Files_Trashbin\Trashbin::deleteAll();
  45. foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) {
  46. $this->trashManager->removeItem($trashItem);
  47. }
  48. }
  49. public function getName(): string {
  50. return 'trash';
  51. }
  52. public function setName($name) {
  53. throw new Forbidden('Permission denied to rename this trashbin');
  54. }
  55. public function createFile($name, $data = null) {
  56. throw new Forbidden('Not allowed to create files in the trashbin');
  57. }
  58. public function createDirectory($name) {
  59. throw new Forbidden('Not allowed to create folders in the trashbin');
  60. }
  61. public function getChildren(): array {
  62. $entries = $this->trashManager->listTrashRoot($this->user);
  63. $children = array_map(function (ITrashItem $entry) {
  64. if ($entry->getType() === FileInfo::TYPE_FOLDER) {
  65. return new TrashFolder($this->trashManager, $entry);
  66. }
  67. return new TrashFile($this->trashManager, $entry);
  68. }, $entries);
  69. return $children;
  70. }
  71. public function getChild($name): ITrash {
  72. $entries = $this->getChildren();
  73. foreach ($entries as $entry) {
  74. if ($entry->getName() === $name) {
  75. return $entry;
  76. }
  77. }
  78. throw new NotFound();
  79. }
  80. public function childExists($name): bool {
  81. try {
  82. $this->getChild($name);
  83. return true;
  84. } catch (NotFound $e) {
  85. return false;
  86. }
  87. }
  88. public function getLastModified(): int {
  89. return 0;
  90. }
  91. }