From 2696ff58b495f28be0a0167fbefdbc6cc728988c Mon Sep 17 00:00:00 2001 From: bnoordsij Date: Mon, 18 Mar 2024 10:05:52 +0100 Subject: [PATCH] allow passing stream directly to scanner (#68) * allow passing stream directly to scanner * moved resource check out of getFilename method to validateFileWithClamAv * removed double spaces --- src/ClamavValidator/ClamavValidator.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ClamavValidator/ClamavValidator.php b/src/ClamavValidator/ClamavValidator.php index 8fd7b34..e9b0ca6 100755 --- a/src/ClamavValidator/ClamavValidator.php +++ b/src/ClamavValidator/ClamavValidator.php @@ -45,7 +45,7 @@ public function __construct( */ public function validateClamav(string $attribute, $value, array $parameters): bool { - if (filter_var(Config::get('clamav.skip_validation'), FILTER_VALIDATE_BOOLEAN)) { + if (filter_var(Config::get('clamav.skip_validation'), FILTER_VALIDATE_BOOLEAN)) { return true; } @@ -71,15 +71,20 @@ public function validateClamav(string $attribute, $value, array $parameters): bo */ protected function validateFileWithClamAv($value): bool { - $file = $this->getFilePath($value); - if (! is_readable($file)) { - throw ClamavValidatorException::forNonReadableFile($file); + if (is_resource($value)) { + $file = $value; + } else { + $file = $this->getFilePath($value); + if (! is_readable($file)) { + throw ClamavValidatorException::forNonReadableFile($file); + } } try { - $socket = $this->getClamavSocket(); + $socket = $this->getClamavSocket(); $scanner = $this->createQuahogScannerClient($socket); - $result = $scanner->scanResourceStream(fopen($file, 'rb')); + $stream = is_resource($file) ? $file : fopen($file, 'rb'); + $result = $scanner->scanResourceStream($stream); } catch (Exception $exception) { if (Config::get('clamav.client_exceptions')) { throw ClamavValidatorException::forClientException($exception);