You are here

public function TemporaryJsonapiFileFieldUploader::handleFileUploadForField in JSON:API 8.2

Creates and validates a file entity for a file field from a file stream.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition of the field for which the file is to be uploaded.

string $filename: The name of the file.

\Drupal\Core\Session\AccountInterface $owner: The owner of the file. Note, it is the responsibility of the caller to enforce access.

Return value

\Drupal\file\FileInterface|\Drupal\Core\Entity\EntityConstraintViolationListInterface The newly uploaded file entity, or a list of validation constraint violations

Throws

\Symfony\Component\HttpKernel\Exception\HttpException Thrown when temporary files cannot be written, a lock cannot be acquired, or when temporary files cannot be moved to their new location.

File

src/Controller/TemporaryJsonapiFileFieldUploader.php, line 144

Class

TemporaryJsonapiFileFieldUploader
Reads data from an upload stream and creates a corresponding file entity.

Namespace

Drupal\jsonapi\Controller

Code

public function handleFileUploadForField(FieldDefinitionInterface $field_definition, $filename, AccountInterface $owner) {
  assert(is_a($field_definition
    ->getClass(), FileFieldItemList::class, TRUE));
  $destination = $this
    ->getUploadLocation($field_definition
    ->getSettings());

  // Check the destination file path is writable.
  // @todo Remove when we stop supporting Drupal 8.5 and 8.6.
  if (floatval(\Drupal::VERSION) < 8.699999999999999) {
    if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
      throw new HttpException(500, 'Destination file path is not writable');
    }
  }
  else {
    if (!$this->fileSystem
      ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
      throw new HttpException(500, 'Destination file path is not writable');
    }
  }
  $validators = $this
    ->getUploadValidators($field_definition);
  $prepared_filename = $this
    ->prepareFilename($filename, $validators);

  // Create the file.
  $file_uri = "{$destination}/{$prepared_filename}";
  $temp_file_path = $this
    ->streamUploadData();

  // @todo Remove when we stop supporting Drupal 8.5 and 8.6.
  if (floatval(\Drupal::VERSION) < 8.699999999999999) {

    // This will take care of altering $file_uri if a file already exists.
    file_unmanaged_prepare($temp_file_path, $file_uri);
  }
  else {
    $file_uri = $this->fileSystem
      ->getDestinationFilename($file_uri, FileSystemInterface::EXISTS_RENAME);
  }

  // Lock based on the prepared file URI.
  $lock_id = $this
    ->generateLockIdFromFileUri($file_uri);
  if (!$this->lock
    ->acquire($lock_id)) {
    throw new HttpException(503, sprintf('File "%s" is already locked for writing.'), NULL, [
      'Retry-After' => 1,
    ]);
  }

  // Begin building file entity.
  $file = File::create([]);
  $file
    ->setOwnerId($owner
    ->id());
  $file
    ->setFilename($prepared_filename);
  $file
    ->setMimeType($this->mimeTypeGuesser
    ->guess($prepared_filename));
  $file
    ->setFileUri($file_uri);

  // Set the size. This is done in File::preSave() but we validate the file
  // before it is saved.
  $file
    ->setSize(@filesize($temp_file_path));

  // Validate the file entity against entity-level validation and field-level
  // validators.
  $violations = $this
    ->validate($file, $validators);
  if ($violations
    ->count() > 0) {
    return $violations;
  }

  // Move the file to the correct location after validation. Use
  // FILE_EXISTS_ERROR as the file location has already been determined above
  // in FileSystem::getDestinationFilename().
  // @todo Remove when we stop supporting Drupal 8.5 and 8.6.
  if (floatval(\Drupal::VERSION) < 8.699999999999999) {
    if (!file_unmanaged_move($temp_file_path, $file_uri, FILE_EXISTS_ERROR)) {
      throw new HttpException(500, 'Temporary file could not be moved to file location.');
    }
  }
  else {
    try {
      $this->fileSystem
        ->move($temp_file_path, $file_uri, FileSystemInterface::EXISTS_ERROR);
    } catch (FileException $e) {
      throw new HttpException(500, 'Temporary file could not be moved to file location');
    }
  }
  $file
    ->save();
  $this->lock
    ->release($lock_id);
  return $file;
}