You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyTrashBackend.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. class LegacyTrashBackend implements ITrashBackend {
  36. /** @var array */
  37. private $deletedFiles = [];
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. public function __construct(IRootFolder $rootFolder) {
  41. $this->rootFolder = $rootFolder;
  42. }
  43. /**
  44. * @param array $items
  45. * @param IUser $user
  46. * @param ITrashItem $parent
  47. * @return ITrashItem[]
  48. */
  49. private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array {
  50. $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : '';
  51. $isRoot = $parent === null;
  52. return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) {
  53. $originalLocation = $isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName();
  54. if (!$originalLocation) {
  55. $originalLocation = $file->getName();
  56. }
  57. $trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime());
  58. return new TrashItem(
  59. $this,
  60. $originalLocation,
  61. $file->getMTime(),
  62. $parentTrashPath . '/' . ($isRoot ? $trashFilename : $file->getName()),
  63. $file,
  64. $user
  65. );
  66. }, $items);
  67. }
  68. public function listTrashRoot(IUser $user): array {
  69. $entries = Helper::getTrashFiles('/', $user->getUID());
  70. return $this->mapTrashItems($entries, $user);
  71. }
  72. public function listTrashFolder(ITrashItem $folder): array {
  73. $user = $folder->getUser();
  74. $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID());
  75. return $this->mapTrashItems($entries, $user, $folder);
  76. }
  77. public function restoreItem(ITrashItem $item) {
  78. Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null);
  79. }
  80. public function removeItem(ITrashItem $item) {
  81. $user = $item->getUser();
  82. if ($item->isRootItem()) {
  83. $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime()));
  84. Trashbin::delete($path, $user->getUID(), $item->getDeletedTime());
  85. } else {
  86. Trashbin::delete($item->getTrashPath(), $user->getUID(), null);
  87. }
  88. }
  89. public function moveToTrash(IStorage $storage, string $internalPath): bool {
  90. if (!$storage instanceof Storage) {
  91. return false;
  92. }
  93. $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true);
  94. $view = Filesystem::getView();
  95. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  96. $this->deletedFiles[$normalized] = $normalized;
  97. if ($filesPath = $view->getRelativePath($normalized)) {
  98. $filesPath = trim($filesPath, '/');
  99. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
  100. } else {
  101. $result = false;
  102. }
  103. unset($this->deletedFiles[$normalized]);
  104. } else {
  105. $result = false;
  106. }
  107. return $result;
  108. }
  109. public function getTrashNodeById(IUser $user, int $fileId) {
  110. try {
  111. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  112. $trash = $userFolder->getParent()->get('files_trashbin/files');
  113. if ($trash instanceof Folder) {
  114. return $trash->getFirstNodeById($fileId);
  115. } else {
  116. return null;
  117. }
  118. } catch (NotFoundException $e) {
  119. return null;
  120. }
  121. }
  122. }