TrashFolder.php 3.6 KB

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