class SanitizerHelper in SVG Upload Sanitizer 8
Sanitizer Helper class.
@package Drupal\svg_upload_sanitizer\Helper
@internal
Hierarchy
- class \Drupal\svg_upload_sanitizer\Helper\SanitizerHelper uses \Psr\Log\LoggerAwareTrait
Expanded class hierarchy of SanitizerHelper
3 files declare their use of SanitizerHelper
- FileInsertHookHandler.php in src/
HookHandler/ FileInsertHookHandler.php - FileInsertHookHandlerTest.php in tests/
src/ Unit/ HookHandler/ FileInsertHookHandlerTest.php - SanitizerHelperTest.php in tests/
src/ Unit/ Helper/ SanitizerHelperTest.php
1 string reference to 'SanitizerHelper'
1 service uses SanitizerHelper
File
- src/
Helper/ SanitizerHelper.php, line 17
Namespace
Drupal\svg_upload_sanitizer\HelperView source
class SanitizerHelper {
use LoggerAwareTrait;
/**
* The Drupal file system helper.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
private $fileSystem;
/**
* The sanitizer.
*
* @var \enshrined\svgSanitize\Sanitizer
*/
private $sanitizer;
/**
* SanitizerHelper constructor.
*
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The Drupal file system helper.
* @param \enshrined\svgSanitize\Sanitizer $sanitizer
* The sanitizer.
*/
public function __construct(FileSystemInterface $file_system, Sanitizer $sanitizer) {
$this->fileSystem = $file_system;
$this->sanitizer = $sanitizer;
}
/**
* Sanitize a File entity.
*
* @param \Drupal\file\FileInterface $file
* The file.
*
* @return bool
* TRUE if the sanitization has succeeded. FALSE otherwise.
*
* @throws \Exception
*/
public function sanitize(FileInterface $file) {
if ('image/svg+xml' !== $file
->getMimeType()) {
return FALSE;
}
$filePath = $this->fileSystem
->realpath($file
->getFileUri());
if (FALSE === $filePath) {
$this->logger
->notice(sprintf('Could not resolve the path of the file (URI: "%s").', $file
->getFileUri()));
return FALSE;
}
if (FALSE === file_exists($filePath)) {
$this->logger
->notice(sprintf('The file does not exist (path: "%s").', $filePath));
return FALSE;
}
$fileContent = file_get_contents($filePath);
if (FALSE === $fileContent || empty($fileContent)) {
$this->logger
->notice(sprintf('Could not retrieve the content of the file (path: "%s").', $filePath));
return FALSE;
}
$fileCleanContent = $this->sanitizer
->sanitize($fileContent);
if (FALSE === file_put_contents($filePath, $fileCleanContent)) {
throw new \Exception(sprintf('Cannot sanitize the file (URI: "%s")', $file
->getFileUri()));
}
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SanitizerHelper:: |
private | property | The Drupal file system helper. | |
SanitizerHelper:: |
private | property | The sanitizer. | |
SanitizerHelper:: |
public | function | Sanitize a File entity. | |
SanitizerHelper:: |
public | function | SanitizerHelper constructor. |