function CKEditor5ImageController::upload
Same name in other branches
- 9 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.
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 144
Class
- CKEditor5ImageController
- Returns response for CKEditor 5 Simple image upload adapter.
Namespace
Drupal\ckeditor5\ControllerCode
public function upload(Request $request) : Response {
// Getting the UploadedFile directly from the request.
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile|null $upload */
$upload = $request->files
->get('upload');
if ($upload === NULL || !$upload->isValid()) {
throw new HttpException(500, $upload?->getErrorMessage() ?: 'Invalid file upload');
}
$filename = $upload->getClientOriginalName();
/** @var \Drupal\editor\EditorInterface $editor */
$editor = $request->attributes
->get('editor');
$settings = $editor->getImageUploadSettings();
$destination = $settings['scheme'] . '://' . $settings['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');
}
$validators = $this->getImageUploadValidators($settings);
$file_uri = "{$destination}/{$filename}";
$file_uri = $this->fileSystem
->getDestinationFilename($file_uri, FileExists::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,
]);
}
try {
$uploadedFile = new FormUploadedFile($upload);
$uploadResult = $this->fileUploadHandler
->handleFileUpload($uploadedFile, $validators, $destination, FileExists::Rename, FALSE);
if ($uploadResult->hasViolations()) {
throw new UnprocessableEntityHttpException((string) $uploadResult->getViolations());
}
} catch (FileException $e) {
throw new HttpException(500, 'File could not be saved');
} catch (LockAcquiringException $e) {
throw new HttpException(503, sprintf('File "%s" is already locked for writing.', $upload->getClientOriginalName()), NULL, [
'Retry-After' => 1,
]);
}
$this->lock
->release($lock_id);
$file = $uploadResult->getFile();
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.