TrashRoot.php 2.7 KB

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