class TemporaryJsonapiFileFieldUploader (View source)

internal  This will be removed once https://www.drupal.org/project/drupal/issues/2940383 lands.
 

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

This is implemented at the field level for the following reasons:

  • Validation for uploaded files is tied to fields (allowed extensions, max size, etc..).
  • The actual files do not need to be stored in another temporary location, to be later moved when they are referenced from a file field.
  • Permission to upload a file can be determined by a user's field- and entity-level access.

Constants

REQUEST_HEADER_FILENAME_REGEX

The regex used to extract the filename from the content disposition header.

BYTES_TO_READ

The amount of bytes to read in each iteration when streaming file data.

Properties

protected LoggerInterface $logger

A logger instance.

protected FileSystemInterface $fileSystem

The file system service.

protected MimeTypeGuesserInterface $mimeTypeGuesser

The MIME type guesser.

protected Token $token

The token replacement instance.

protected LockBackendInterface $lock

The lock service.

protected ImmutableConfig $systemFileConfig

System file configuration.

protected EventDispatcherInterface $eventDispatcher

The event dispatcher.

Methods

__construct(LoggerInterface $logger, FileSystemInterface $file_system, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispatcher = NULL)

Constructs a FileUploadResource instance.

handleFileUploadForField(FieldDefinitionInterface $field_definition, string $filename, AccountInterface $owner)

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

string
validateAndParseContentDispositionHeader(Request $request)

Validates and extracts the filename from the Content-Disposition header.

checkFileUploadAccess(AccountInterface $account, FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL)

Checks if the current user has access to upload the file.

string
streamUploadData()

Streams file upload data to temporary file and moves to file destination.

validate(FileInterface $file, array $validators)

Validates the file.

string
prepareFilename(string $filename, array $validators)

Prepares the filename to strip out any malicious extensions.

string
getUploadLocation(array $settings)

Determines the URI for a file field.

array
getUploadValidators(FieldDefinitionInterface $field_definition)

Retrieves the upload validators for a field definition.

static string
generateLockIdFromFileUri(string $file_uri)

Generates a lock ID based on the file URI.

Details

__construct(LoggerInterface $logger, FileSystemInterface $file_system, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispatcher = NULL)

Constructs a FileUploadResource instance.

Parameters

LoggerInterface $logger

A logger instance.

FileSystemInterface $file_system

The file system service.

MimeTypeGuesserInterface $mime_type_guesser

The MIME type guesser.

Token $token

The token replacement instance.

LockBackendInterface $lock

The lock service.

ConfigFactoryInterface $config_factory

The config factory.

EventDispatcherInterface $event_dispatcher

(optional) The event dispatcher.

FileInterface|EntityConstraintViolationListInterface handleFileUploadForField(FieldDefinitionInterface $field_definition, string $filename, AccountInterface $owner)

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

Parameters

FieldDefinitionInterface $field_definition

The field definition of the field for which the file is to be uploaded.

string $filename

The name of the file.

AccountInterface $owner

The owner of the file. Note, it is the responsibility of the caller to enforce access.

Return Value

FileInterface|EntityConstraintViolationListInterface

The newly uploaded file entity, or a list of validation constraint violations

Exceptions

HttpException

string validateAndParseContentDispositionHeader(Request $request)

Validates and extracts the filename from the Content-Disposition header.

Parameters

Request $request

The request object.

Return Value

string

The filename extracted from the header.

Exceptions

BadRequestHttpException

static AccessResultInterface checkFileUploadAccess(AccountInterface $account, FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL)

Checks if the current user has access to upload the file.

Parameters

AccountInterface $account

The account for which file upload access should be checked.

FieldDefinitionInterface $field_definition

The field definition for which to get validators.

EntityInterface $entity

(optional) The entity to which the file is to be uploaded, if it exists. If the entity does not exist and it is not given, create access to the file will be checked.

Return Value

AccessResultInterface

The file upload access result.

protected string streamUploadData()

Streams file upload data to temporary file and moves to file destination.

Return Value

string

The temp file path.

Exceptions

HttpException

protected EntityConstraintViolationListInterface validate(FileInterface $file, array $validators)

Validates the file.

this method is unused in this class because file validation needs to be split up in 2 steps in ::handleFileUploadForField(). Add a deprecation notice as soon as a central core file upload service can be used in this class. See https://www.drupal.org/project/drupal/issues/2940383

Parameters

FileInterface $file

The file entity to validate.

array $validators

An array of upload validators to pass to file_validate().

Return Value

EntityConstraintViolationListInterface

The list of constraint violations, if any.

protected string prepareFilename(string $filename, array $validators)

Prepares the filename to strip out any malicious extensions.

Parameters

string $filename

The file name.

array $validators

The array of upload validators.

Return Value

string

The prepared/munged filename.

protected string getUploadLocation(array $settings)

Determines the URI for a file field.

Parameters

array $settings

The array of field settings.

Return Value

string

An un-sanitized file directory URI with tokens replaced. The result of the token replacement is then converted to plain text and returned.

protected array getUploadValidators(FieldDefinitionInterface $field_definition)

Retrieves the upload validators for a field definition.

This is copied from \Drupal\file\Plugin\Field\FieldType\FileItem as there is no entity instance available here that a FileItem would exist for.

Parameters

FieldDefinitionInterface $field_definition

The field definition for which to get validators.

Return Value

array

An array suitable for passing to file_save_upload() or the file field element's '#upload_validators' property.

static protected string generateLockIdFromFileUri(string $file_uri)

Generates a lock ID based on the file URI.

Parameters

string $file_uri

The file URI.

Return Value

string

The generated lock ID.