You are here

public static function DrupalRemoteStreamWrapper::getMimeType in Remote Stream Wrapper 7

Base implementation of getMimeType().

Overrides DrupalStreamWrapperInterface::getMimeType

File

./remote_stream_wrapper.inc, line 63

Class

DrupalRemoteStreamWrapper
Stream wrapper to support local files.

Code

public 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 . '/includes/file.mimetypes.inc';
    $mapping = file_mimetype_mapping();
  }
  if ($path = parse_url($uri, PHP_URL_PATH)) {
    $extension = '';
    $file_parts = explode('.', drupal_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
    //   - 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]];
      }
    }
  }

  // Fallback to the 'Content-Type' header.
  $request = drupal_http_request($uri, array(
    'method' => 'HEAD',
  ));
  if (empty($request->error) && !empty($request->headers['content-type'])) {
    return $request->headers['content-type'];
  }
  return 'application/octet-stream';
}