You are here

public function AssetFileEntityHelper::createNewFile in Media: Acquia DAM 8

Creates a new file for an asset.

Parameters

\cweagans\webdam\Entity\Asset $asset: The asset to save a new file for.

string $destinationFolder: The path to save the asset into.

Return value

bool|\Drupal\file\FileInterface The created file or FALSE on failure.

File

src/Service/AssetFileEntityHelper.php, line 202

Class

AssetFileEntityHelper
Class AssetFileEntityHelper.

Namespace

Drupal\media_acquiadam\Service

Code

public function createNewFile(Asset $asset, $destinationFolder) {

  // Ensure we can write to our destination directory.
  if (!$this->fileSystem
    ->prepareDirectory($destinationFolder, FileSystemInterface::CREATE_DIRECTORY)) {
    $this->loggerChannel
      ->warning('Unable to save file for asset ID @asset_id on directory @destination_folder.', [
      '@asset_id' => $asset->id,
      '@destination_folder' => $destinationFolder,
    ]);
    return FALSE;
  }
  $destination_path = sprintf('%s/%s', $destinationFolder, $asset->filename);
  $file_contents = $this
    ->fetchRemoteAssetData($asset, $destinationFolder, $destination_path);
  $existing = $this->assetMediaFactory
    ->getFileEntity($asset->id);
  $file = $existing instanceof FileInterface ? $this
    ->replaceExistingFile($existing, $file_contents, $destination_path) : $this
    ->drupalFileSaveData($file_contents, $destination_path);
  if ($file instanceof FileInterface) {
    return $file;
  }
  $this->loggerChannel
    ->warning('Unable to save file for asset ID @asset_id.', [
    '@asset_id' => $asset->id,
  ]);
  return FALSE;
}