You are here

public function ProcessFileAttachment::process in Replication 8

Same name and namespace in other branches
  1. 8.2 src/ProcessFileAttachment.php \Drupal\replication\ProcessFileAttachment::process()

Processes a file attachment.

Returns the file object or NULL if it can't be created.

Parameters

array $data:

string $format:

\Drupal\multiversion\Entity\WorkspaceInterface $workspace:

Return value

\Drupal\file\FileInterface|NULL

File

src/ProcessFileAttachment.php, line 39

Class

ProcessFileAttachment

Namespace

Drupal\replication

Code

public function process($data, $format, WorkspaceInterface $workspace = null) {
  $current_user_id = $this->current_user
    ->id();
  $uri = $data['uri'];
  $file_uuid = $data['uuid'];
  multiversion_prepare_file_destination($uri);

  // Check if exists a file entity with this uuid.
  $uuid_index = $this->index_factory
    ->get('multiversion.entity_index.uuid', $workspace);
  $entity_info = $uuid_index
    ->get($file_uuid);
  if (!empty($entity_info)) {

    /** @var FileInterface $file */
    $file = $this->entity_type_manager
      ->getStorage($entity_info['entity_type_id'])
      ->load($entity_info['entity_id']);
    if (!$file || !is_file($file
      ->getFileUri())) {
      $file_context = [
        'uri' => $uri,
        'uuid' => $file_uuid,
        'status' => FILE_STATUS_PERMANENT,
        'uid' => $current_user_id,
      ];
      if ($workspace) {
        $file_context['workspace'] = $workspace;
      }
      $file = \Drupal::getContainer()
        ->get('serializer')
        ->deserialize($data['data'], '\\Drupal\\file\\FileInterface', $format, $file_context);
    }
    return $file;
  }

  // Create the new entity file and the file itself.
  // Check if exists a file with this $uri, if it exists then rename the file.
  $existing_files = $this->entity_type_manager
    ->getStorage('file')
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (count($existing_files)) {
    $uri = file_destination($uri, FILE_EXISTS_RENAME);
  }
  $file_context = [
    'uri' => $uri,
    'uuid' => $file_uuid,
    'status' => FILE_STATUS_PERMANENT,
    'uid' => $current_user_id,
    'workspace' => $workspace,
  ];
  $file = \Drupal::getContainer()
    ->get('serializer')
    ->deserialize($data['data'], '\\Drupal\\file\\FileInterface', $format, $file_context);
  return $file;
}