function FileUpload::handleFileUploadForResource
Same name in other branches
- 11.x core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::handleFileUploadForResource()
Handles JSON:API file upload requests.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The HTTP request object.
\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type for the current request.
string $file_field_name: The file field for which the file is to be uploaded.
\Drupal\Core\Entity\FieldableEntityInterface|null $entity: (optional) The entity for which the file is to be uploaded.
Return value
\Drupal\file\Upload\FileUploadResult The file upload result.
2 calls to FileUpload::handleFileUploadForResource()
- FileUpload::handleFileUploadForExistingResource in core/
modules/ jsonapi/ src/ Controller/ FileUpload.php - Handles JSON:API file upload requests.
- FileUpload::handleFileUploadForNewResource in core/
modules/ jsonapi/ src/ Controller/ FileUpload.php - Handles JSON:API file upload requests.
File
-
core/
modules/ jsonapi/ src/ Controller/ FileUpload.php, line 188
Class
- FileUpload
- Handles file upload requests.
Namespace
Drupal\jsonapi\ControllerCode
protected function handleFileUploadForResource(Request $request, ResourceType $resource_type, string $file_field_name, ?FieldableEntityInterface $entity = NULL) : FileUploadResult {
$file_field_name = $resource_type->getInternalName($file_field_name);
$field_definition = $this->validateAndLoadFieldDefinition($resource_type->getEntityTypeId(), $resource_type->getBundle(), $file_field_name);
static::ensureFileUploadAccess($this->currentUser, $field_definition, $entity);
$filename = ContentDispositionFilenameParser::parseFilename($request);
$tempPath = $this->inputStreamFileWriter
->writeStreamToFile();
$uploadedFile = new InputStreamUploadedFile($filename, $filename, $tempPath, @filesize($tempPath));
$settings = $field_definition->getSettings();
$validators = $this->getFileUploadValidators($settings);
if (!array_key_exists('FileExtension', $validators) && $settings['file_extensions'] === '') {
// An empty string means 'all file extensions' but the FileUploadHandler
// needs the FileExtension entry to be present and empty in order for this
// to be respected. An empty array means 'all file extensions'.
// @see \Drupal\file\Upload\FileUploadHandler::handleExtensionValidation
$validators['FileExtension'] = [];
}
$destination = $this->getUploadLocation($field_definition);
// 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');
}
try {
$result = $this->fileUploadHandler
->handleFileUpload($uploadedFile, $validators, $destination, FileExists::Rename, FALSE);
} catch (LockAcquiringException $e) {
throw new HttpException(503, $e->getMessage(), NULL, [
'Retry-After' => 1,
]);
} catch (UploadException $e) {
throw new HttpException(500, 'Input file data could not be read', $e);
} catch (CannotWriteFileException $e) {
throw new HttpException(500, 'Temporary file data could not be written', $e);
} catch (NoFileException $e) {
throw new HttpException(500, 'Temporary file could not be opened', $e);
} catch (FileExistsException $e) {
throw new HttpException(500, $e->getMessage(), $e);
} catch (FileException $e) {
throw new HttpException(500, 'Temporary file could not be moved to file location');
}
if ($result->hasViolations()) {
$message = "Unprocessable Entity: file validation failed.\n";
$message .= implode("\n", array_map(function (ConstraintViolationInterface $violation) {
return PlainTextOutput::renderFromHtml($violation->getMessage());
}, (array) $result->getViolations()
->getIterator()));
throw new UnprocessableEntityHttpException($message);
}
return $result;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.