TrashFolderFolder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 TrashFolderFolder implements ICollection, ITrash {
  30. /** @var string */
  31. private $root;
  32. /** @var string */
  33. private $userId;
  34. /** @var FileInfo */
  35. private $data;
  36. /** @var string */
  37. private $location;
  38. public function __construct(string $root,
  39. string $userId,
  40. FileInfo $data,
  41. string $location) {
  42. $this->root = $root;
  43. $this->userId = $userId;
  44. $this->data = $data;
  45. $this->location = $location;
  46. }
  47. public function createFile($name, $data = null) {
  48. throw new Forbidden();
  49. }
  50. public function createDirectory($name) {
  51. throw new Forbidden();
  52. }
  53. public function getChild($name): ITrash {
  54. $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
  55. foreach ($entries as $entry) {
  56. if ($entry->getName() === $name) {
  57. if ($entry->getType() === FileInfo::TYPE_FOLDER) {
  58. return new TrashFolderFolder($this->root . '/' . $this->getName(), $this->userId, $entry, $this->getOriginalLocation());
  59. }
  60. return new TrashFolderFile($this->root . '/' . $this->getName(), $this->userId, $entry, $this->getOriginalLocation());
  61. }
  62. }
  63. throw new NotFound();
  64. }
  65. public function getChildren(): array {
  66. $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
  67. $children = array_map(function (FileInfo $entry) {
  68. if ($entry->getType() === FileInfo::TYPE_FOLDER) {
  69. return new TrashFolderFolder($this->root.'/'.$this->getName(), $this->userId, $entry, $this->getOriginalLocation());
  70. }
  71. return new TrashFolderFile($this->root.'/'.$this->getName(), $this->userId, $entry, $this->getOriginalLocation());
  72. }, $entries);
  73. return $children;
  74. }
  75. public function childExists($name): bool {
  76. $entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $this->getName(), $this->userId);
  77. foreach ($entries as $entry) {
  78. if ($entry->getName() === $name) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. public function delete() {
  85. \OCA\Files_Trashbin\Trashbin::delete($this->root . '/' . $this->getName(), $this->userId, null);
  86. }
  87. public function getName(): string {
  88. return $this->data->getName();
  89. }
  90. public function setName($name) {
  91. throw new Forbidden();
  92. }
  93. public function getLastModified(): int {
  94. return $this->data->getMtime();
  95. }
  96. public function restore(): bool {
  97. return \OCA\Files_Trashbin\Trashbin::restore($this->root . '/' . $this->getName(), $this->data->getName(), null);
  98. }
  99. public function getFilename(): string {
  100. return $this->data->getName();
  101. }
  102. public function getOriginalLocation(): string {
  103. return $this->location . '/' . $this->getFilename();
  104. }
  105. public function getDeletionTime(): int {
  106. return $this->getLastModified();
  107. }
  108. public function getSize(): int {
  109. return $this->data->getSize();
  110. }
  111. public function getFileId(): int {
  112. return $this->data->getId();
  113. }
  114. }