You are here

class FeedsEnclosure in Feeds 7

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

Enclosure element, can be part of the result array.

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
FeedsElement::$value protected property
FeedsElement::getValue public function @todo Make value public and deprecate use of getValue(). 3
FeedsElement::__toString public function 1
FeedsEnclosure::$file protected property
FeedsEnclosure::$mime_type protected property
FeedsEnclosure::getContent public function
FeedsEnclosure::getFile public function Get a Drupal file object of the enclosed resource, download if necessary.
FeedsEnclosure::getMIMEType public function 1
FeedsEnclosure::__construct public function Constructor, requires MIME type. Overrides FeedsElement::__construct 1