static function LocalStream::getMimeType

Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getMimeType().

Overrides StreamWrapperInterface::getMimeType

1 call to LocalStream::getMimeType()
file_get_mimetype in drupal/core/includes/file.inc
Determines an Internet Media Type or MIME type from a filename.

File

drupal/core/lib/Drupal/Core/StreamWrapper/LocalStream.php, line 99
Definition of Drupal\Core\StreamWrapper\LocalStream.

Class

LocalStream
Defines a Drupal stream wrapper base class for local files.

Namespace

Drupal\Core\StreamWrapper

Code

static function getMimeType($uri, $mapping = NULL) {
  if (!isset($mapping)) {

    // The default file map, defined in file.mimetypes.inc is quite big.
    // We only load it when necessary.
    include_once DRUPAL_ROOT . '/core/includes/file.mimetypes.inc';
    $mapping = file_mimetype_mapping();
  }
  $extension = '';
  $file_parts = explode('.', drupal_basename($uri));

  // 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
  //   - image.jpeg, and
  //   - awesome.image.jpeg
  while ($additional_part = array_pop($file_parts)) {
    $extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
    if (isset($mapping['extensions'][$extension])) {
      return $mapping['mimetypes'][$mapping['extensions'][$extension]];
    }
  }
  return 'application/octet-stream';
}