LegacyTrashBackend.php 4.2 KB

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