class FeedsEnclosure in Feeds 7
Same name and namespace in other branches
- 6 plugins/FeedsParser.inc \FeedsEnclosure
- 7.2 plugins/FeedsParser.inc \FeedsEnclosure
Enclosure element, can be part of the result array.
Hierarchy
- class \FeedsElement
- class \FeedsEnclosure
Expanded class hierarchy of FeedsEnclosure
File
- plugins/
FeedsParser.inc, line 194
View source
class FeedsEnclosure extends FeedsElement {
protected $mime_type;
protected $file;
/**
* Constructor, requires MIME type.
*/
public function __construct($value, $mime_type) {
parent::__construct($value);
$this->mime_type = $mime_type;
}
/**
* @return
* MIME type of return value of getValue().
*/
public function getMIMEType() {
return $this->mime_type;
}
/**
* @return
* The content of the referenced resource.
*/
public function getContent() {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this
->getValue());
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array(
'@url' => $this
->getValue(),
'!code' => $result->code,
)));
}
return $result->data;
}
/**
* Get a Drupal file object of the enclosed resource, download if necessary.
*
* @param $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
* A file path to the enclosed resource.
*
* @todo file_destination(*, FILE_EXISTS_RENAME) is not concurrency safe.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsElement:: |
protected | property | ||
FeedsElement:: |
public | function | @todo Make value public and deprecate use of getValue(). | 3 |
FeedsElement:: |
public | function | 1 | |
FeedsEnclosure:: |
protected | property | ||
FeedsEnclosure:: |
protected | property | ||
FeedsEnclosure:: |
public | function | ||
FeedsEnclosure:: |
public | function | Get a Drupal file object of the enclosed resource, download if necessary. | |
FeedsEnclosure:: |
public | function | 1 | |
FeedsEnclosure:: |
public | function |
Constructor, requires MIME type. Overrides FeedsElement:: |
1 |