From ba5f40081dbc347bc0ccb0f9ee3ee25a96ae73cd Mon Sep 17 00:00:00 2001 From: Philip Stadermann Date: Tue, 11 Jun 2024 09:40:02 +0200 Subject: [PATCH] On-upload: Tag files immediately #38 Tag uploaded files with "Unscanned" --- README.md | 13 +++++----- lib/AppInfo/Application.php | 7 ++++-- lib/EventListener.php | 48 +++++++++++++++++++++++++++++++++++++ lib/Service/TagService.php | 9 +++++++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 lib/EventListener.php diff --git a/README.md b/README.md index 19372532..d92c1b57 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,10 @@ folder "nextcloud-docker-dev" and running ```docker compose up nextcloud proxy`` ### Useful commands -| Description | Command | -|---------------------------|--------------------------------------------------------------------------------------| -| Trigger cronjobs manually | `docker exec --user www-data {nextcloud_container} php /var/www/html/cron.php` | -| Upgrade Nextcloud via CLI | `docker exec --user www-data {nextcloud_container} php occ upgrade` | -| Watch logs | `docker exec --user www-data {nextcloud_container} php occ log:watch` | -| Set log level to debug | `docker exec --user www-data {nextcloud_container} php occ log:manage --level DEBUG` | +| Description | Command | +|---------------------------|----------------------------------------------------------------------------------------------------------| +| Trigger cronjobs manually | `docker exec --user www-data {nextcloud_container} php /var/www/html/cron.php` | +| Upgrade Nextcloud via CLI | `docker exec --user www-data {nextcloud_container} php occ upgrade` | +| Watch logs | `docker exec --user www-data {nextcloud_container} php occ log:watch` | +| Set log level to debug | `docker exec --user www-data {nextcloud_container} php occ log:manage --level DEBUG` | +| Watch logs | `docker exec --user www-data nextcloud-container /bin/sh -c "tail -f data/nextcloud.log" \| jq .message` | diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index bc01e2f9..6f7ca8fd 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -7,6 +7,7 @@ use OC\Files\Filesystem; use OCA\GDataVaas\AvirWrapper; use OCA\GDataVaas\Service\VerdictService; +use OCA\GDataVaas\EventListener; use OCP\Activity\IManager; use OCP\App\IAppManager; use OCP\AppFramework\App; @@ -48,8 +49,10 @@ public function register(IRegistrationContext $context): void { if (file_exists($composerAutoloadFile)) { require_once $composerAutoloadFile; } - - // Util::connection is deprecated, but required ATM by FileSystem::addStorageWrapper + + EventListener::register($context); + + // Util::connection is deprecated, but required ATM by FileSystem::addStorageWrapper Util::connectHook('OC_Filesystem', 'preSetup', $this, 'setupWrapper'); } diff --git a/lib/EventListener.php b/lib/EventListener.php new file mode 100644 index 00000000..d81314ad --- /dev/null +++ b/lib/EventListener.php @@ -0,0 +1,48 @@ +logger = $logger; + $this->tagService = $tagService; + } + + public static function register(IRegistrationContext $context): void { + $context->registerEventListener(CacheEntryInsertedEvent::class, EventListener::class); + $context->registerEventListener(CacheEntryUpdatedEvent::class, EventListener::class); + } + + public function handle(Event $event): void + { + if (!$event instanceof AbstractCacheEvent) { + return; + } + + $path = $event->getPath(); + $fileId = $event->getFileId(); + + if (self::shouldTag($path) && !$this->tagService->hasAnyVaasTag($fileId)) { + $this->tagService->setTag($event->getFileId(), TagService::UNSCANNED); + } + } + + private static function shouldTag(string $path): bool { + return str_starts_with($path, 'files/'); + } +} diff --git a/lib/Service/TagService.php b/lib/Service/TagService.php index 4fc2241e..f5bea13a 100644 --- a/lib/Service/TagService.php +++ b/lib/Service/TagService.php @@ -112,6 +112,15 @@ public function hasUnscannedTag(int $fileId): bool { return $this->tagMapper->haveTag([$fileId], 'files', $this->getTag(self::UNSCANNED)->getId()); } + /** + * Checks if a file has any Vaas tag. + * @param int $fileId + * @return bool + */ + public function hasAnyVaasTag(int $fileId): bool { + return $this->hasAnyButUnscannedTag($fileId) || $this->hasUnscannedTag($fileId); + } + /** * @param string $tagName * @param int $limit Count of object ids you want to get