You are here

function oembed_remote_file_formatter_view in oEmbed 7

Same name and namespace in other branches
  1. 8 oembed.module \oembed_remote_file_formatter_view()
  2. 7.0 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 662
Core functionality for oEmbed

Code

function oembed_remote_file_formatter_view($file, $display, $langcode) {
  $scheme = file_uri_scheme($file->uri);
  if ($scheme == 'oembed') {

    // URI of local copy of remote file must be stored because it may be
    // different from the oEmbed URLs. If the URL does not have a valid
    // extension, it will redirect to a URL that does.
    if (!isset($file->metadata['oembed_remote_file_image']) || !file_exists($file->metadata['oembed_remote_file_image'])) {
      $embed = $file->metadata['oembed'];
      if ($embed['type'] == 'photo' && !empty($embed['url'])) {
        $url = $embed['url'];
      }
      else {
        if (isset($embed['thumbnail_url'])) {
          $url = $embed['thumbnail_url'];
        }
      }
      if (isset($url)) {
        $result = drupal_http_request($url);

        // Using the redirect URL's basename might guarantee a path with an
        // appropriate file extension.
        if (isset($result->redirect_url)) {

          // If the redirect and original basenames are identical, do nothing.
          if (drupal_basename($result->redirect_url) != drupal_basename($url)) {
            $url .= '/' . drupal_basename($result->redirect_url);
          }
        }
        $parsed_url = parse_url($url);

        // Store local copies of images using hostname, path and filename of source.
        $path = $parsed_url['host'];
        $path .= drupal_dirname($parsed_url['path']);
        if (substr($path, -1) != '/') {
          $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)) {

          // Drupal dislikes protocol relative URL schemes. Everything should
          // be accessible without HTTPS.
          if (strpos($url, '//') === 0) {
            $url = 'http:' . $url;
          }

          /// Ensure filesystem has directories for new file.
          $dirname = drupal_dirname($local_uri);
          file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

          // Save the file data to the local directory.
          $files = entity_load('file', FALSE, array(
            'uri' => $local_uri,
          ));
          if (!empty($files)) {
            file_unmanaged_save_data($result->data, $local_uri);
          }
          else {
            file_save_data($result->data, $local_uri);
          }
        }
        $file->metadata['oembed_remote_file_image'] = $local_uri;

        // Redundantish. See file_entity_file_insert and related hooks.
        foreach (array(
          'oembed_remote_file_image',
        ) as $name) {
          if (!empty($file->metadata[$name])) {
            $value = $file->metadata[$name];
            db_merge('file_metadata')
              ->fields(array(
              'value' => serialize($value),
            ))
              ->key(array(
              'fid' => $file->fid,
              'name' => $name,
            ))
              ->execute();
          }
        }
      }
    }
    if (isset($file->metadata['oembed_remote_file_image'])) {
      $local_uri = $file->metadata['oembed_remote_file_image'];
      $image_file = file_uri_to_object($local_uri);
      $image_file->metadata = array();

      // Forcing the image file's type is perhaps no longer necessary.
      if (!isset($image_file->type) || $image_file->type === FILE_TYPE_NONE) {
        $image_file->type = 'image';
      }
      if ($image_file->filesize) {
        $image_file->metadata = image_get_info($image_file->uri);
        $image_file->filemime = $image_file->metadata['mime_type'];
        return file_entity_file_formatter_file_image_view($image_file, $display, $langcode);
      }
    }
  }
}