You are here

function emthumb_fetch_remote_thumbnail in Embedded Media Field 6

Same name and namespace in other branches
  1. 6.3 contrib/emthumb/emthumb.module \emthumb_fetch_remote_thumbnail()
  2. 6.2 contrib/emthumb/emthumb.module \emthumb_fetch_remote_thumbnail()

This fetches the thumbnail from the remote provider for local storage.

1 call to emthumb_fetch_remote_thumbnail()
emthumb_emfield_field_extra in contrib/emthumb/emthumb.module
Implements hook_emfield_field_extra(). This is called on field operations to allow us to act on emthumbs.

File

contrib/emthumb/emthumb.module, line 551
Allows for custom thumbnail overrides to Embedded Media Field.

Code

function emthumb_fetch_remote_thumbnail($item, $field) {

  // Obviously, only go forward if our item has been parsed for a provider.
  if ($item['provider']) {

    // Get the URL to the original thumbnail.
    $thumbnail = emfield_include_invoke($field['module'], $item['provider'], 'thumbnail', $field, $item, 'thumbnail', NULL, $field['widget']['thumbnail_width'], $field['widget']['thumbnail_height'], array());

    // Go forward only if we have a URL to go by.
    if ($thumbnail) {

      // The new file will be associated with the global user.
      global $user;

      // Attempt to fetch the thumbnail from the provided URL.
      $request = drupal_http_request($thumbnail);

      // Only go forward if we actually have an image stream.
      if ($image = $request->data) {

        // Add in our check of the the file name length.
        $validators['file_validate_name_length'] = array();

        // Allow for transliteration, which will take unicode data and convert
        // it to US-ASCII for better file storage.
        if (module_exists('transliteration')) {

          // Transliterate our original URL.
          $thumbnail = transliteration_get($thumbnail);
        }

        // We need to account for slashes in the value, such as from hulu.
        // Thus we'll convert them to dashes.
        // Our new filepath will be in the form of emvideo-youtube-xd3ewke.jpg.
        $basename = $field['module'] . '-' . $item['provider'] . '-' . str_replace('/', '-', $item['value']) . '.' . pathinfo($thumbnail, PATHINFO_EXTENSION);

        // Get the base Drupal files path.
        $directory = file_directory_path();
        if ($field['widget']['emimport_image_path']) {

          // Add the field's image path here.
          $directory .= '/' . $field['widget']['emimport_image_path'];
        }

        // Create a new filepath from our desired filename.
        $filepath = file_create_filename($basename, $directory);

        // Begin building file object.
        $file = new stdClass();
        $file->uid = $user->uid;

        // Strip out the query if provided.
        $basename_arr = parse_url($basename);
        $filepath_arr = parse_url($filepath);
        $file->filename = $basename_arr['path'];
        $file->filepath = $filepath_arr['path'];

        // If we have mimedetect, then do so. Otherwise we make a best guess
        // based on the filename.
        $file->filemime = module_exists('mimedetect') ? mimedetect_mime($file) : file_get_mimetype($file->filename);

        // Rename potentially executable files, to help prevent exploits.
        if (preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && substr($file->filename, -4) != '.txt') {
          $file->filemime = 'text/plain';
          $file->filepath .= '.txt';
          $file->filename .= '.txt';
        }

        // If the destination is not provided, or is not writable, then use the
        // temporary directory.
        if (empty($dest) || file_check_path($dest) === FALSE) {
          $dest = file_directory_temp();
        }
        $file->source = 'emthumb_fetch_remote_thumbnail';
        $file->destination = file_destination($file->filepath, $replace);
        $file->filesize = strlen($image);

        // Call the validation functions.
        $errors = array();
        foreach ($validators as $function => $args) {
          array_unshift($args, $file);
          $errors = array_merge($errors, call_user_func_array($function, $args));
        }

        // Check for validation errors.
        if (!empty($errors)) {
          $message = t('The selected file %name could not be saved.', array(
            '%name' => $file->filename,
          ));
          if (count($errors) > 1) {
            $message .= '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
          }
          else {
            $message .= ' ' . array_pop($errors);
          }
          form_set_error($file->source, $message);
          return 0;
        }
        if (!file_save_data($image, $file->filepath, FILE_EXISTS_RENAME)) {
          form_set_error($file->source, t('Thumbnail error. Could not copy provider thumbnail.'));
          watchdog('file', 'Upload error. Could not move file %file to destination %destination.', array(
            '%file' => $file->filename,
            '%destination' => $file->destination,
          ));
          return 0;
        }

        // If we made it this far it's safe to record this file in the database.
        $file->status = FILE_STATUS_PERMANENT;
        $file->timestamp = time();
        drupal_write_record('files', $file);

        // Let modules add additional properties to the yet barebone file object.
        // This uses the future hook_file from d7's API. Not sure if anything
        // actually uses this right now, but they might in the future.
        foreach (module_implements('file_insert') as $module) {
          $function = $module . '_file_insert';
          $function($file);
        }
        return (array) $file;
      }
    }
  }
}