Skip to content

Commit

Permalink
On-upload: Tag files immediately #38
Browse files Browse the repository at this point in the history
Tag uploaded files with "Unscanned"
  • Loading branch information
Philip Stadermann committed Jun 12, 2024
1 parent 60ad9ee commit ba5f400
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
7 changes: 5 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down
48 changes: 48 additions & 0 deletions lib/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace OCA\GDataVaas;

use OCA\GDataVaas\Service\TagService;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\AbstractCacheEvent;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Cache\CacheEntryUpdatedEvent;
use Psr\Log\LoggerInterface;

class EventListener implements IEventListener
{
private LoggerInterface $logger;

private TagService $tagService;

public function __construct(LoggerInterface $logger, TagService $tagService)
{
$this->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/');
}
}
9 changes: 9 additions & 0 deletions lib/Service/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba5f400

Please sign in to comment.