You are here

public static function S3fsStreamWrapper::getMimeType in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::getMimeType()
  2. 7 S3fsStreamWrapper.inc \S3fsStreamWrapper::getMimeType()

Static function to determine a file's media type.

Uses Drupal's mimetype mapping, unless a different mapping is specified.

Return value

string The file's MIME type, or 'application/octet-stream' if no type can be determined.

Overrides DrupalStreamWrapperInterface::getMimeType

1 call to S3fsStreamWrapper::getMimeType()
S3fsStreamWrapper::stream_flush in ./S3fsStreamWrapper.inc
Support for fflush(). Flush current cached stream data to a file in S3.

File

./S3fsStreamWrapper.inc, line 239
Drupal stream wrapper implementation for S3 File System.

Class

S3fsStreamWrapper
The stream wrapper class.

Code

public static function getMimeType($uri, $mapping = NULL) {
  self::_debug("getMimeType({$uri}, {$mapping}) called.");

  // Load the default mime type map.
  if (!isset(self::$mimeTypeMapping)) {
    include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
    self::$mimeTypeMapping = file_mimetype_mapping();
  }

  // If a mapping wasn't specified, use the default map.
  if ($mapping == NULL) {
    $mapping = self::$mimeTypeMapping;
  }
  $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
  // - 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]];
    }
  }

  // No mime type matches, so return the default.
  return 'application/octet-stream';
}