You are here

public static function StreamWrapper::getMimeType in AmazonS3 7.2

File

src/StreamWrapper.php, line 262
Drupal stream wrapper implementation for Amazon S3

Class

StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Namespace

Drupal\amazons3

Code

public static function getMimeType($uri, $mapping = NULL) {

  // Load the default file map.
  // @codeCoverageIgnoreStart
  if (!$mapping) {
    $mapping = static::file_mimetype_mapping();
  }

  // @codeCoverageIgnoreEnd
  $extension = '';
  $file_parts = explode('.', 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]];
    }

    // @codeCoverageIgnoreStart
  }

  // @codeCoverageIgnoreEnd
  return 'application/octet-stream';
}