function TemporaryJsonapiFileFieldUploader::handleFileUploadForField

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::handleFileUploadForField()
  2. 10 core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::handleFileUploadForField()
  3. 11.x core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::handleFileUploadForField()

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

core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php, line 161

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));
    $settings = $field_definition->getSettings();
    $destination = $this->getUploadLocation($settings);
    // Check the destination file path is writable.
    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}";
    if ($destination === $settings['uri_scheme'] . '://') {
        $file_uri = "{$destination}{$prepared_filename}";
    }
    $temp_file_path = $this->streamUploadData();
    $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.', $file_uri), NULL, [
            'Retry-After' => 1,
        ]);
    }
    // Begin building file entity.
    $file = File::create([]);
    $file->setOwnerId($owner->id());
    $file->setFilename($prepared_filename);
    if ($this->mimeTypeGuesser instanceof MimeTypeGuesserInterface) {
        $file->setMimeType($this->mimeTypeGuesser
            ->guessMimeType($prepared_filename));
    }
    else {
        @trigger_error('\\Symfony\\Component\\HttpFoundation\\File\\MimeType\\MimeTypeGuesserInterface is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Implement \\Symfony\\Component\\Mime\\MimeTypeGuesserInterface instead. See https://www.drupal.org/node/3133341', E_USER_DEPRECATED);
        $file->setMimeType($this->mimeTypeGuesser
            ->guess($prepared_filename));
    }
    $file->setFileUri($temp_file_path);
    // 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 against field-level validators first while the file is
    // still a temporary file. Validation is split up in 2 steps to be the same
    // as in _file_save_upload_single().
    // For backwards compatibility this part is copied from ::validate() to
    // leave that method behavior unchanged.
    // @todo Improve this with a file uploader service in
    //   https://www.drupal.org/project/drupal/issues/2940383
    $errors = file_validate($file, $validators);
    if (!empty($errors)) {
        $violations = new EntityConstraintViolationList($file);
        $translator = new DrupalTranslator();
        $entity = EntityAdapter::createFromEntity($file);
        foreach ($errors as $error) {
            $violation = new ConstraintViolation($translator->trans($error), $error, [], $entity, '', NULL);
            $violations->add($violation);
        }
        return $violations;
    }
    $file->setFileUri($file_uri);
    // Move the file to the correct location after validation. Use
    // FileSystemInterface::EXISTS_ERROR as the file location has already been
    // determined above in FileSystem::getDestinationFilename().
    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');
    }
    // Second step of the validation on the file object itself now.
    $violations = $file->validate();
    // Remove violations of inaccessible fields as they cannot stem from our
    // changes.
    $violations->filterByFieldAccess();
    if ($violations->count() > 0) {
        return $violations;
    }
    $file->save();
    $this->lock
        ->release($lock_id);
    return $file;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.