TrashManager.php 3.5 KB

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