diff --git a/Listener/VirusScannerListener.php b/Listener/VirusScannerListener.php index f1dd77a82914d7ae7a77234aadf2c1edd67ffcce..47c0e81f368aa397fc90379e204553d611bcfdd7 100644 --- a/Listener/VirusScannerListener.php +++ b/Listener/VirusScannerListener.php @@ -28,17 +28,6 @@ class VirusScannerListener public function __construct(Client $client) { $this->client = $client; - - try { - // Check clamd server's state - $this->client->ping(); - - // Reload the virus database - $this->client->reload(); - } catch (ConnectionException $connectionException) { - $this->client->shutdown(); - $this->client = null; - } } @@ -47,27 +36,42 @@ class VirusScannerListener */ public function onFileUploadCompleted(FileUploadCompleteEvent $event) { - if ($this->client != null) { - $file = $event->getUploadedFile(); - $path = $file->getLocalPath(); + $this->client = $this->getClient(); + + $file = $event->getUploadedFile(); + $path = $file->getLocalPath(); + + $result = $this->client->scanFile($path); + $hasVirus = $result['status'] === Client::RESULT_FOUND; + + $meta = $file->getMetadata(); + $meta['virus_scanner'] = ['has_virus' => $hasVirus]; - $result = $this->client->scanFile($path); - $hasVirus = $result['status'] == Client::RESULT_FOUND; + if ($hasVirus) { + $desc = $result['reason']; + $meta['virus_scanner']['detected'] = $result['reason']; + } - $meta = $file->getMetadata(); - $meta['virus_scanner'] = ['has_virus' => $hasVirus]; + $file->setMetadata($meta); - if ($hasVirus) { - $desc = $result['reason']; - $meta['virus_scanner']['detected'] = $result['reason']; - } + if ($hasVirus) { + throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !'); + } + } - $file->setMetadata($meta); + /** + * @return Client + * @throws ConnectionException + */ + private function getClient(): Client + { + try { + // Check clamd server's state + $this->client->ping(); + return $this->client; - if ($hasVirus) { - throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !'); - } - $this->client->shutdown(); + } catch (ConnectionException $connectionException) { + throw $connectionException; } } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index ec2f9b3ad74251d9f3c0341846d0498eed6017a6..4cc0d7fbcad73fa0aed1959ff215776e2109248c 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -10,9 +10,9 @@ parameters: irstea_file_upload.max_chunk_size: 0 - irstea_file_upload.clamav_socket_path: unix:///var/run/clamav/clamd.ctl - irstea_file_upload.client_timeout: 30 - irstea_file_upload.client_mode: PHP_NORMAL_READ + irstea_file_upload.virus_scanner.clamav_socket_path: unix:///var/run/clamav/clamd.ctl + irstea_file_upload.virus_scanner.client_timeout: 30 + irstea_file_upload.virus_scanner.client_mode: !php/const:PHP_NORMAL_READ services: @@ -74,22 +74,22 @@ services: - "%irstea_file_upload.filesystem.name%" # Scanner anti-virus - irstea_file_upload.socket: + irstea_file_upload.virus_scanner.socket: class: Socket\Raw\Socket arguments: - - "%irstea_file_upload.clamav_socket_path" + - "%irstea_file_upload.virus_scanner.clamav_socket_path" - irstea_file_upload.client: + irstea_file_upload.virus_scanner.client: class: Xenolope\Quahog\Client arguments: - - "@irstea_file_upload.socket" - - "%irstea_file_upload.client_timeout" - - "%irstea_file_upload.client_mode" + - "@irstea_file_upload.virus_scanner.socket" + - "%irstea_file_upload.virus_scanner.client_timeout" + - "%irstea_file_upload.virus_scanner.client_mode" irstea_file_upload.virus_scanner: class: Irstea\FileUploadBundle\Listener\VirusScannerListener arguments: - - "@irstea_file_upload.client" + - "@irstea_file_upload.virus_scanner.client" tags: - { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted } diff --git a/Tests/Listener/VirusScannerListenerTest.php b/Tests/Listener/VirusScannerListenerTest.php index 6b9ad06b6f680faef246047aca1a3eee011ae3d5..c3db1fe07adfcdf4f1261d0d6fa29291b294a2c9 100644 --- a/Tests/Listener/VirusScannerListenerTest.php +++ b/Tests/Listener/VirusScannerListenerTest.php @@ -12,6 +12,7 @@ use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Xenolope\Quahog\Client; +use Xenolope\Quahog\Exception\ConnectionException; /** * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16. @@ -46,14 +47,15 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase $this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); $this->file = $this->getMock(UploadedFileInterface::class); - $this->file->expects(static::once())->method('getLocalPath')->willReturn('foopath'); - $this->file->expects(static::once())->method('getMetadata')->willReturn([]); + $this->file->expects($this->any())->method('getLocalPath')->willReturn('foopath'); + $this->file->expects($this->any())->method('getMetadata')->willReturn([]); $this->event = new FileUploadCompleteEvent($this->file); $this->listener = new VirusScannerListener($this->client); } + public function testOnFileUploadCompletedResultOk() { $result = [ @@ -62,8 +64,8 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase 'status' => Client::RESULT_OK ]; - $this->client->expects(static::once())->method('scanFile')->with('foopath')->willReturn($result); - $this->file->expects(static::once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]); + $this->client->expects($this->any())->method('scanFile')->with('foopath')->willReturn($result); + $this->file->expects($this->any())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]); $this->listener->onFileUploadCompleted($this->event); } @@ -84,10 +86,27 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase ->with('foopath') ->willReturn($result); - $this->file->expects(static::once()) + $this->file->expects($this->any()) ->method('setMetadata') ->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]); $this->listener->onFileUploadCompleted($this->event); } + + /** + * @expectedException Xenolope\Quahog\Exception\ConnectionException + */ + public function testGetClientFailed() + { + $this->client->expects($this->any())->method('ping')->willThrowException(new ConnectionException()); + $this->listener->onFileUploadCompleted($this->event); + } + + + public function testGetClientSucceed() + { + $this->client->expects($this->any())->method('ping')->willReturn(true); + $this->listener->onFileUploadCompleted($this->event); + } + }