You are here

public function MediaController::upload in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 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 156

Class

MediaController
Returns responses for our image routes.

Namespace

Drupal\gutenberg\Controller

Code

public function upload(Request $request, ?Editor $editor) {
  $files = $request->files
    ->get('files', []);
  $uploaded_file = $files['fid'] ?? NULL;
  if (!$uploaded_file instanceof UploadedFile) {
    return new JsonResponse([
      'error' => $this
        ->t('Invalid file has been uploaded.'),
    ], 422);
  }
  try {
    return new JsonResponse($this->mediaService
      ->processMediaEntityUpload($uploaded_file, $editor));
  } catch (\Exception $exception) {
    return new JsonResponse([
      'error' => $this
        ->t($exception
        ->getMessage()),
    ], 500);
  }
}