You are here

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'
svg_upload_sanitizer.services.yml in ./svg_upload_sanitizer.services.yml
svg_upload_sanitizer.services.yml
1 service uses SanitizerHelper
svg_upload_sanitizer.helper.sanitizer in ./svg_upload_sanitizer.services.yml
Drupal\svg_upload_sanitizer\Helper\SanitizerHelper

File

src/Helper/SanitizerHelper.php, line 17

Namespace

Drupal\svg_upload_sanitizer\Helper
View 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

Namesort descending Modifiers Type Description Overrides
SanitizerHelper::$fileSystem private property The Drupal file system helper.
SanitizerHelper::$sanitizer private property The sanitizer.
SanitizerHelper::sanitize public function Sanitize a File entity.
SanitizerHelper::__construct public function SanitizerHelper constructor.