public static function OEmbedStreamWrapper::getMimeType in oEmbed 7
Same name and namespace in other branches
- 7.0 OEmbedStreamWrapper.inc \OEmbedStreamWrapper::getMimeType()
Base implementation of getMimeType().
Overrides DrupalStreamWrapperInterface::getMimeType
File
- ./OEmbedStreamWrapper.inc, line 25 
- Create a oEmbed Stream Wrapper class.
Class
- OEmbedStreamWrapper
- @file Create a oEmbed Stream Wrapper class.
Code
public static function getMimeType($uri, $mapping = NULL) {
  $url = rawurldecode(substr($uri, 9));
  $embed = oembed_get_data($url);
  // The mime type can be specified in hook_oembed_response_alter() which is
  // useful to map responses with type 'rich' and 'link' to more appropriate
  // Drupal file entity bundles. See oembed_oembed_response_alter().
  if (isset($embed['mime_type'])) {
    return $embed['mime_type'];
  }
  if ($embed) {
    switch ($embed['type']) {
      case 'video':
        return 'video/oembed';
      case 'photo':
        return 'image/oembed';
      default:
        return 'text/oembed';
    }
  }
  else {
    // URIs for valid oEmbed responses may become invalid after they are saved
    // to the file_managed table. This might happen because the oEmbed
    // endpoint is down or the provider is misconfigured. The content may
    // have been deleted or become inaccessible. Some of these
    // situations are temporary, so the stream wrapper should try to return a
    // MIME type for URIs that are already saved as Drupal file entities.
    $type = db_select('file_managed', 'f')
      ->fields('f', array(
      'type',
    ))
      ->condition('uri', $uri)
      ->execute()
      ->fetchField();
    if (in_array($type, array(
      'image',
      'video',
      'audio',
    ))) {
      return $type . '/oembed';
    }
    else {
      if ($type) {
        return 'text/oembed';
      }
    }
  }
  return FALSE;
}