You are here

public function PrivateFileSchemeHandler::getFile in Acquia Content Hub 8.2

Makes file available to Drupal through the correct stream wrapper.

This does not return the file, but will save it with the appropriate stream wrapper for Drupal to utilize.

Parameters

\Acquia\ContentHubClient\CDF\CDFObject $object: The CDFObject from which to extract details about getting the file.

Return value

bool Whether the file successfully saved or not.

Overrides FileSchemeHandlerInterface::getFile

File

src/Plugin/FileSchemeHandler/PrivateFileSchemeHandler.php, line 91

Class

PrivateFileSchemeHandler
File scheme handler for private files.

Namespace

Drupal\acquia_contenthub\Plugin\FileSchemeHandler

Code

public function getFile(CDFObject $object) {
  if ($object
    ->getAttribute('file_location') && $object
    ->getAttribute('file_uri') && $object
    ->getUuid()) {
    $url = $object
      ->getAttribute('file_location')
      ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    $uri = $object
      ->getAttribute('file_uri')
      ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    $uuid = $object
      ->getUuid();
    $dirname = $this->fileSystem
      ->dirname($uri);
    if ($this->fileSystem
      ->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY)) {
      try {
        $contents = $this->contentHubCommonActions
          ->requestRemoteEntity($url, $uri, $uuid, 'private');
      } catch (\Exception $exception) {
        \Drupal::messenger()
          ->addError(t('Unable to request file.'));
      }
      return $this
        ->saveData($contents, $uri, FileSystemInterface::EXISTS_REPLACE);
    }
  }
  return FALSE;
}