You are here

protected function SocialContent::saveExternalFile in Social Content 7.2

Utility static function to get and save a remote file.

Parameters

string $url: The url of the file to get.

string $field_name: The field name which to file will be saved to

array $validators: List of validators to run against the file before it is imported.

Return value

mixed An array representing the saved image or FALSE if there was an error.

10 calls to SocialContent::saveExternalFile()
SocialContent::attachFields in ./social_content.class.inc
Attach declare fields onto wrapper.
SocialContentFacebook::prepareRow in modules/facebook/social_content_facebook.class.inc
Do the uploads and attach expected fields to a row about to be imported.
SocialContentFlickr::prepareRow in modules/flickr/social_content_flickr.class.inc
Do the uploads and attach expected fields to a row about to be imported.
SocialContentInstagram::prepareRow in modules/instagram/social_content_instagram.class.inc
Do the uploads and attach expected fields to a row about to be imported.
SocialContentLinkedin::prepareRow in modules/linkedin/social_content_linkedin.class.inc
Do the uploads and attach expected fields to a row about to be imported.

... See full list

File

./social_content.class.inc, line 867
Social Content class.

Class

SocialContent
TODO: Table names should be a property for ease of change Separate this class into smaller classes.

Code

protected function saveExternalFile($url, $field_name, $validators = array()) {
  $result = self::httpRequest($url);
  if ($result->code != 200) {
    return FALSE;
  }

  // Find the image extension
  // Facebook has generic /graph/picture urls that redirect.
  // So look for a redirect url first in the response.
  if (isset($result->redirect_url)) {
    $url_info = drupal_parse_url($result->redirect_url);
  }
  else {
    $url_info = drupal_parse_url($url);
  }
  $path_info = pathinfo($url_info['path']);
  $filename = $path_info['basename'];
  $field_info = field_info_field($field_name);
  if ($field_info['type'] == 'image') {
    $validators += array(
      'file_validate_is_image' => array(),
    );
  }
  $field_uri_scheme = $field_info['settings']['uri_scheme'];
  $instance_info = field_info_instance('node', $field_name, $this->settings['instance']['content_type']);
  if ($instance_info && !empty($instance_info['settings']) && !empty($instance_info['settings']['file_directory'])) {
    $directory = $instance_info['settings']['file_directory'];
    $dir_path = $field_uri_scheme . '://' . $directory;
    if (file_prepare_directory($dir_path, FILE_CREATE_DIRECTORY)) {
      $filename = $directory . '/' . $filename;
    }
  }
  $file = NULL;
  try {
    $file = file_save_data($result->data, $field_uri_scheme . '://' . $filename, FILE_EXISTS_RENAME);
  } catch (Exception $e) {
    watchdog('social_content', 'Error saving file: %message', array(
      '%message' => $e
        ->getMessage(),
    ));
    return FALSE;
  }
  if (!$file) {
    return FALSE;
  }
  if ($validators && file_validate($file, $validators)) {
    return FALSE;
  }

  // Add additional fields (required for adding to a file field).
  $file->status = 1;
  $file->display = 1;
  return (array) $file;
}