You are here

public function BrightcoveVideoController::ingestionCallback in Brightcove Video Connect 8.2

Same name and namespace in other branches
  1. 8 src/Controller/BrightcoveVideoController.php \Drupal\brightcove\Controller\BrightcoveVideoController::ingestionCallback()
  2. 3.x src/Controller/BrightcoveVideoController.php \Drupal\brightcove\Controller\BrightcoveVideoController::ingestionCallback()

Ingestion callback for Brightcove.

Parameters

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

int $token: The token which matches the video ID.

Return value

\Symfony\Component\HttpFoundation\Response An empty Response object.

1 string reference to 'BrightcoveVideoController::ingestionCallback'
brightcove.routing.yml in ./brightcove.routing.yml
brightcove.routing.yml

File

src/Controller/BrightcoveVideoController.php, line 160

Class

BrightcoveVideoController
Provides controller for video related callbacks.

Namespace

Drupal\brightcove\Controller

Code

public function ingestionCallback(Request $request, $token) {
  $status = BrightcoveUtil::runWithSemaphore(function () use ($request, $token) {
    $content = Json::decode($request
      ->getContent());
    if (json_last_error() !== JSON_ERROR_NONE) {
      throw new \Exception('Failed to decode JSON from response: ' . json_last_error_msg());
    }
    if (is_array($content) && $content['status'] == 'SUCCESS' && $content['version'] == 1 && $content['action'] == 'CREATE') {
      $video_id = $this->keyValueExpirable
        ->get('brightcove_callback')
        ->get($token);
      if (!empty($video_id)) {

        /** @var \Drupal\brightcove\Entity\BrightcoveVideo $video_entity */
        $video_entity = BrightcoveVideo::load($video_id);
        if (!is_null($video_entity)) {
          $cms = BrightcoveUtil::getCmsApi($video_entity
            ->getApiClient());
          switch ($content['entityType']) {

            // Video.
            case 'TITLE':

              // Delete video file from the entity.
              $video_entity
                ->setVideoFile(NULL);
              $video_entity
                ->save();
              break;

            // Assets.
            case 'ASSET':

              // Check for Text Track ID format. There is no other way to
              // determine whether the asset is a text track or something
              // else...
              if (preg_match('/^\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}$/i', $content['entity'])) {
                $text_tracks = $video_entity
                  ->getTextTracks();
                foreach ($text_tracks as $key => $text_track) {
                  if (!empty($text_track['target_id'])) {

                    /** @var \Drupal\brightcove\Entity\BrightcoveTextTrack $text_track_entity */
                    $text_track_entity = BrightcoveTextTrack::load($text_track['target_id']);
                    if (!is_null($text_track_entity) && empty($text_track_entity
                      ->getTextTrackId())) {

                      // Remove text track without Brightcove ID.
                      $text_track_entity
                        ->delete();

                      // Try to find the ingested text track on the video
                      // object and recreate it.
                      $cms = BrightcoveUtil::getCmsApi($video_entity
                        ->getApiClient());
                      $video = $cms
                        ->getVideo($video_entity
                        ->getBrightcoveId());
                      $api_text_tracks = $video
                        ->getTextTracks();
                      $found_api_text_track = NULL;
                      foreach ($api_text_tracks as $api_text_track) {
                        if ($api_text_track
                          ->getId() == $content['entity']) {
                          $found_api_text_track = $api_text_track;
                          break;
                        }
                      }

                      // Create new text track.
                      if (!is_null($found_api_text_track)) {
                        BrightcoveTextTrack::createOrUpdate($found_api_text_track, $this->textTrackStorage, $video_entity
                          ->id());
                      }

                      // We need to process only one per request.
                      break;
                    }
                  }
                }
              }
              else {
                $client = BrightcoveUtil::getClient($video_entity
                  ->getApiClient());
                $api_client = BrightcoveUtil::getApiClient($video_entity
                  ->getApiClient());

                // Check for each asset type by try-and-error, currently there
                // is no other way to determine which asset is what...
                $asset_types = [
                  BrightcoveVideo::IMAGE_TYPE_THUMBNAIL,
                  BrightcoveVideo::IMAGE_TYPE_POSTER,
                ];
                foreach ($asset_types as $type) {
                  try {
                    $client
                      ->request('GET', 1, 'cms', $api_client
                      ->getAccountId(), '/videos/' . $video_entity
                      ->getBrightcoveId() . '/assets/' . $type . '/' . $content['entity'], NULL);
                    switch ($type) {
                      case BrightcoveVideo::IMAGE_TYPE_THUMBNAIL:
                      case BrightcoveVideo::IMAGE_TYPE_POSTER:
                        $images = $cms
                          ->getVideoImages($video_entity
                          ->getBrightcoveId());
                        $function = ucfirst($type);

                        // @TODO: Download the image only if truly different.
                        // But this may not be possible without downloading
                        // the image.
                        $video_entity
                          ->saveImage($type, $images
                          ->{"get{$function}"}());
                        break 2;
                    }
                  } catch (APIException $e) {

                    // Do nothing here, just catch the exception to not
                    // generate an error.
                  }
                }
              }

              // @TODO: Do we have to do anything with the rest of the assets?
              break;

            // Don't do anything if we got something else.
            default:
              return NULL;
          }

          // Update video entity with the latest update date.
          $video = $cms
            ->getVideo($video_entity
            ->getBrightcoveId());
          $video_entity
            ->setChangedTime(strtotime($video
            ->getUpdatedAt()));
          $video_entity
            ->save();
        }
      }
    }
    return TRUE;
  }, $this->lock);
  return new Response('', $status === FALSE ? 409 : 200);
}