TrashManager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\Files_Trashbin\Trash;
  22. use OCP\Files\FileInfo;
  23. use OCP\Files\Mount\IMountPoint;
  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. return $backend->moveToTrash($storage, $internalPath);
  90. } catch (BackendNotFoundException $e) {
  91. return false;
  92. }
  93. }
  94. public function getTrashNodeById(IUser $user, int $fileId) {
  95. foreach ($this->backends as $backend) {
  96. $item = $backend->getTrashNodeById($user, $fileId);
  97. if ($item !== null) {
  98. return $item;
  99. }
  100. }
  101. return null;
  102. }
  103. public function pauseTrash() {
  104. $this->trashPaused = true;
  105. }
  106. public function resumeTrash() {
  107. $this->trashPaused = false;
  108. }
  109. }