LegacyTrashBackend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_Trashbin\Trash;
  24. use OC\Files\Filesystem;
  25. use OC\Files\View;
  26. use OCA\Files_Trashbin\Helper;
  27. use OCA\Files_Trashbin\Storage;
  28. use OCA\Files_Trashbin\Trashbin;
  29. use OCP\Files\FileInfo;
  30. use OCP\Files\Folder;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\Storage\IStorage;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. class LegacyTrashBackend implements ITrashBackend {
  37. /** @var array */
  38. private $deletedFiles = [];
  39. public function __construct(
  40. private IRootFolder $rootFolder,
  41. private IUserManager $userManager,
  42. ) {
  43. }
  44. /**
  45. * @param array $items
  46. * @param IUser $user
  47. * @param ITrashItem $parent
  48. * @return ITrashItem[]
  49. */
  50. private function mapTrashItems(array $items, IUser $user, ?ITrashItem $parent = null): array {
  51. $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : '';
  52. $isRoot = $parent === null;
  53. return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) {
  54. $originalLocation = $isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName();
  55. if (!$originalLocation) {
  56. $originalLocation = $file->getName();
  57. }
  58. /** @psalm-suppress UndefinedInterfaceMethod */
  59. $deletedBy = $this->userManager->get($file['deletedBy']) ?? $parent?->getDeletedBy();
  60. $trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime());
  61. return new TrashItem(
  62. $this,
  63. $originalLocation,
  64. $file->getMTime(),
  65. $parentTrashPath . '/' . ($isRoot ? $trashFilename : $file->getName()),
  66. $file,
  67. $user,
  68. $deletedBy,
  69. );
  70. }, $items);
  71. }
  72. public function listTrashRoot(IUser $user): array {
  73. $entries = Helper::getTrashFiles('/', $user->getUID());
  74. return $this->mapTrashItems($entries, $user);
  75. }
  76. public function listTrashFolder(ITrashItem $folder): array {
  77. $user = $folder->getUser();
  78. $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID());
  79. return $this->mapTrashItems($entries, $user, $folder);
  80. }
  81. public function restoreItem(ITrashItem $item) {
  82. Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null);
  83. }
  84. public function removeItem(ITrashItem $item) {
  85. $user = $item->getUser();
  86. if ($item->isRootItem()) {
  87. $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime()));
  88. Trashbin::delete($path, $user->getUID(), $item->getDeletedTime());
  89. } else {
  90. Trashbin::delete($item->getTrashPath(), $user->getUID(), null);
  91. }
  92. }
  93. public function moveToTrash(IStorage $storage, string $internalPath): bool {
  94. if (!$storage instanceof Storage) {
  95. return false;
  96. }
  97. $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true);
  98. $view = Filesystem::getView();
  99. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  100. $this->deletedFiles[$normalized] = $normalized;
  101. if ($filesPath = $view->getRelativePath($normalized)) {
  102. $filesPath = trim($filesPath, '/');
  103. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
  104. } else {
  105. $result = false;
  106. }
  107. unset($this->deletedFiles[$normalized]);
  108. } else {
  109. $result = false;
  110. }
  111. return $result;
  112. }
  113. public function getTrashNodeById(IUser $user, int $fileId) {
  114. try {
  115. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  116. $trash = $userFolder->getParent()->get('files_trashbin/files');
  117. if ($trash instanceof Folder) {
  118. return $trash->getFirstNodeById($fileId);
  119. } else {
  120. return null;
  121. }
  122. } catch (NotFoundException $e) {
  123. return null;
  124. }
  125. }
  126. }