You are here

public function SophronMimeTypeGuesser::guessMimeType in Sophron 8

Same name in this branch
  1. 8 sophron_guesser/src/SophronMimeTypeGuesser.php \Drupal\sophron_guesser\SophronMimeTypeGuesser::guessMimeType()
  2. 8 sophron_guesser/src/ProxyClass/SophronMimeTypeGuesser.php \Drupal\sophron_guesser\ProxyClass\SophronMimeTypeGuesser::guessMimeType()
1 call to SophronMimeTypeGuesser::guessMimeType()
SophronMimeTypeGuesser::guess in sophron_guesser/src/SophronMimeTypeGuesser.php
Guesses the mime type of the file with the given path.

File

sophron_guesser/src/SophronMimeTypeGuesser.php, line 44

Class

SophronMimeTypeGuesser
Makes possible to guess the MIME type of a file using its extension.

Namespace

Drupal\sophron_guesser

Code

public function guessMimeType(string $path) : ?string {
  $extension = '';
  $file_parts = explode('.', $this->fileSystem
    ->basename($path));

  // Remove the first part: a full filename should not match an extension.
  array_shift($file_parts);

  // Iterate over the file parts, trying to find a match.
  // For 'my.awesome.image.jpeg', we try: 'jpeg', then 'image.jpeg', then
  // 'awesome.image.jpeg'.
  while ($additional_part = array_pop($file_parts)) {
    $extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
    if ($mime_map_extension = $this->mimeMapManager
      ->getExtension($extension)) {
      return $mime_map_extension
        ->getDefaultType(FALSE);
    }
  }
  return 'application/octet-stream';
}