Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/Listener/ShareListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/Public/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/ActionScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
82 changes: 43 additions & 39 deletions lib/Service/FsEventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,7 +25,7 @@ public function __construct(
private QueueService $queue,
private ActionScheduler $actionService,
private StorageService $storageService,
private \OCP\Share\IManager $shareManager,
private IRootFolder $rootFolder,
) {

}
Expand All @@ -34,60 +36,55 @@ 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 {
if ($node instanceof Folder) {
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more O(n) DB queries

$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 {
Expand All @@ -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)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAllFilesInFolder checks mimetypes already

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;
Expand Down
6 changes: 1 addition & 5 deletions lib/Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Loading