You are here

class File in Helper 8

Provides helpers for working with files.

Hierarchy

  • class \Drupal\helper\File

Expanded class hierarchy of File

1 file declares its use of File
HelperExtension.php in src/Twig/HelperExtension.php
1 string reference to 'File'
helper.services.yml in ./helper.services.yml
helper.services.yml
1 service uses File
helper.file in ./helper.services.yml
Drupal\helper\File

File

src/File.php, line 13

Namespace

Drupal\helper
View source
class File {

  /**
   * The file storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $fileStorage;

  /**
   * The mime type guesser.
   *
   * @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
   */
  protected $mimeTypeGuesser;

  /**
   * File constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $mime_type_guesser
   *   The mime type guesser.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MimeTypeGuesserInterface $mime_type_guesser) {
    $this->fileStorage = $entity_type_manager
      ->getStorage('file');
    $this->mimeTypeGuesser = $mime_type_guesser;
  }

  /**
   * Creates or reuses a file object based on a URI.
   *
   * @param string $uri
   *   A string containing the URI.
   * @param bool $reuse_existing
   *   If TRUE will try to reuse an existing file with the same URI.
   *   If FALSE will always create a new file.
   *
   * @return \Drupal\file\FileInterface
   *   A file object.
   */
  public function createOrReuseFromUri($uri, $reuse_existing = TRUE) {
    if ($reuse_existing) {

      // Check if this file already exists, and if so, return that.
      $files = $this->fileStorage
        ->loadByProperties([
        'uri' => $uri,
      ]);
      if ($valid_files = array_filter($files, [
        $this,
        'filterValidFiles',
      ])) {
        return reset($valid_files);
      }
    }

    // If an existing file could not be found, create a new file.
    return $this->fileStorage
      ->create([
      'uri' => $uri,
      'uid' => \Drupal::currentUser()
        ->id(),
    ]);
  }

  /**
   * Filter callback; Permanent files or temporary files owned by current user.
   *
   * @param \Drupal\file\FileInterface $file
   *   The file object.
   *
   * @return bool
   *   TRUE if the file is valid, or FALSE otherwise.
   */
  public static function filterValidFiles(FileInterface $file) {
    return $file
      ->isPermanent() || $file
      ->getOwnerId() == \Drupal::currentUser()
      ->id();
  }

  /**
   * Converts a file URL into a data URI.
   *
   * @param string $uri
   *   The file URI.
   * @param bool $base_64_encode
   *   TRUE to return the data URI as base-64 encoded content.
   * @param string|null $mimetype
   *   The optional mime type to provide for the data URI. If not provided
   *   the mime type guesser service will be used.
   *
   * @return string
   *   The image data URI for use in a src attribute.
   *
   * @throws \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException
   *   If the file cannot be read.
   */
  public function getDataUri($uri, $base_64_encode = TRUE, $mimetype = NULL) {
    if (!isset($mimetype)) {
      $mimetype = $this->mimeTypeGuesser
        ->guess($uri);
    }
    $contents = file_get_contents($uri);
    if ($contents === FALSE) {
      throw new AccessDeniedException($uri);
    }
    if ($base_64_encode) {
      $contents = base64_encode($contents);
    }
    return 'data:' . $mimetype . ($base_64_encode ? ';base64' : '') . ',' . $contents;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
File::$fileStorage protected property The file storage.
File::$mimeTypeGuesser protected property The mime type guesser.
File::createOrReuseFromUri public function Creates or reuses a file object based on a URI.
File::filterValidFiles public static function Filter callback; Permanent files or temporary files owned by current user.
File::getDataUri public function Converts a file URL into a data URI.
File::__construct public function File constructor.