diff --git a/lib/Listener/ShareListener.php b/lib/Listener/ShareListener.php index 8a770148..fe16be67 100644 --- a/lib/Listener/ShareListener.php +++ b/lib/Listener/ShareListener.php @@ -64,15 +64,12 @@ public function handle(Event $event): void { } if ($node->getType() === FileInfo::TYPE_FOLDER) { - $files = $this->storageService->getAllFilesInFolder($node); - foreach ($files as $file) { - if (!$file instanceof File) { - continue; - } + $fileIds = $this->storageService->getAllFilesInFolder($node); + foreach ($fileIds as $fileId) { $this->actionService->updateAccess( UpdateAccessOp::ALLOW, $userIds, - ProviderConfigService::getSourceId($file->getId()), + ProviderConfigService::getSourceId($fileId), ); } } else { @@ -128,11 +125,11 @@ public function handle(Event $event): void { $userIds = array_values(array_unique(array_merge($realFileUserIds, $shareUserIds))); if ($node instanceof Folder) { - $files = $this->storageService->getAllFilesInFolder($node); - foreach ($files as $file) { + $fileIds = $this->storageService->getAllFilesInFolder($node); + foreach ($fileIds as $fileId) { $this->actionService->updateAccessDeclSource( $userIds, - ProviderConfigService::getSourceId($file->getId()), + ProviderConfigService::getSourceId($fileId), ); } } else { diff --git a/lib/Public/ContentManager.php b/lib/Public/ContentManager.php index 9526e018..a70ee432 100644 --- a/lib/Public/ContentManager.php +++ b/lib/Public/ContentManager.php @@ -242,7 +242,7 @@ public function deleteContent(string $appId, string $providerId, array $itemIds) $this->collectAllContentProviders(); $providerKey = ProviderConfigService::getConfigKey($appId, $providerId); - $this->actionService->deleteSources(...array_map(function (string $itemId) use ($providerKey) { + $this->actionService->deleteSources(array_map(function (string $itemId) use ($providerKey) { return ProviderConfigService::getSourceId($itemId, $providerKey); }, $itemIds)); } diff --git a/lib/Service/ActionScheduler.php b/lib/Service/ActionScheduler.php index c99cafdb..dc91ad03 100644 --- a/lib/Service/ActionScheduler.php +++ b/lib/Service/ActionScheduler.php @@ -17,7 +17,7 @@ use OCP\BackgroundJob\IJobList; class ActionScheduler { - private const BATCH_SIZE = 500; + public const BATCH_SIZE = 500; public function __construct( private IJobList $jobList, @@ -46,7 +46,7 @@ private function scheduleAction(string $type, string $payload): void { * @param string[] $sourceIds * @return void */ - public function deleteSources(string ...$sourceIds): void { + public function deleteSources(array $sourceIds): void { // batch sourceIds into self::BATCH_SIZE chunks $batches = array_chunk($sourceIds, self::BATCH_SIZE); diff --git a/lib/Service/FsEventService.php b/lib/Service/FsEventService.php index d4c7653d..faae1703 100644 --- a/lib/Service/FsEventService.php +++ b/lib/Service/FsEventService.php @@ -11,8 +11,10 @@ use OCA\ContextChat\Db\QueueFile; use OCA\ContextChat\Logger; use OCP\DB\Exception; +use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\InvalidPathException; +use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; @@ -23,7 +25,7 @@ public function __construct( private QueueService $queue, private ActionScheduler $actionService, private StorageService $storageService, - private \OCP\Share\IManager $shareManager, + private IRootFolder $rootFolder, ) { } @@ -34,36 +36,24 @@ public function onAccessUpdateDecl(Node $node, bool $recurse = true): void { return; } - $files = $this->storageService->getAllFilesInFolder($node); + $fileIds = $this->storageService->getAllFilesInFolder($node); } else { - $files = [$node]; - } - - foreach ($files as $file) { - if (!$this->allowedMimeType($file)) { - continue; + if (!$this->allowedMimeType($node)) { + return; } try { - $fileRef = ProviderConfigService::getSourceId($file->getId()); - $fileUserIds = $this->storageService->getUsersForFileId($file->getId()); - - if (class_exists('OCP\Files\Config\Event\UserMountAddedEvent')) { - $userIds = $fileUserIds; - } else { - // todo: Remove this once we no longer support Nextcloud 31 - $shareAccessList = $this->shareManager->getAccessList($file, true, true); - /** @var string[] $shareUserIds */ - $shareUserIds = array_keys($shareAccessList['users']); - $userIds = array_values(array_unique(array_merge($shareUserIds, $fileUserIds))); - } - - $this->actionService->updateAccessDeclSource($userIds, $fileRef); + $fileIds = [$node->getId()]; } catch (InvalidPathException|NotFoundException $e) { - $this->logger->warning('Cannot get file id for declarative access update:' . $e->getMessage(), [ - 'exception' => $e - ]); + return; } } + + foreach ($fileIds as $fileId) { + $fileRef = ProviderConfigService::getSourceId($fileId); + $fileUserIds = $this->storageService->getUsersForFileId($fileId); + + $this->actionService->updateAccessDeclSource($fileUserIds, $fileRef); + } } public function onDelete(Node $node, bool $recurse = true): void { @@ -71,23 +61,30 @@ public function onDelete(Node $node, bool $recurse = true): void { if (!$recurse) { return; } - $files = $this->storageService->getAllFilesInFolder($node); + $fileIds = $this->storageService->getAllFilesInFolder($node); } else { - $files = [$node]; - } - - foreach ($files as $file) { - if (!$this->allowedMimeType($file)) { - continue; + if (!$this->allowedMimeType($node)) { + return; } + try { + $fileIds = [$node->getId()]; + } catch (InvalidPathException|NotFoundException $e) { + return; + } + } + $fileRefs = []; + foreach ($fileIds as $fileId) { try { - $fileRef = ProviderConfigService::getSourceId($file->getId()); - $this->actionService->deleteSources($fileRef); + $fileRefs[] = ProviderConfigService::getSourceId($fileId); } catch (InvalidPathException|NotFoundException $e) { $this->logger->warning($e->getMessage(), ['exception' => $e]); } } + $batches = array_chunk($fileRefs, ActionScheduler::BATCH_SIZE); + foreach ($batches as $batch) { + $this->actionService->deleteSources($batch); + } } public function onInsert(Node $node, bool $recurse = true, bool $update = false): void { @@ -98,19 +95,26 @@ public function onInsert(Node $node, bool $recurse = true, bool $update = false) if (!$recurse) { return; } - $files = $this->storageService->getAllFilesInFolder($node); + $fileIds = $this->storageService->getAllFilesInFolder($node); } else { - $files = [$node]; + if (!$this->allowedMimeType($node)) { + return; + } + try { + $fileIds = [$node->getId()]; + } catch (InvalidPathException|NotFoundException $e) { + return; + } } - foreach ($files as $file) { - if (!$this->allowedMimeType($file)) { + foreach ($fileIds as $fileId) { + $file = current($this->rootFolder->getById($fileId)); + if (!$file instanceof File) { continue; } if (!$this->allowedPath($file)) { continue; } - $queueFile = new QueueFile(); if ($file->getMountPoint()->getNumericStorageId() === null) { return; diff --git a/lib/Service/StorageService.php b/lib/Service/StorageService.php index f295c812..50e50aa1 100644 --- a/lib/Service/StorageService.php +++ b/lib/Service/StorageService.php @@ -338,11 +338,7 @@ public function getAllFilesInFolder(Node $node): \Generator { $filesGen = $this->getFilesInMount($mount->getNumericStorageId(), $node->getId(), 0, 0); foreach ($filesGen as $fileId) { - $node = current($this->rootFolder->getById($fileId)); - if (!$node instanceof File) { - continue; - } - yield $node; + yield $fileId; } }