You are here

public function MediaService::processMediaEntityUpload in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 src/Service/MediaService.php \Drupal\gutenberg\Service\MediaService::processMediaEntityUpload()

Save uploaded file, create file and media entity if possible.

Parameters

\Symfony\Component\HttpFoundation\File\UploadedFile $uploaded_file: Uploaded file instance.

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

Return value

mixed The media entity data.

Throws

\Drupal\gutenberg\Service\FileEntityNotSavedException

\Drupal\gutenberg\Service\MediaEntityNotSavedException

\Drupal\gutenberg\Service\MediaEntityNotMatchedException

File

src/Service/MediaService.php, line 246

Class

MediaService
The media service class.

Namespace

Drupal\gutenberg\Service

Code

public function processMediaEntityUpload(UploadedFile $uploaded_file, Editor $editor) {

  /** @var \Drupal\file\Entity\File $file_entity */
  if (!($file_entity = $this->mediaUploader
    ->upload($uploaded_file, $editor))) {
    throw new FileEntityNotSavedException();
  }
  if (!$this->moduleHandler
    ->moduleExists('media')) {
    return $this->entityDataProviderManager
      ->getData('file', $file_entity);
  }
  $mime_type_pieces = explode('/', $uploaded_file
    ->getClientMimeType());
  if (!($media_type = $this->mediaTypeGuesser
    ->guess(reset($mime_type_pieces), MediaTypeGuesserInterface::RETURN_NEGOTIATED))) {
    throw new MediaEntityNotMatchedException();
  }
  if (!($media_entity = $this->mediaTypePersisterManager
    ->save($media_type, $file_entity))) {
    throw new MediaEntityNotSavedException();
  }
  return $this->entityDataProviderManager
    ->getData('media', $file_entity, [
    'media_id' => $media_entity
      ->id(),
    'media_type' => $media_entity
      ->bundle(),
  ]);
}