You are here

protected function MimeDetectService::getMimeByDetector in MimeDetect 8

Get the MIME type for a given file by MIME detector plugins.

Parameters

\Drupal\file\FileInterface $file: The file to be analyzed.

Return value

string The detected MIME, NULL if no detectors available for the given filename or its content is not recognized by any detector.

1 call to MimeDetectService::getMimeByDetector()
MimeDetectService::getMime in src/MimeDetectService.php
Get the MIME type for a given file.

File

src/MimeDetectService.php, line 122

Class

MimeDetectService
MimeDetect service.

Namespace

Drupal\mimedetect

Code

protected function getMimeByDetector(FileInterface $file) {
  $filename_parts = explode('.', $file
    ->getFilename());
  $filename_extension = strtolower(array_pop($filename_parts));
  $file_path = $this->fileSystem
    ->realpath($file
    ->getFileUri());
  $mime = NULL;
  foreach ($this->mimeDetectorPluginManager
    ->getDefinitions() as $plugin_id => $definition) {
    if (in_array($filename_extension, $definition['filename_extensions']) && ($mime = $this->mimeDetectorPluginManager
      ->createInstance($plugin_id)
      ->detect($file_path))) {
      break;
    }
  }
  return $mime;
}