Skip to content

Commit

Permalink
On-upload: Tag files immediately #38
Browse files Browse the repository at this point in the history
* Rename EventListener to CacheEntryListener
* Add documentation to VerdictService
  • Loading branch information
Philip Stadermann committed Jun 13, 2024
1 parent c0da548 commit 5d77499
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use OC\Files\Filesystem;
use OCA\GDataVaas\AvirWrapper;
use OCA\GDataVaas\Service\VerdictService;
use OCA\GDataVaas\EventListener;
use OCA\GDataVaas\CacheEntryListener;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\AppFramework\App;
Expand Down Expand Up @@ -50,7 +50,7 @@ public function register(IRegistrationContext $context): void {
require_once $composerAutoloadFile;
}

EventListener::register($context);
CacheEntryListener::register($context);

// Util::connection is deprecated, but required ATM by FileSystem::addStorageWrapper
Util::connectHook('OC_Filesystem', 'preSetup', $this, 'setupWrapper');
Expand Down
6 changes: 3 additions & 3 deletions lib/EventListener.php → lib/CacheEntryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCP\Files\Cache\CacheEntryUpdatedEvent;
use Psr\Log\LoggerInterface;

class EventListener implements IEventListener
class CacheEntryListener implements IEventListener
{
private LoggerInterface $logger;

Expand All @@ -28,8 +28,8 @@ public function __construct(LoggerInterface $logger, TagService $tagService, Ver
}

public static function register(IRegistrationContext $context): void {
$context->registerEventListener(CacheEntryInsertedEvent::class, EventListener::class);
$context->registerEventListener(CacheEntryUpdatedEvent::class, EventListener::class);
$context->registerEventListener(CacheEntryInsertedEvent::class, CacheEntryListener::class);
$context->registerEventListener(CacheEntryUpdatedEvent::class, CacheEntryListener::class);
}

public function handle(Event $event): void
Expand Down
32 changes: 28 additions & 4 deletions lib/Service/VerdictService.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,23 @@ private function tagFile(int $fileId, string $tagName) {
}
}

public static function isFileTooLargeToScan($path) {
/**
* Checks if a file is too large to be scanned.
* @param string $path
* @return bool
*/
public static function isFileTooLargeToScan(string $path): bool {
$size = filesize($path);
return !$size || $size > self::MAX_FILE_SIZE;
}

public function scan(string $filePath): VaasVerdict {

/**
* Scans a file for malicious content with G DATA Verdict-as-a-Service and returns the verdict.
* @param string $filePath The local path to the file to scan.
* @return VaasVerdict The verdict.
*/
public function scan(string $filePath): VaasVerdict {
$this->lastLocalPath = $filePath;
$this->lastVaasVerdict = null;

Expand All @@ -155,14 +166,27 @@ public function scan(string $filePath): VaasVerdict {
}
}

public function onRename(string $localSource, string $localTarget)
/**
* Call this from a StorageWrapper, when a local file was renamed. This allows the scanner to track the name
* of the file that was scanned last.
* @param string $localSource The local source path.
* @param string $localTarget The local destination path.
*/
public function onRename(string $localSource, string $localTarget): void
{
if ($localSource === $this->lastLocalPath) {
$this->lastLocalPath = $localTarget;
}
}

public function tagLastScannedFile(string $localPath, int $fileId) {

/**
* Tag the file that was scanned last with it's verdict. Call this from an EventListener on CacheEntryInsertedEvent or
* CacheEntryUpdatedEvent.
* @param string $localPath The local path.
* @param int $fileId The corresponding file id to tag.
*/
public function tagLastScannedFile(string $localPath, int $fileId): void {
if (self::isFileTooLargeToScan($localPath)) {
$this->tagFile($fileId, TagService::WONT_SCAN);
return;
Expand Down

0 comments on commit 5d77499

Please sign in to comment.