You are here

function oembed_remote_file_formatter_view in oEmbed 7.0

Same name and namespace in other branches
  1. 8 oembed.module \oembed_remote_file_formatter_view()
  2. 7 oembed.module \oembed_remote_file_formatter_view()

Implements hook_file_formatter_FORMATTER_view().

1 string reference to 'oembed_remote_file_formatter_view'
oembed_file_formatter_info_alter in ./oembed.file.inc
Implements hook_file_formatter_info_alter().

File

./oembed.module, line 671

Code

function oembed_remote_file_formatter_view($file, $display, $langcode) {
  $scheme = file_uri_scheme($file->uri);
  if ($scheme == 'oembed') {
    $embed = $file->oembed;
    if ($embed['type'] == 'photo') {
      $url = $embed['url'];
    }
    else {
      if (isset($embed['thumbnail_url'])) {
        $url = $embed['thumbnail_url'];
      }
    }
    if (isset($url)) {
      $parsed_url = parse_url($url);
      $path = $parsed_url['host'];
      $path .= drupal_dirname($parsed_url['path']);
      $filename = drupal_basename($parsed_url['path']);
      if (strpos($filename, '.') !== FALSE) {
        $filename = file_munge_filename($filename, 'jpg jpeg gif png', FALSE);
      }
      $path .= '/' . $filename;
      $local_uri = file_stream_wrapper_uri_normalize(file_default_scheme() . '://oembed/' . $path);
      if (!file_exists($local_uri)) {
        if (strpos($url, '//') === 0) {
          $url = 'http:' . $url;
        }
        $dirname = drupal_dirname($local_uri);
        file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
        $data = drupal_http_request($url);
        file_unmanaged_save_data($data->data, $local_uri);
      }
      $image_file = file_uri_to_object($local_uri);
      if (!isset($image_file->type) || $image_file->type === FILE_TYPE_NONE) {
        $type = file_get_type($image_file);
        if (isset($type)) {
          $image_file->type = $type;
        }
      }
      if ($image_file->filesize) {
        $image_file->image_dimensions = image_get_info($image_file->uri);
        $image_file->filemime = $image_file->image_dimensions['mime_type'];
        return file_entity_file_formatter_file_image_view($image_file, $display, $langcode);
      }
    }
  }
}