/** * This action delivers entry-related data, based on the user's context: access control, restriction, playback format and storage information. * @action getContextData * @param string $entryId * @param KalturaEntryContextDataParams $contextDataParams * @return KalturaEntryContextDataResult */ public function getContextData($entryId, KalturaEntryContextDataParams $contextDataParams) { $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $ks = $this->getKs(); $isAdmin = false; if ($ks) { $isAdmin = $ks->isAdmin(); } $accessControl = $dbEntry->getAccessControl(); /* @var $accessControl accessControl */ $result = new KalturaEntryContextDataResult(); $result->isAdmin = $isAdmin; $result->isScheduledNow = $dbEntry->isScheduledNow($contextDataParams->time); if ($dbEntry->getStartDate() && abs($dbEntry->getStartDate(null) - time()) <= 86400 || $dbEntry->getEndDate() && abs($dbEntry->getEndDate(null) - time()) <= 86400) { KalturaResponseCacher::setConditionalCacheExpiry(600); } if ($accessControl && $accessControl->hasRules()) { $disableCache = true; if (kConf::hasMap("optimized_playback")) { $partnerId = $accessControl->getPartnerId(); $optimizedPlayback = kConf::getMap("optimized_playback"); if (array_key_exists($partnerId, $optimizedPlayback)) { $params = $optimizedPlayback[$partnerId]; if (array_key_exists('cache_kdp_acccess_control', $params) && $params['cache_kdp_acccess_control']) { $disableCache = false; } } } $accessControlScope = $accessControl->getScope(); $contextDataParams->toObject($accessControlScope); $accessControlScope->setEntryId($entryId); $result->isAdmin = $accessControlScope->getKs() && $accessControlScope->getKs()->isAdmin(); $dbResult = new kEntryContextDataResult(); if ($accessControl->applyContext($dbResult) && $disableCache) { KalturaResponseCacher::disableCache(); } $result->fromObject($dbResult); } $partner = PartnerPeer::retrieveByPK($dbEntry->getPartnerId()); if (PermissionPeer::isValidForPartner(PermissionName::FEATURE_REMOTE_STORAGE_DELIVERY_PRIORITY, $dbEntry->getPartnerId()) && $partner->getStorageServePriority() != StorageProfile::STORAGE_SERVE_PRIORITY_KALTURA_ONLY) { if (is_null($contextDataParams->flavorAssetId)) { if ($contextDataParams->flavorTags) { $assets = assetPeer::retrieveReadyByEntryIdAndTag($entryId, $contextDataParams->flavorTags); $asset = reset($assets); } else { $asset = assetPeer::retrieveBestPlayByEntryId($entryId); } if (!$asset) { throw new KalturaAPIException(KalturaErrors::NO_FLAVORS_FOUND, $entryId); } } else { $asset = assetPeer::retrieveByPK($contextDataParams->flavorAssetId); if (!$asset) { throw new KalturaAPIException(KalturaErrors::FLAVOR_ASSET_ID_NOT_FOUND, $contextDataParams->flavorAssetId); } } if (!$asset) { throw new KalturaAPIException(KalturaErrors::FLAVOR_ASSET_ID_NOT_FOUND, $entryId); } $assetSyncKey = $asset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); $fileSyncs = kFileSyncUtils::getAllReadyExternalFileSyncsForKey($assetSyncKey); $storageProfilesXML = new SimpleXMLElement("<StorageProfiles/>"); foreach ($fileSyncs as $fileSync) { $storageProfileId = $fileSync->getDc(); $storageProfile = StorageProfilePeer::retrieveByPK($storageProfileId); if (!$storageProfile->getDeliveryRmpBaseUrl() && (!$contextDataParams->streamerType || $contextDataParams->streamerType == StorageProfile::PLAY_FORMAT_AUTO)) { $contextDataParams->streamerType = StorageProfile::PLAY_FORMAT_HTTP; $contextDataParams->mediaProtocol = StorageProfile::PLAY_FORMAT_HTTP; } $storageProfileXML = $storageProfilesXML->addChild("StorageProfile"); $storageProfileXML->addAttribute("storageProfileId", $storageProfileId); $storageProfileXML->addChild("Name", $storageProfile->getName()); $storageProfileXML->addChild("SystemName", $storageProfile->getSystemName()); } $result->storageProfilesXML = $storageProfilesXML->saveXML(); } if ($contextDataParams->streamerType && $contextDataParams->streamerType != StorageProfile::PLAY_FORMAT_AUTO) { $result->streamerType = $contextDataParams->streamerType; $result->mediaProtocol = $contextDataParams->mediaProtocol ? $contextDataParams->mediaProtocol : $contextDataParams->streamerType; } else { $result->streamerType = $this->getPartner()->getStreamerType(); if (!$result->streamerType) { $result->streamerType = StorageProfile::PLAY_FORMAT_HTTP; } $result->mediaProtocol = $this->getPartner()->getMediaProtocol(); if (!$result->mediaProtocol) { $result->mediaProtocol = StorageProfile::PLAY_FORMAT_HTTP; } } return $result; }
/** * Retrieve a single object by pkey. * * @param int $pk the primary key. * @param PropelPDO $con the connection to use * @return thumbAsset */ public static function retrieveByPK($pk, PropelPDO $con = null) { self::getInstance(); return parent::retrieveByPK($pk, $con); }
/** * @param kResource $resource */ protected function setEntryTypeByResource(entry $dbEntry, kResource $resource) { $fullPath = null; switch ($resource->getType()) { case 'kAssetParamsResourceContainer': return $this->setEntryTypeByResource($dbEntry, $resource->getResource()); case 'kAssetsParamsResourceContainers': return $this->setEntryTypeByResource($dbEntry, reset($resource->getResources())); case 'kFileSyncResource': $sourceEntry = null; if ($resource->getFileSyncObjectType() == FileSyncObjectType::ENTRY) { $sourceEntry = entryPeer::retrieveByPK($resource->getObjectId()); } if ($resource->getFileSyncObjectType() == FileSyncObjectType::FLAVOR_ASSET) { $sourceAsset = assetPeer::retrieveByPK($resource->getObjectId()); if ($sourceAsset) { $sourceEntry = $sourceAsset->getentry(); } } if ($sourceEntry) { $dbEntry->setType($sourceEntry->getType()); $dbEntry->setMediaType($sourceEntry->getMediaType()); } return; case 'kLocalFileResource': $fullPath = $resource->getLocalFilePath(); break; case 'kUrlResource': case 'kRemoteStorageResource': $fullPath = $resource->getUrl(); break; default: return; } if ($fullPath) { $this->setEntryTypeByExtension($dbEntry, $fullPath); } }
/** * @param KalturaBatchJob $job * @param KalturaPostConvertJobData $data * @return KalturaBatchJob */ private function postConvert(KalturaBatchJob $job, KalturaPostConvertJobData $data) { DbManager::setConfig(kConf::getDB()); DbManager::initialize(); $flavorAsset = assetPeer::retrieveByPK($data->flavorAssetId); if (!empty($flavorAsset) && $this->checkMediaInfoExists($data->flavorAssetId) && $flavorAsset->getIsOriginal() && $flavorAsset->getSize() !== 0) { $data->createThumb = false; return $this->closeJob($job, null, null, 'Media info already exists', KalturaBatchJobStatus::FINISHED, $data); } if ($data->flavorParamsOutputId) { $data->flavorParamsOutput = $this->kClient->flavorParamsOutput->get($data->flavorParamsOutputId); } try { $mediaFile = trim($data->srcFileSyncLocalPath); $mediaFileExt = pathinfo($mediaFile, PATHINFO_EXTENSION); if (!is_null($data->srcFileSyncWamsAssetId)) { $tempFile = kWAMS::getInstance($job->partnerId)->createTempFileForAssetId($data->srcFileSyncWamsAssetId, $mediaFileExt); if (file_exists($tempFile)) { $mediaFile = $tempFile; } } if (!$data->flavorParamsOutput || !$data->flavorParamsOutput->sourceRemoteStorageProfileId) { if (!$this->pollingFileExists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } } KalturaLog::debug("mediaFile [{$mediaFile}]"); $this->updateJob($job, "Extracting file media info on {$mediaFile}", KalturaBatchJobStatus::QUEUED, 1); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } $mediaInfo = null; try { $engine = KBaseMediaParser::getParser($job->jobSubType, realpath($mediaFile), $this->taskConfig, $job, $data->srcFileSyncWamsAssetId); if ($engine) { KalturaLog::info("Media info engine [" . get_class($engine) . "]"); $mediaInfo = $engine->getMediaInfo(); if (!empty($data->srcFileSyncWamsAssetId)) { kWAMS::getInstance($job->partnerId)->deleteTempFileForAssetId($data->srcFileSyncWamsAssetId, $mediaFileExt); } } else { $err = "Media info engine not found for job subtype [" . $job->jobSubType . "]"; KalturaLog::info($err); return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::ENGINE_NOT_FOUND, $err, KalturaBatchJobStatus::FAILED); } } catch (Exception $ex) { KalturaLog::err("Error: " . $ex->getMessage()); $mediaInfo = null; } /* @var $mediaInfo KalturaMediaInfo */ if (is_null($mediaInfo)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::EXTRACT_MEDIA_FAILED, "Failed to extract media info: {$mediaFile}", KalturaBatchJobStatus::FAILED); } try { $mediaInfo->flavorAssetId = $data->flavorAssetId; $createdMediaInfo = $this->getClient()->batch->addMediaInfo($mediaInfo); if ($createdMediaInfo instanceof KalturaMediaInfo) { $mediaInfo = $createdMediaInfo; } // must save the mediaInfoId before reporting that the task is finished $this->updateJob($job, "Saving media info id {$createdMediaInfo->id}", KalturaBatchJobStatus::PROCESSED, 50, $data); $data->thumbPath = null; if (!$data->createThumb) { return $this->closeJob($job, null, null, "Media info id {$createdMediaInfo->id} saved", KalturaBatchJobStatus::FINISHED, $data); } // creates a temp file path $rootPath = $this->taskConfig->params->localTempPath; $this->createDir($rootPath); // creates the path $uniqid = uniqid('thumb_'); $thumbPath = realpath($rootPath) . "/{$uniqid}"; $videoDurationSec = floor($mediaInfo->videoDuration / 1000); $data->thumbOffset = max(0, min($data->thumbOffset, $videoDurationSec)); if ($mediaInfo->videoHeight) { $data->thumbHeight = $mediaInfo->videoHeight; } if ($mediaInfo->videoBitRate) { $data->thumbBitrate = $mediaInfo->videoBitRate; } // generates the thumbnail if (!is_null($data->srcFileSyncWamsAssetId)) { $thumbMaker = new KWAMSThumbnailMaker($data->srcFileSyncWamsAssetId, $thumbPath); $created = $thumbMaker->createThumbnail($data->thumbOffset, $mediaInfo->videoWidth, $mediaInfo->videoHeight, $mediaInfo->videoDar); } else { $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $thumbPath, $this->taskConfig->params->FFMpegCmd); $created = $thumbMaker->createThumnail($data->thumbOffset, $mediaInfo->videoWidth, $mediaInfo->videoHeight, null, null, $mediaInfo->videoDar); } if (!$created || !file_exists($thumbPath)) { $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, 'Thumbnail not created', KalturaBatchJobStatus::FINISHED, $data); } $data->thumbPath = $thumbPath; $job = $this->moveFile($job, $data); if ($this->checkFileExists($job->data->thumbPath)) { return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $data); } $data->createThumb = false; return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FINISHED, $data); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } }