1
0

TrashManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 OCP\Files\Storage\IStorage;
  8. use OCP\IUser;
  9. class TrashManager implements ITrashManager {
  10. /** @var ITrashBackend[] */
  11. private $backends = [];
  12. private $trashPaused = false;
  13. public function registerBackend(string $storageType, ITrashBackend $backend) {
  14. $this->backends[$storageType] = $backend;
  15. }
  16. /**
  17. * @return ITrashBackend[]
  18. */
  19. private function getBackends(): array {
  20. return $this->backends;
  21. }
  22. public function listTrashRoot(IUser $user): array {
  23. $items = array_reduce($this->getBackends(), function (array $items, ITrashBackend $backend) use ($user) {
  24. return array_merge($items, $backend->listTrashRoot($user));
  25. }, []);
  26. usort($items, function (ITrashItem $a, ITrashItem $b) {
  27. return $b->getDeletedTime() - $a->getDeletedTime();
  28. });
  29. return $items;
  30. }
  31. private function getBackendForItem(ITrashItem $item) {
  32. return $item->getTrashBackend();
  33. }
  34. public function listTrashFolder(ITrashItem $folder): array {
  35. return $this->getBackendForItem($folder)->listTrashFolder($folder);
  36. }
  37. public function restoreItem(ITrashItem $item) {
  38. return $this->getBackendForItem($item)->restoreItem($item);
  39. }
  40. public function removeItem(ITrashItem $item) {
  41. $this->getBackendForItem($item)->removeItem($item);
  42. }
  43. /**
  44. * @param IStorage $storage
  45. * @return ITrashBackend
  46. * @throws BackendNotFoundException
  47. */
  48. public function getBackendForStorage(IStorage $storage): ITrashBackend {
  49. $fullType = get_class($storage);
  50. $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) {
  51. if (
  52. $storage->instanceOfStorage($registeredType) &&
  53. ($type === '' || is_subclass_of($registeredType, $type))
  54. ) {
  55. return $registeredType;
  56. } else {
  57. return $type;
  58. }
  59. }, '');
  60. if ($foundType === '') {
  61. throw new BackendNotFoundException("Trash backend for $fullType not found");
  62. } else {
  63. return $this->backends[$foundType];
  64. }
  65. }
  66. public function moveToTrash(IStorage $storage, string $internalPath): bool {
  67. if ($this->trashPaused) {
  68. return false;
  69. }
  70. try {
  71. $backend = $this->getBackendForStorage($storage);
  72. $this->trashPaused = true;
  73. $result = $backend->moveToTrash($storage, $internalPath);
  74. $this->trashPaused = false;
  75. return $result;
  76. } catch (BackendNotFoundException $e) {
  77. return false;
  78. }
  79. }
  80. public function getTrashNodeById(IUser $user, int $fileId) {
  81. foreach ($this->backends as $backend) {
  82. $item = $backend->getTrashNodeById($user, $fileId);
  83. if ($item !== null) {
  84. return $item;
  85. }
  86. }
  87. return null;
  88. }
  89. public function pauseTrash() {
  90. $this->trashPaused = true;
  91. }
  92. public function resumeTrash() {
  93. $this->trashPaused = false;
  94. }
  95. }