You are here

public function FeedsEnclosureUnique::saveTo in Feeds Atom 6

Same name and namespace in other branches
  1. 7 plugins/FeedsAtomRDFProcessor.inc \FeedsEnclosureUnique::saveTo()

Saves the file represented by this enclosure to disk.

If the file already exists, based on its sha1() hash, then we will simply reuse the existing file rather than saving a new one.

Parameters

$target_dir: The directory to which to save the file. Note that if the file has already been imported it is possible that it will not be in the requested directory, in which case this method returns the existing file info in its existing location.

Return value

The file info array as defined by filefield of the file that we just saved, or of the pre-existing file that should be used instead.

File

plugins/FeedsAtomRDFProcessor.inc, line 262
Contains the feeds atom RDF processor class.

Class

FeedsEnclosureUnique
Specialized version of FeedsEnclosure to ensure uniqueness when saving.

Code

public function saveTo($target_dir) {
  $new_file_hash = sha1_file($this->file);
  $fid = db_result(db_query("SELECT fid FROM {feeds_atom_file_import} WHERE sha1 = '%s'", $new_file_hash));
  if ($fid) {

    // Pull the info for the existing file and return that. We won't save
    // the new file at all.
    $info = field_file_load($fid);
    return $info;
  }
  else {

    // Save the new file, and record its hash for later matching.
    $info = field_file_save_file($this->file, array(), $target_dir);
    db_query("INSERT INTO {feeds_atom_file_import} (fid, sha1) VALUES (%d, '%s')", $info['fid'], $new_file_hash);
    return $info;
  }
}