You are here

public function FeedsEnclosure::getFile in Feeds 7

Same name and namespace in other branches
  1. 6 plugins/FeedsParser.inc \FeedsEnclosure::getFile()
  2. 7.2 plugins/FeedsParser.inc \FeedsEnclosure::getFile()

Get a Drupal file object of the enclosed resource, download if necessary.

@todo file_destination(*, FILE_EXISTS_RENAME) is not concurrency safe.

Parameters

$destination: The path or uri specifying the target directory in which the file is expected. If not given, a previously defined directory will be assumed. The default directory is tmp.

Return value

A file path to the enclosed resource.

File

plugins/FeedsParser.inc, line 240

Class

FeedsEnclosure
Enclosure element, can be part of the result array.

Code

public function getFile($destination) {
  if (empty($this->file) && $this
    ->getValue()) {

    // File is in the right place, nothing to do.
    if (strpos($this
      ->getValue(), $directory) === 0) {
      $this->file = $this
        ->getValue();
      return $this->file;
    }

    // Prepare destination directory.
    file_prepare_directory($destination, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);

    // Copy or save file depending its location.
    if (drupal_realpath($this
      ->getValue())) {
      $file = new stdClass();
      $file->uid = $user->uid;
      $file->status = FILE_STATUS_PERMANENT;
      $file->uri = $this
        ->getValue();
      $this->file = file_copy($file, $destination);
    }
    else {
      $filename = basename($this
        ->getValue());
      if (module_exists('transliteration')) {
        require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
        $filename = transliteration_clean_filename($filename);
      }
      $this->file = file_save_data($this
        ->getContent(), "{$destination}/{$filename}");
    }

    // We couldn't make sense of this enclosure, throw an exception.
    if (!$this->file) {
      throw new Exception(t('Invalid enclosure %enclosure', array(
        '%enclosure' => $this
          ->getValue(),
      )));
    }
  }
  return $this->file;
}