/** * @param BatchJob $dbBatchJob * @param kImportJobData $data * @param BatchJob $twinJob * @return BatchJob */ public static function handleImportFinished(BatchJob $dbBatchJob, kImportJobData $data, BatchJob $twinJob = null) { KalturaLog::debug("Import finished, with file: " . $data->getDestFileLocalPath()); if ($dbBatchJob->getAbort()) { return $dbBatchJob; } if (!$twinJob) { if (!file_exists($data->getDestFileLocalPath())) { throw new APIException(APIErrors::INVALID_FILE_NAME, $data->getDestFileLocalPath()); } } $msg = null; $flavorAsset = kFlowHelper::createOriginalFlavorAsset($dbBatchJob->getPartnerId(), $dbBatchJob->getEntryId(), $msg); if (!$flavorAsset) { KalturaLog::err("Flavor asset not created for entry [" . $dbBatchJob->getEntryId() . "]"); kBatchManager::updateEntry($dbBatchJob, entryStatus::ERROR_CONVERTING); $dbBatchJob->setMessage($msg); $dbBatchJob->setDescription($dbBatchJob->getDescription() . "\n" . $msg); return $dbBatchJob; } $syncKey = null; if ($twinJob) { $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); // copy file sync $twinData = $twinJob->getData(); if ($twinData instanceof kImportJobData) { $twinFlavorAsset = flavorAssetPeer::retrieveById($twinData->getFlavorAssetId()); if ($twinFlavorAsset) { $twinSyncKey = $twinFlavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); if ($twinSyncKey && kFileSyncUtils::file_exists($twinSyncKey)) { kFileSyncUtils::softCopy($twinSyncKey, $syncKey); } } } } else { $ext = pathinfo($data->getDestFileLocalPath(), PATHINFO_EXTENSION); KalturaLog::info("Imported file extension: {$ext}"); $flavorAsset->setFileExt($ext); $flavorAsset->save(); $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey); } // set the path in the job data $data->setDestFileLocalPath(kFileSyncUtils::getLocalFilePathForKey($syncKey)); $data->setFlavorAssetId($flavorAsset->getId()); $dbBatchJob->setData($data); $dbBatchJob->save(); kEventsManager::raiseEvent(new kObjectAddedEvent($flavorAsset)); return $dbBatchJob; }
/** * @param EntryDistribution $entryDistribution * @param DistributionProfile $distributionProfile * @param int $dc * @return bool true if the job could be created */ protected static function prepareDistributionJob(EntryDistribution $entryDistribution, DistributionProfile $distributionProfile, &$dc) { // prepare ids list of all the assets $assetIds = explode(',', implode(',', array($entryDistribution->getThumbAssetIds(), $entryDistribution->getFlavorAssetIds()))); $assets = assetPeer::retrieveByIds($assetIds); $assetObjects = array(); foreach ($assets as $asset) { /* @var $asset asset */ $assetObjects[$asset->getId()] = array('asset' => $asset, 'downloadUrl' => null); } // lists all files from all assets $c = new Criteria(); $c->add(FileSyncPeer::OBJECT_TYPE, FileSyncObjectType::ASSET); $c->add(FileSyncPeer::OBJECT_SUB_TYPE, asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); $c->add(FileSyncPeer::OBJECT_ID, $assetIds, Criteria::IN); $c->add(FileSyncPeer::PARTNER_ID, $entryDistribution->getPartnerId()); $c->add(FileSyncPeer::STATUS, FileSync::FILE_SYNC_STATUS_READY); $fileSyncs = FileSyncPeer::doSelect($c); $dcs = array(); foreach ($fileSyncs as $fileSync) { /* @var $fileSync FileSync */ $assetId = $fileSync->getObjectId(); if (!isset($assetObjects[$assetId])) { // the object is not in the list of assets continue; } $asset = $assetObjects[$assetId]['asset']; /* @var $asset asset */ if ($asset->getVersion() != $fileSync->getVersion()) { // the file sync is not of the current asset version continue; } $fileSync = kFileSyncUtils::resolve($fileSync); // use the best URL as the source for download in case it will be needed if ($fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) { if (!is_null($assetObjects[$assetId]['downloadUrl']) && $fileSync->getDc() != $distributionProfile->getRecommendedStorageProfileForDownload()) { continue; } $downloadUrl = $fileSync->getExternalUrl($entryDistribution->getEntryId()); if (!$downloadUrl) { continue; } $assetObjects[$assetId]['downloadUrl'] = $downloadUrl; continue; } // populates the list of files in each dc $fileSyncDc = $fileSync->getDc(); if (!isset($dcs[$fileSyncDc])) { $dcs[$fileSyncDc] = array(); } $dcs[$fileSyncDc][$assetId] = $fileSync->getId(); } if (isset($dcs[$dc]) && count($dcs[$dc]) == count($assets)) { KalturaLog::debug("All files exist in the preferred dc [{$dc}]"); return true; } // check if all files exist on any of the remote dcs $otherDcs = kDataCenterMgr::getAllDcs(true); foreach ($otherDcs as $remoteDc) { $remoteDcId = $remoteDc['id']; if (!isset($dcs[$remoteDcId]) && count($dcs[$remoteDcId]) != count($assets)) { continue; } $dc = $remoteDcId; KalturaLog::debug("All files exist in none-preferred dc [{$dc}]"); return true; } if ($entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_SUBMITTING || $entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_UPDATING) { KalturaLog::debug("Entry distribution already importing"); return false; } // create all needed import jobs $destinationDc = $distributionProfile->getRecommendedDcForDownload(); $dcExistingFiles = $dcs[$destinationDc]; foreach ($assetObjects as $assetId => $assetObject) { if (is_null($assetObject['downloadUrl'])) { KalturaLog::debug("Download URL not found for asset [{$assetId}]"); continue; } $asset = $assetObject['asset']; /* @var $asset asset */ if (isset($dcExistingFiles[$assetId])) { continue; } $jobData = new kImportJobData(); $jobData->setCacheOnly(true); self::addImportJob($destinationDc, $assetObject['downloadUrl'], $asset); } return false; }
public static function addImportJob(BatchJob $parentJob = null, $entryId, $partnerId, $entryUrl, asset $asset = null, $subType = null, kImportJobData $jobData = null, $keepCurrentVersion = false) { $entryUrl = str_replace('//', '/', $entryUrl); $entryUrl = preg_replace('/^((https?)|(ftp)|(scp)|(sftp)):\\//', '$1://', $entryUrl); if (is_null($subType)) { if (stripos($entryUrl, 'sftp:') === 0) { $subType = kFileTransferMgrType::SFTP; } elseif (stripos($entryUrl, 'scp:') === 0) { $subType = kFileTransferMgrType::SCP; } elseif (stripos($entryUrl, 'ftp:') === 0) { $subType = kFileTransferMgrType::FTP; } elseif (stripos($entryUrl, 'https:') === 0) { $subType = kFileTransferMgrType::HTTPS; } else { $subType = kFileTransferMgrType::HTTP; } } if (!$jobData) { $jobData = new kImportJobData(); } $jobData->setSrcFileUrl($entryUrl); if ($asset) { if ($keepCurrentVersion) { if (!$asset->isLocalReadyStatus()) { $asset->setStatus(asset::FLAVOR_ASSET_STATUS_IMPORTING); } } else { $asset->incrementVersion(); $asset->setStatus(asset::FLAVOR_ASSET_STATUS_IMPORTING); } $asset->save(); $jobData->setFlavorAssetId($asset->getId()); } $entry = entryPeer::retrieveByPK($entryId); if ($entry) { $higherStatuses = array(entryStatus::PRECONVERT, entryStatus::READY); if (!in_array($entry->getStatus(), $higherStatuses)) { $entry->setStatus(entryStatus::IMPORT); $entry->save(); } } $batchJob = null; if ($parentJob) { $batchJob = $parentJob->createChild(BatchJobType::IMPORT, $subType); } else { $batchJob = new BatchJob(); $batchJob->setEntryId($entryId); $batchJob->setPartnerId($partnerId); } $batchJob->setObjectId($jobData->getFlavorAssetId()); $batchJob->setObjectType(BatchJobObjectType::ASSET); return self::addJob($batchJob, $jobData, BatchJobType::IMPORT, $subType); }
/** * @param BatchJob $dbBatchJob * @param kImportJobData $data * @return BatchJob */ public static function handleImportFinished(BatchJob $dbBatchJob, kImportJobData $data) { if ($dbBatchJob->getExecutionStatus() == BatchJobExecutionStatus::ABORTED) { return $dbBatchJob; } if (!file_exists($data->getDestFileLocalPath())) { throw new APIException(APIErrors::INVALID_FILE_NAME, $data->getDestFileLocalPath()); } // get entry $entryId = $dbBatchJob->getEntryId(); $dbEntry = entryPeer::retrieveByPKNoFilter($entryId); // IMAGE media entries if ($dbEntry->getType() == entryType::MEDIA_CLIP && $dbEntry->getMediaType() == entry::ENTRY_MEDIA_TYPE_IMAGE) { //setting the entry's data so it can be used for creating file-syncs' file-path version & extension - in kFileSyncUtils::moveFromFile //without saving - the updated entry object exists in the instance pool $dbEntry->setData(".jpg"); $syncKey = $dbEntry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA); try { kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); } catch (Exception $e) { if ($dbEntry->getStatus() == entryStatus::NO_CONTENT) { $dbEntry->setStatus(entryStatus::ERROR_CONVERTING); $dbEntry->save(); } throw $e; } $dbEntry->setStatus(entryStatus::READY); $dbEntry->save(); return $dbBatchJob; } $flavorAsset = null; if ($data->getFlavorAssetId()) { $flavorAsset = assetPeer::retrieveById($data->getFlavorAssetId()); } $isNewFlavor = false; if (!$flavorAsset) { $msg = null; $flavorAsset = kFlowHelper::createOriginalFlavorAsset($dbBatchJob->getPartnerId(), $dbBatchJob->getEntryId(), $msg); if (!$flavorAsset) { KalturaLog::err("Flavor asset not created for entry [" . $dbBatchJob->getEntryId() . "]"); kBatchManager::updateEntry($dbBatchJob->getEntryId(), entryStatus::ERROR_CONVERTING); $dbBatchJob->setMessage($msg); $dbBatchJob->setDescription($dbBatchJob->getDescription() . "\n" . $msg); return $dbBatchJob; } $isNewFlavor = true; } $isNewContent = true; $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); if (kFileSyncUtils::fileSync_exists($syncKey)) { $isNewContent = false; } $ext = pathinfo($data->getDestFileLocalPath(), PATHINFO_EXTENSION); KalturaLog::info("Imported file extension: {$ext}"); if (!$flavorAsset->getVersion()) { $flavorAsset->incrementVersion(); } if ($ext) { $flavorAsset->setFileExt($ext); } if ($flavorAsset instanceof thumbAsset) { list($width, $height, $type, $attr) = getimagesize($data->getDestFileLocalPath()); $flavorAsset->setWidth($width); $flavorAsset->setHeight($height); $flavorAsset->setSize(filesize($data->getDestFileLocalPath())); } $flavorAsset->save(); $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); // set the path in the job data $localFilePath = kFileSyncUtils::getLocalFilePathForKey($syncKey); $data->setDestFileLocalPath($localFilePath); $data->setFlavorAssetId($flavorAsset->getId()); $dbBatchJob->setData($data); $dbBatchJob->save(); $convertProfileExist = self::activateConvertProfileJob($dbBatchJob->getEntryId(), $localFilePath); if (($isNewContent || $dbEntry->getStatus() == entryStatus::IMPORT) && !$convertProfileExist) { // check if status == import for importing file of type url (filesync exists, and we want to raise event for conversion profile to start) kEventsManager::raiseEvent(new kObjectAddedEvent($flavorAsset, $dbBatchJob)); } if (!$isNewFlavor) { $entryFlavors = assetPeer::retrieveByEntryIdAndStatus($flavorAsset->getEntryId(), flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT); $originalFlavorAsset = assetPeer::retrieveOriginalByEntryId($flavorAsset->getEntryId()); foreach ($entryFlavors as $entryFlavor) { /* @var $entryFlavor flavorAsset */ if ($entryFlavor->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT && $entryFlavor->getFlavorParamsId()) { $flavor = assetParamsOutputPeer::retrieveByAsset($entryFlavor); kBusinessPreConvertDL::decideFlavorConvert($entryFlavor, $flavor, $originalFlavorAsset, null, null, $dbBatchJob); } } $entryThumbnails = assetPeer::retrieveThumbnailsByEntryId($flavorAsset->getEntryId()); foreach ($entryThumbnails as $entryThumbnail) { /* @var $entryThumbnail thumbAsset */ if ($entryThumbnail->getStatus() != asset::ASSET_STATUS_WAIT_FOR_CONVERT || !$entryThumbnail->getFlavorParamsId()) { continue; } $thumbParamsOutput = assetParamsOutputPeer::retrieveByAssetId($entryThumbnail->getId()); /* @var $thumbParamsOutput thumbParamsOutput */ if ($thumbParamsOutput->getSourceParamsId() != $flavorAsset->getFlavorParamsId()) { continue; } $srcSyncKey = $flavorAsset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); $srcAssetType = $flavorAsset->getType(); kJobsManager::addCapturaThumbJob($entryThumbnail->getPartnerId(), $entryThumbnail->getEntryId(), $entryThumbnail->getId(), $srcSyncKey, $flavorAsset->getId(), $srcAssetType, $thumbParamsOutput); } } return $dbBatchJob; }
public static function addImportJob(BatchJob $parentJob = null, $entryId, $partnerId, $entryUrl) { $jobData = new kImportJobData(); $jobData->setSrcFileUrl($entryUrl); $batchJob = null; if ($parentJob) { $batchJob = $parentJob->createChild(); } else { $batchJob = new BatchJob(); $batchJob->setEntryId($entryId); $batchJob->setPartnerId($partnerId); } return self::addJob($batchJob, $jobData, BatchJobType::IMPORT); }
/** * @param BatchJob $dbBatchJob * @param kImportJobData $data * @param BatchJob $twinJob * @return BatchJob */ public static function handleImportFinished(BatchJob $dbBatchJob, kImportJobData $data, BatchJob $twinJob = null) { KalturaLog::debug("Import finished, with file: " . $data->getDestFileLocalPath()); if ($dbBatchJob->getAbort()) { return $dbBatchJob; } if (!$twinJob) { if (!file_exists($data->getDestFileLocalPath())) { throw new APIException(APIErrors::INVALID_FILE_NAME, $data->getDestFileLocalPath()); } } // get entry $entryId = $dbBatchJob->getEntryId(); $dbEntry = entryPeer::retrieveByPKNoFilter($entryId); // IMAGE media entries if ($dbEntry->getType() == entryType::MEDIA_CLIP && $dbEntry->getMediaType() == entry::ENTRY_MEDIA_TYPE_IMAGE) { $syncKey = $dbEntry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA); try { kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); } catch (Exception $e) { if ($dbEntry->getStatus() == entryStatus::NO_CONTENT) { $dbEntry->setStatus(entryStatus::ERROR_CONVERTING); $dbEntry->save(); } throw $e; } $dbEntry->setStatus(entryStatus::READY); $dbEntry->save(); return $dbBatchJob; } $flavorAsset = null; if ($data->getFlavorAssetId()) { $flavorAsset = assetPeer::retrieveById($data->getFlavorAssetId()); } $isNewFlavor = false; if (!$flavorAsset) { $msg = null; $flavorAsset = kFlowHelper::createOriginalFlavorAsset($dbBatchJob->getPartnerId(), $dbBatchJob->getEntryId(), $msg); if (!$flavorAsset) { KalturaLog::err("Flavor asset not created for entry [" . $dbBatchJob->getEntryId() . "]"); kBatchManager::updateEntry($dbBatchJob->getEntryId(), entryStatus::ERROR_CONVERTING); $dbBatchJob->setMessage($msg); $dbBatchJob->setDescription($dbBatchJob->getDescription() . "\n" . $msg); return $dbBatchJob; } $isNewFlavor = true; } $isNewContent = true; $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); if (kFileSyncUtils::fileSync_exists($syncKey)) { $isNewContent = false; } if ($twinJob) { $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); // copy file sync $twinData = $twinJob->getData(); if ($twinData instanceof kImportJobData) { $twinFlavorAsset = assetPeer::retrieveById($twinData->getFlavorAssetId()); if ($twinFlavorAsset) { $twinSyncKey = $twinFlavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); if ($twinSyncKey && kFileSyncUtils::file_exists($twinSyncKey)) { kFileSyncUtils::softCopy($twinSyncKey, $syncKey); } } } } else { $ext = pathinfo($data->getDestFileLocalPath(), PATHINFO_EXTENSION); KalturaLog::info("Imported file extension: {$ext}"); if (!$flavorAsset->getVersion()) { $flavorAsset->incrementVersion(); } $flavorAsset->setFileExt($ext); $flavorAsset->save(); $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); kFileSyncUtils::moveFromFile($data->getDestFileLocalPath(), $syncKey, true, false, $data->getCacheOnly()); } // set the path in the job data $localFilePath = kFileSyncUtils::getLocalFilePathForKey($syncKey); $data->setDestFileLocalPath($localFilePath); $data->setFlavorAssetId($flavorAsset->getId()); $dbBatchJob->setData($data); $dbBatchJob->save(); if ($isNewContent || $dbEntry->getStatus() == entryStatus::IMPORT) { // check if status == import for importing file of type url (filesync exists, and we want to raise event for conversion profile to start) kEventsManager::raiseEvent(new kObjectAddedEvent($flavorAsset, $dbBatchJob)); } if (!$isNewFlavor && $flavorAsset->getIsOriginal()) { $entryFlavors = assetPeer::retrieveFlavorsByEntryId($flavorAsset->getEntryId()); foreach ($entryFlavors as $entryFlavor) { /* @var $entryFlavor flavorAsset */ if ($entryFlavor->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT && $entryFlavor->getFlavorParamsId()) { kBusinessPreConvertDL::decideAddEntryFlavor($dbBatchJob, $flavorAsset->getEntryId(), $entryFlavor->getFlavorParamsId()); } } $entryThumbnails = assetPeer::retrieveThumbnailsByEntryId($flavorAsset->getEntryId()); foreach ($entryThumbnails as $entryThumbnail) { /* @var $entryThumbnail thumbAsset */ if ($entryThumbnail->getStatus() != asset::ASSET_STATUS_WAIT_FOR_CONVERT || !$entryThumbnail->getFlavorParamsId()) { continue; } $thumbParamsOutput = assetParamsOutputPeer::retrieveByAssetId($entryThumbnail->getId()); /* @var $thumbParamsOutput thumbParamsOutput */ if ($thumbParamsOutput->getSourceParamsId() != $flavorAsset->getFlavorParamsId()) { continue; } $srcSyncKey = $flavorAsset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET); $srcAssetType = $flavorAsset->getType(); kJobsManager::addCapturaThumbJob($entryThumbnail->getPartnerId(), $entryThumbnail->getEntryId(), $entryThumbnail->getId(), $srcSyncKey, $flavorAsset->getId(), $srcAssetType, $thumbParamsOutput); } } return $dbBatchJob; }