Skip to content

Commit

Permalink
Ability to validate multiple files in one request (#36)
Browse files Browse the repository at this point in the history
* * Make sure ClamavValidator can validate single as well as multiple files
* Provide test cases for validating multiple files
* Provide additional file without virus or error

* * Code cosmetics after peer review.
* Update README.md indicating that clamav-validator supports validating multiple files.

Co-authored-by: guuske <guus.leeuw@itpassion.com>
Co-authored-by: PHPGuus <php.guus@gmail.com>
  • Loading branch information
3 people authored Apr 21, 2021
1 parent d9d81af commit 2cc3e5b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ $rules = [
];
```

`ClamavValidator` will automatically run multiple files one-by-one through ClamAV in case `file` represent multiple uploaded files.

<a name="author"></a>
## Author

Expand Down
22 changes: 22 additions & 0 deletions src/ClamavValidator/ClamavValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ public function validateClamav($attribute, $value, $parameters)
return true;
}

if(is_array($value)) {
$result = true;
foreach($value as $file) {
$result &= $this->validateFileWithClamAv($file);
}

return $result;
}

return $this->validateFileWithClamAv($value);
}

/**
* Validate the single uploaded file for virus/malware with ClamAV.
*
* @param $value mixed
*
* @return bool
* @throws ClamavValidatorException
*/
protected function validateFileWithClamAv($value)
{
$file = $this->getFilePath($value);
if (! is_readable($file)) {
throw ClamavValidatorException::forNonReadableFile($file);
Expand Down
39 changes: 39 additions & 0 deletions tests/ClamavValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ClamavValidatorTest extends \PHPUnit_Framework_TestCase
protected $error_data;
protected $rules;
protected $messages;
protected $multiple_files_all_clean;
protected $multiple_files_some_with_virus;

public function setUp()
{
Expand All @@ -35,6 +37,19 @@ public function setUp()
$this->error_data = [
'file' => $this->getTempPath(__DIR__ . '/files/test3.txt')
];
$this->multiple_files_all_clean = [
'files' => [
$this->getTempPath(__DIR__ . '/files/test1.txt'),
$this->getTempPath(__DIR__ . '/files/test4.txt'),
]
];
$this->multiple_files_some_with_virus = [
'files' => [
$this->getTempPath(__DIR__ . '/files/test1.txt'),
$this->getTempPath(__DIR__ . '/files/test2.txt'),
$this->getTempPath(__DIR__ . '/files/test4.txt'),
]
];
$this->messages = [];

$config = new Config();
Expand Down Expand Up @@ -66,6 +81,18 @@ public function testValidatesClean()
$this->assertTrue($validator->passes());
}

public function testValidatesCleanMultiFile()
{
$validator = new ClamavValidator(
$this->translator,
$this->multiple_files_all_clean,
['files' => 'clamav'],
$this->messages
);

$this->assertTrue($validator->passes());
}

public function testValidatesVirus()
{
$validator = new ClamavValidator(
Expand All @@ -78,6 +105,18 @@ public function testValidatesVirus()
$this->assertFalse($validator->passes());
}

public function testValidatesVirusMultiFile()
{
$validator = new ClamavValidator(
$this->translator,
$this->multiple_files_some_with_virus,
['files' => 'clamav'],
$this->messages
);

$this->assertFalse($validator->passes());
}

public function testValidatesError()
{
$this->setExpectedException(ClamavValidatorException::class, 'is not readable');
Expand Down
5 changes: 5 additions & 0 deletions tests/files/test4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dfdsfdsfdsf
ds
fds
fdsfds
fdsfdsfds

0 comments on commit 2cc3e5b

Please sign in to comment.