1
0

LegacyTrashBackend.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime());
  57. return new TrashItem(
  58. $this,
  59. $originalLocation,
  60. $file->getMTime(),
  61. $parentTrashPath . '/' . ($isRoot ? $trashFilename : $file->getName()),
  62. $file,
  63. $user
  64. );
  65. }, $items);
  66. }
  67. public function listTrashRoot(IUser $user): array {
  68. $entries = Helper::getTrashFiles('/', $user->getUID());
  69. return $this->mapTrashItems($entries, $user);
  70. }
  71. public function listTrashFolder(ITrashItem $folder): array {
  72. $user = $folder->getUser();
  73. $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID());
  74. return $this->mapTrashItems($entries, $user ,$folder);
  75. }
  76. public function restoreItem(ITrashItem $item) {
  77. Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null);
  78. }
  79. public function removeItem(ITrashItem $item) {
  80. $user = $item->getUser();
  81. if ($item->isRootItem()) {
  82. $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime()));
  83. Trashbin::delete($path, $user->getUID(), $item->getDeletedTime());
  84. } else {
  85. Trashbin::delete($item->getTrashPath(), $user->getUID(), null);
  86. }
  87. }
  88. public function moveToTrash(IStorage $storage, string $internalPath): bool {
  89. if (!$storage instanceof Storage) {
  90. return false;
  91. }
  92. $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true);
  93. $view = Filesystem::getView();
  94. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  95. $this->deletedFiles[$normalized] = $normalized;
  96. if ($filesPath = $view->getRelativePath($normalized)) {
  97. $filesPath = trim($filesPath, '/');
  98. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
  99. } else {
  100. $result = false;
  101. }
  102. unset($this->deletedFiles[$normalized]);
  103. } else {
  104. $result = false;
  105. }
  106. return $result;
  107. }
  108. public function getTrashNodeById(IUser $user, int $fileId) {
  109. try {
  110. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  111. $trash = $userFolder->getParent()->get('files_trashbin/files');
  112. $trashFiles = $trash->getById($fileId);
  113. if (!$trashFiles) {
  114. return null;
  115. }
  116. return $trashFiles ? array_pop($trashFiles) : null;
  117. } catch (NotFoundException $e) {
  118. return null;
  119. }
  120. }
  121. }