function CKEditor5ImageController::upload
Same name in other branches
- 10 core/modules/ckeditor5/src/Controller/CKEditor5ImageController.php \Drupal\ckeditor5\Controller\CKEditor5ImageController::upload()
- 11.x core/modules/ckeditor5/src/Controller/CKEditor5ImageController.php \Drupal\ckeditor5\Controller\CKEditor5ImageController::upload()
Uploads and saves an image from a CKEditor 5 POST.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object.
Return value
\Symfony\Component\HttpFoundation\JsonResponse A JSON object including the file url.
Throws
\Symfony\Component\HttpKernel\Exception\HttpException Thrown when file system errors occur.
\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException Thrown when validation errors occur.
\Drupal\Core\Entity\EntityStorageException Thrown when file entity could not be saved.
1 string reference to 'CKEditor5ImageController::upload'
- ckeditor5.routing.yml in core/
modules/ ckeditor5/ ckeditor5.routing.yml - core/modules/ckeditor5/ckeditor5.routing.yml
File
-
core/
modules/ ckeditor5/ src/ Controller/ CKEditor5ImageController.php, line 125
Class
- CKEditor5ImageController
- Returns response for CKEditor 5 Simple image upload adapter.
Namespace
Drupal\ckeditor5\ControllerCode
public function upload(Request $request) {
// Getting the UploadedFile directly from the request.
$upload = $request->files
->get('upload');
$filename = $upload->getClientOriginalName();
$editor = $request->attributes
->get('editor');
$image_upload = $editor->getImageUploadSettings();
$destination = $image_upload['scheme'] . '://' . $image_upload['directory'];
// 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');
}
$max_filesize = min(Bytes::toNumber($image_upload['max_size']), Environment::getUploadMaxSize());
if (!empty($image_upload['max_dimensions']['width']) || !empty($image_upload['max_dimensions']['height'])) {
$max_dimensions = $image_upload['max_dimensions']['width'] . 'x' . $image_upload['max_dimensions']['height'];
}
else {
$max_dimensions = 0;
}
$validators = [
'file_validate_extensions' => [
'gif png jpg jpeg',
],
'file_validate_size' => [
$max_filesize,
],
'file_validate_image_resolution' => [
$max_dimensions,
],
];
$prepared_filename = $this->prepareFilename($filename, $validators);
// Create the file.
$file_uri = "{$destination}/{$prepared_filename}";
// Using the UploadedFile method instead of streamUploadData.
$temp_file_path = $upload->getRealPath();
$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($this->currentUser
->id());
$file->setFilename($prepared_filename);
if ($this->mimeTypeGuesser instanceof MimeTypeGuesserInterface) {
$file->setMimeType($this->mimeTypeGuesser
->guessMimeType($prepared_filename));
}
else {
$file->setMimeType($this->mimeTypeGuesser
->guess($prepared_filename));
@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->setFileUri($file_uri);
$file->setSize(@filesize($temp_file_path));
$violations = $this->validate($file, $validators);
if ($violations->count() > 0) {
throw new UnprocessableEntityHttpException($violations->__toString());
}
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 new JsonResponse([
'url' => $file->createFileUrl(),
'uuid' => $file->uuid(),
'entity_type' => $file->getEntityTypeId(),
], 201);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.