You are here

public function MediaController::upload in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/Controller/MediaController.php \Drupal\gutenberg\Controller\MediaController::upload()

Upload files, save as file and media entity if possible.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Current request.

\Drupal\editor\Entity\Editor|null $editor: Editor entity instance.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\Exception

1 string reference to 'MediaController::upload'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/MediaController.php, line 177

Class

MediaController
Returns responses for our image routes.

Namespace

Drupal\gutenberg\Controller

Code

public function upload(Request $request, Editor $editor = NULL) {
  $files = $request->files
    ->get('files', []);
  $uploaded_file = $files['fid'] ?? NULL;
  if (!$uploaded_file instanceof UploadedFile) {
    throw new UnprocessableEntityHttpException('The uploaded file is invalid.');
  }
  try {
    return new JsonResponse($this->mediaService
      ->processMediaEntityUpload('fid', $uploaded_file, $editor));
  } catch (\Exception $exception) {

    // DefaultExceptionSubscriber::on4xx only normalizes 4xx client errors.
    return new JsonResponse($this
      ->getErrorResponse($exception
      ->getMessage()), JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
  }
}