You are here

public function FileEntityNormalizer::denormalize in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Normalizer/FileEntityNormalizer.php \Drupal\content_sync\Normalizer\FileEntityNormalizer::denormalize()

Overrides ContentEntityNormalizer::denormalize

File

src/Normalizer/FileEntityNormalizer.php, line 55

Class

FileEntityNormalizer
Adds the file URI to embedded file entities.

Namespace

Drupal\content_sync\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $serializer_context = array()) {
  $file_data = '';

  // Check if the image is available as base64-encoded image.
  if (!empty($data['data'][0]['value'])) {
    $file_data = $data['data'][0]['value'];

    // Avoid 'data' being treated as a field.
    unset($data['data']);
  }

  // If a directory is set, we must to copy the file to the file system.
  if (!empty($serializer_context['content_sync_directory_files'])) {
    $scheme = \Drupal::service('stream_wrapper_manager')
      ->getScheme($data['uri'][0]['value']);
    if (!empty($scheme)) {
      $source_path = realpath($serializer_context['content_sync_directory_files']) . '/' . $scheme . '/';
      $source = str_replace($scheme . '://', $source_path, $data['uri'][0]['value']);
      if (file_exists($source)) {
        $file = $this->fileSystem
          ->realpath($data['uri'][0]['value']);
        if (!file_exists($file) || md5_file($file) !== md5_file($source)) {
          $dir = $this->fileSystem
            ->dirname($data['uri'][0]['value']);
          $this->fileSystem
            ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
          $uri = $this->fileSystem
            ->copy($source, $data['uri'][0]['value']);
          $data['uri'] = [
            [
              'value' => $uri,
              'url' => str_replace($GLOBALS['base_url'], '', file_create_url($uri)),
            ],
          ];

          // We just need one method to create the image.
          $file_data = '';
        }
      }
    }
  }
  $entity = parent::denormalize($data, $class, $format, $serializer_context);

  // If the image was sent as base64 we must to create the physical file.
  if ($file_data) {

    // Decode and save to file.
    $file_contents = base64_decode($file_data);
    $dirname = $this->fileSystem
      ->dirname($entity
      ->getFileUri());
    $this->fileSystem
      ->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
    if ($uri = $this->fileSystem
      ->saveData($file_contents, $entity
      ->getFileUri())) {
      $entity
        ->setFileUri($uri);
    }
    else {
      throw new \RuntimeException(new FormattableMarkup('Failed to write @filename.', [
        '@filename' => $entity
          ->getFilename(),
      ]));
    }
  }

  // If the image was sent as URL we must to create the physical file.

  /*if ($file_data) {
      // Decode and save to file.
      $file_contents = base64_decode($file_data);
      $dirname = $this->fileSystem->dirname($entity->getFileUri());
      file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
      if ($uri = file_unmanaged_save_data($file_contents, $entity->getFileUri())) {
        $entity->setFileUri($uri);
      }
      else {
        throw new \RuntimeException(SafeMarkup::format('Failed to write @filename.', array('@filename' => $entity->getFilename())));
      }
    }*/
  return $entity;
}