public function FeedsEnclosureUnique::saveTo in Feeds Atom 7
Same name and namespace in other branches
- 6 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 231 - 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_query("SELECT fid FROM {feeds_atom_file_import} WHERE sha1 = :sha1", array(
':sha1' => $new_file_hash,
))
->fetchField();
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);
// @todo Please review the conversion of this statement to the D7 database API syntax.
/* db_query("INSERT INTO {feeds_atom_file_import} (fid, sha1) VALUES (%d, '%s')", $info['fid'], $new_file_hash) */
$id = db_insert('feeds_atom_file_import')
->fields(array(
'fid' => $info['fid'],
'sha1' => $new_file_hash,
))
->execute();
return $info;
}
}