public static function createThumbnailAssetFromFile(entry $entry, $filePath) { $fileLocation = tempnam(sys_get_temp_dir(), $entry->getId()); $res = KCurlWrapper::getDataFromFile($filePath, $fileLocation, kConf::get('thumb_size_limit')); if (!$res) { throw new Exception("thumbnail cannot be created from {$filePath} " . error_get_last()); } $thumbAsset = new thumbAsset(); $thumbAsset->setPartnerId($entry->getPartnerId()); $thumbAsset->setEntryId($entry->getId()); $thumbAsset->setStatus(thumbAsset::ASSET_STATUS_QUEUED); $thumbAsset->incrementVersion(); $thumbAsset->save(); $fileSyncKey = $thumbAsset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($fileLocation, $fileSyncKey); $finalPath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey); $ext = pathinfo($finalPath, PATHINFO_EXTENSION); $thumbAsset->setFileExt($ext); list($width, $height, $type, $attr) = getimagesize($finalPath); $thumbAsset->setWidth($width); $thumbAsset->setHeight($height); $thumbAsset->setSize(filesize($finalPath)); $thumbAsset->setStatus(thumbAsset::ASSET_STATUS_READY); $thumbAsset->save(); kBusinessConvertDL::setAsDefaultThumbAsset($thumbAsset); myNotificationMgr::createNotification(kNotificationJobData::NOTIFICATION_TYPE_ENTRY_UPDATE_THUMBNAIL, $entry); }
/** * decideThumbGenerate is the decision layer for a single thumbnail generation * * @param entry $entry * @param thumbParams $destThumbParams * @param BatchJob $parentJob * @return thumbAsset */ public static function decideThumbGenerate(entry $entry, thumbParams $destThumbParams, BatchJob $parentJob = null, $sourceAssetId = null, $runSync = false) { $srcAsset = null; assetPeer::resetInstanceCriteriaFilter(); if ($sourceAssetId) { $srcAsset = assetPeer::retrieveById($sourceAssetId); } else { if ($destThumbParams->getSourceParamsId()) { KalturaLog::debug("Look for flavor params [" . $destThumbParams->getSourceParamsId() . "]"); $srcAsset = assetPeer::retrieveByEntryIdAndParams($entry->getId(), $destThumbParams->getSourceParamsId()); } if (is_null($srcAsset)) { KalturaLog::debug("Look for original flavor"); $srcAsset = flavorAssetPeer::retrieveOriginalByEntryId($entry->getId()); } if (is_null($srcAsset) || $srcAsset->getStatus() != flavorAsset::FLAVOR_ASSET_STATUS_READY) { KalturaLog::debug("Look for highest bitrate flavor"); $srcAsset = flavorAssetPeer::retrieveHighestBitrateByEntryId($entry->getId()); } } if (is_null($srcAsset)) { throw new APIException(APIErrors::FLAVOR_ASSET_IS_NOT_READY); } $errDescription = null; $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($srcAsset->getId()); $destThumbParamsOutput = self::validateThumbAndMediaInfo($destThumbParams, $mediaInfo, $errDescription); $thumbAsset = thumbAssetPeer::retrieveByEntryIdAndParams($entry->getId(), $destThumbParams->getId()); if ($thumbAsset) { $description = $thumbAsset->getDescription() . "\n" . $errDescription; $thumbAsset->setDescription($description); } else { $thumbAsset = new thumbAsset(); $thumbAsset->setPartnerId($entry->getPartnerId()); $thumbAsset->setEntryId($entry->getId()); $thumbAsset->setDescription($errDescription); $thumbAsset->setFlavorParamsId($destThumbParams->getId()); } $thumbAsset->incrementVersion(); $thumbAsset->setStatus(flavorAsset::FLAVOR_ASSET_STATUS_CONVERTING); $thumbAsset->setTags($destThumbParamsOutput->getTags()); $thumbAsset->setFileExt($destThumbParamsOutput->getFileExt()); if (!$destThumbParamsOutput) { $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_ERROR); $thumbAsset->save(); return null; } $thumbAsset->save(); // save flavor params $destThumbParamsOutput->setPartnerId($entry->getPartnerId()); $destThumbParamsOutput->setEntryId($entry->getId()); $destThumbParamsOutput->setFlavorAssetId($thumbAsset->getId()); $destThumbParamsOutput->setFlavorAssetVersion($thumbAsset->getVersion()); $destThumbParamsOutput->save(); $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); $srcAssetType = $srcAsset->getType(); if (!$runSync) { $job = kJobsManager::addCapturaThumbJob($parentJob, $entry->getPartnerId(), $entry->getId(), $thumbAsset->getId(), $srcSyncKey, $srcAssetType, $destThumbParamsOutput); return $thumbAsset; } $errDescription = null; $capturedPath = self::generateThumbnail($srcAsset, $destThumbParamsOutput, $errDescription); // failed if (!$capturedPath) { $thumbAsset->incrementVersion(); $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_ERROR); $thumbAsset->setDescription($thumbAsset->getDescription() . "\n{$errDescription}"); $thumbAsset->save(); return $thumbAsset; } $thumbAsset->incrementVersion(); $thumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_READY); if (file_exists($capturedPath)) { list($width, $height, $type, $attr) = getimagesize($capturedPath); $thumbAsset->setWidth($width); $thumbAsset->setHeight($height); $thumbAsset->setSize(filesize($capturedPath)); } $logPath = $capturedPath . '.log'; if (file_exists($logPath)) { $thumbAsset->incLogFileVersion(); $thumbAsset->save(); // creats the file sync $logSyncKey = $thumbAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_CONVERT_LOG); kFileSyncUtils::moveFromFile($logPath, $logSyncKey); KalturaLog::debug("Log archived file to: " . kFileSyncUtils::getLocalFilePathForKey($logSyncKey)); } else { $thumbAsset->save(); } $syncKey = $thumbAsset->getSyncKey(thumbAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($capturedPath, $syncKey); KalturaLog::debug("Thumbnail archived file to: " . kFileSyncUtils::getLocalFilePathForKey($syncKey)); if ($thumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { // increment thumbnail version $entry->setThumbnail(".jpg"); $entry->save(); $entrySyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_THUMB); $syncFile = kFileSyncUtils::createSyncFileLinkForKey($entrySyncKey, $syncKey, false); if ($syncFile) { // removes the DEFAULT_THUMB tag from all other thumb assets $entryThumbAssets = thumbAssetPeer::retrieveByEntryId($thumbAsset->getEntryId()); foreach ($entryThumbAssets as $entryThumbAsset) { if ($entryThumbAsset->getId() == $thumbAsset->getId()) { continue; } if (!$entryThumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB)) { continue; } $entryThumbAsset->removeTags(array(thumbParams::TAG_DEFAULT_THUMB)); $entryThumbAsset->save(); } } } if (!is_null($thumbAsset->getFlavorParamsId())) { kFlowHelper::generateThumbnailsFromFlavor($thumbAsset->getEntryId(), null, $thumbAsset->getFlavorParamsId()); } return $thumbAsset; }
/** * @action addFromImage * @param string $entryId * @param file $fileData * @return KalturaThumbAsset * * @throws KalturaErrors::ENTRY_ID_NOT_FOUND * @validateUser entry entryId edit */ public function addFromImageAction($entryId, $fileData) { $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $ext = pathinfo($fileData["name"], PATHINFO_EXTENSION); $dbThumbAsset = new thumbAsset(); $dbThumbAsset->setPartnerId($dbEntry->getPartnerId()); $dbThumbAsset->setEntryId($dbEntry->getId()); $dbThumbAsset->setStatus(thumbAsset::ASSET_STATUS_QUEUED); $dbThumbAsset->setFileExt($ext); $dbThumbAsset->incrementVersion(); $dbThumbAsset->save(); $syncKey = $dbThumbAsset->getSyncKey(thumbAsset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($fileData["tmp_name"], $syncKey); $finalPath = kFileSyncUtils::getLocalFilePathForKey($syncKey); list($width, $height, $type, $attr) = getimagesize($finalPath); $dbThumbAsset->setWidth($width); $dbThumbAsset->setHeight($height); $dbThumbAsset->setSize(filesize($finalPath)); $dbThumbAsset->setStatus(thumbAsset::ASSET_STATUS_READY); $dbThumbAsset->save(); $dbEntryThumbs = assetPeer::retrieveThumbnailsByEntryId($entryId); //If the thums has the default tag or the entry is in no content and this is the first thumb if ($dbEntry->getCreateThumb() && ($dbThumbAsset->hasTag(thumbParams::TAG_DEFAULT_THUMB) || $dbEntry->getStatus() == KalturaEntryStatus::NO_CONTENT && count($dbEntryThumbs) == 1)) { $this->setAsDefaultAction($dbThumbAsset->getId()); } $thumbAssets = new KalturaThumbAsset(); $thumbAssets->fromObject($dbThumbAsset); return $thumbAssets; }
/** * @action addFromImage * @param string $entryId * @param file $fileData * @return KalturaThumbAsset * * @throws KalturaErrors::ENTRY_ID_NOT_FOUND */ public function addFromImageAction($entryId, $fileData) { $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $ext = pathinfo($fileData["name"], PATHINFO_EXTENSION); $dbThumbAsset = new thumbAsset(); $dbThumbAsset->setPartnerId($dbEntry->getPartnerId()); $dbThumbAsset->setEntryId($dbEntry->getId()); $dbThumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_QUEUED); $dbThumbAsset->setFileExt($ext); $dbThumbAsset->incrementVersion(); $dbThumbAsset->save(); $syncKey = $dbThumbAsset->getSyncKey(thumbAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($fileData["tmp_name"], $syncKey); $finalPath = kFileSyncUtils::getLocalFilePathForKey($syncKey); list($width, $height, $type, $attr) = getimagesize($finalPath); $dbThumbAsset->setWidth($width); $dbThumbAsset->setHeight($height); $dbThumbAsset->setSize(filesize($finalPath)); $dbThumbAsset->setStatus(thumbAsset::FLAVOR_ASSET_STATUS_READY); $dbThumbAsset->save(); $thumbAssets = new KalturaThumbAsset(); $thumbAssets->fromObject($dbThumbAsset); return $thumbAssets; }