You are here

public function FeedsEnclosure::getFile in Feeds 7.2

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

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

Parameters

string $destination: The path or uri specifying the target directory in which the file is expected. Don't use trailing slashes unless it's a streamwrapper scheme.

int $replace: Replace behavior when the destination file already exists.

Return value

object|false A Drupal temporary file object of the enclosed resource or FALSE if the value is empty.

Throws

Exception If file object could not be created.

See also

file_save_data()

File

plugins/FeedsParser.inc, line 470
Contains FeedsParser and related classes.

Class

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

Code

public function getFile($destination, $replace = FILE_EXISTS_RENAME) {
  $file = FALSE;
  if ($this
    ->getValue()) {

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

    // Copy or save file depending on whether it is remote or local.
    if (drupal_realpath($this
      ->getSanitizedUri())) {
      $file = new stdClass();
      $file->uid = 0;
      $file->uri = $this
        ->getSanitizedUri();
      $file->filemime = $this
        ->getMIMEType();
      $file->filename = $this
        ->getSafeFilename();
      if (drupal_dirname($file->uri) !== $destination) {
        $file = file_copy($file, $destination, $replace);
      }
      else {

        // If file is not to be copied, check whether file already exists,
        // as file_save() won't do that for us (compare file_copy() and
        // file_save())
        $existing_files = file_load_multiple(array(), array(
          'uri' => $file->uri,
        ));
        if (count($existing_files)) {
          $existing = reset($existing_files);
          if ($replace == FEEDS_FILE_EXISTS_SKIP) {
            return $existing;
          }
          $file->fid = $existing->fid;
          $file->filename = $existing->filename;
        }
        file_save($file);
      }
    }
    else {
      if (file_uri_target($destination)) {
        $destination = trim($destination, '/') . '/';
      }
      try {
        $filename = $this
          ->getLocalValue();
        if (module_exists('transliteration')) {
          require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
          $filename = transliteration_clean_filename($filename);
        }
        $file = file_save_data($this
          ->getContent(), $destination . $filename, $replace);
      } catch (Exception $e) {
        watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
      }
    }

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