LegacyTrashBackend.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Trashbin\Trash;
  7. use OC\Files\Filesystem;
  8. use OC\Files\View;
  9. use OCA\Files_Trashbin\Helper;
  10. use OCA\Files_Trashbin\Storage;
  11. use OCA\Files_Trashbin\Trashbin;
  12. use OCP\Files\FileInfo;
  13. use OCP\Files\Folder;
  14. use OCP\Files\IRootFolder;
  15. use OCP\Files\NotFoundException;
  16. use OCP\Files\Storage\IStorage;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. class LegacyTrashBackend implements ITrashBackend {
  20. /** @var array */
  21. private $deletedFiles = [];
  22. public function __construct(
  23. private IRootFolder $rootFolder,
  24. private IUserManager $userManager,
  25. ) {
  26. }
  27. /**
  28. * @param array $items
  29. * @param IUser $user
  30. * @param ITrashItem $parent
  31. * @return ITrashItem[]
  32. */
  33. private function mapTrashItems(array $items, IUser $user, ?ITrashItem $parent = null): array {
  34. $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : '';
  35. $isRoot = $parent === null;
  36. return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) {
  37. $originalLocation = $isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName();
  38. if (!$originalLocation) {
  39. $originalLocation = $file->getName();
  40. }
  41. /** @psalm-suppress UndefinedInterfaceMethod */
  42. $deletedBy = $this->userManager->get($file['deletedBy']) ?? $parent?->getDeletedBy();
  43. $trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime());
  44. return new TrashItem(
  45. $this,
  46. $originalLocation,
  47. $file->getMTime(),
  48. $parentTrashPath . '/' . ($isRoot ? $trashFilename : $file->getName()),
  49. $file,
  50. $user,
  51. $deletedBy,
  52. );
  53. }, $items);
  54. }
  55. public function listTrashRoot(IUser $user): array {
  56. $entries = Helper::getTrashFiles('/', $user->getUID());
  57. return $this->mapTrashItems($entries, $user);
  58. }
  59. public function listTrashFolder(ITrashItem $folder): array {
  60. $user = $folder->getUser();
  61. $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID());
  62. return $this->mapTrashItems($entries, $user, $folder);
  63. }
  64. public function restoreItem(ITrashItem $item) {
  65. Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null);
  66. }
  67. public function removeItem(ITrashItem $item) {
  68. $user = $item->getUser();
  69. if ($item->isRootItem()) {
  70. $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime()));
  71. Trashbin::delete($path, $user->getUID(), $item->getDeletedTime());
  72. } else {
  73. Trashbin::delete($item->getTrashPath(), $user->getUID(), null);
  74. }
  75. }
  76. public function moveToTrash(IStorage $storage, string $internalPath): bool {
  77. if (!$storage instanceof Storage) {
  78. return false;
  79. }
  80. $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true);
  81. $view = Filesystem::getView();
  82. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  83. $this->deletedFiles[$normalized] = $normalized;
  84. if ($filesPath = $view->getRelativePath($normalized)) {
  85. $filesPath = trim($filesPath, '/');
  86. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
  87. } else {
  88. $result = false;
  89. }
  90. unset($this->deletedFiles[$normalized]);
  91. } else {
  92. $result = false;
  93. }
  94. return $result;
  95. }
  96. public function getTrashNodeById(IUser $user, int $fileId) {
  97. try {
  98. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  99. $trash = $userFolder->getParent()->get('files_trashbin/files');
  100. if ($trash instanceof Folder) {
  101. return $trash->getFirstNodeById($fileId);
  102. } else {
  103. return null;
  104. }
  105. } catch (NotFoundException $e) {
  106. return null;
  107. }
  108. }
  109. }