You are here

function PMPAPIDrupalPull::createEnclosureFile in Public Media Platform API Integration 7

Creates a file object from a PMP object.

Parameters

object $enclosure: A PMP object enclosure

string $profile: The name of a PMP profile

string $guid: A PMP GUID

Return value

A file object

1 call to PMPAPIDrupalPull::createEnclosureFile()
PMPAPIDrupalPull::saveEntity in pmpapi_pull/classes/PMPAPIDrupalPull.php
Saves a PMP doc as an entity.

File

pmpapi_pull/classes/PMPAPIDrupalPull.php, line 183
Contains PMPAPIDrupalPull.

Class

PMPAPIDrupalPull
Turns PMP hypermedia docs in Drupal entities.

Code

function createEnclosureFile($enclosure, $profile, $guid) {
  $href = pmpapi_pull_get_enclosure_url($enclosure);
  $file = new stdClass();
  if (pmpapi_pull_make_local_files($profile)) {
    $file_mimetype = $enclosure->type;

    // NPR enclosures are M3U's. The call to pmpapi_pull_get_enclosure_url()
    // above should extract an MP3 URL, but we also need to also adjust the
    // enclosure->type (if it exists)
    if ($file_mimetype == 'audio/m3u') {
      $file_mimetype = 'audio/mpeg';
    }
    $ext = pmpapi_pull_get_ext_from_mimetype($file_mimetype);
    $filename = $guid . '.' . $ext;

    // Each (media) profile can have its own dedicated stream wrapper (no GUI
    // for this yet)
    $scheme = variable_get('pmpapi_stream_wrapper_' . $profile, file_default_scheme());
    $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
    $path = $wrapper
      ->getDirectoryPath();
    $subdir = method_exists($wrapper, 'getSubdirectory') ? $wrapper
      ->getSubdirectory() : NULL;
    if ($subdir) {
      $dest = $path . '/' . $subdir . '/' . $filename;
    }
    else {
      $dest = $path . '/' . $filename;
    }
    $local_uri = system_retrieve_file($href, $dest, FALSE, FILE_EXISTS_REPLACE);
    $filesize = filesize($local_uri);
    if ($subdir) {
      $uri = $scheme . '://' . $subdir . '/' . $filename;
    }
    else {
      $uri = $scheme . '://' . $filename;
    }
    $file->filename = $filename;
    $file->filemime = $file_mimetype;
    $file->filesize = $filesize;
  }
  else {
    $uri = $href;
  }
  $file->uid = $this->uid;
  $file->uri = $uri;
  $file->status = 1;
  $file->pmpapi_guid = $guid;

  // Give valid->from and valid->to NULL values; the second entity save will
  // map any applicable values from the doc.
  $file->pmpapi_valid_from = NULL;
  $file->pmpapi_valid_to = NULL;
  $file->pmpapi_pull = TRUE;

  // to ensure it doesn't get pushed
  // Does file already exist?
  $files = entity_load('file', FALSE, array(
    'uri' => $uri,
  ));
  if (!empty($files)) {
    $existing_file = array_shift($files);
    $file->fid = $existing_file->fid;
  }
  file_save($file);
  return $file;
}