Esempio n. 1
0
 /**
  * @param BatchJob $dbBatchJob
  * @param kConvertCollectionJobData $data
  * @param BatchJob $twinJob
  * @return BatchJob
  */
 public static function handleConvertCollectionFinished(BatchJob $dbBatchJob, kConvertCollectionJobData $data, BatchJob $twinJob = null)
 {
     KalturaLog::debug("Convert Collection finished for entry id: " . $dbBatchJob->getEntryId());
     if ($dbBatchJob->getAbort()) {
         return $dbBatchJob;
     }
     $entry = $dbBatchJob->getEntry();
     if (!$entry) {
         throw new APIException(APIErrors::INVALID_ENTRY, $dbBatchJob, $dbBatchJob->getEntryId());
     }
     $ismPath = $data->getDestDirLocalPath() . DIRECTORY_SEPARATOR . $data->getDestFileName() . '.ism';
     $ismcPath = $data->getDestDirLocalPath() . DIRECTORY_SEPARATOR . $data->getDestFileName() . '.ismc';
     $logPath = $data->getDestDirLocalPath() . DIRECTORY_SEPARATOR . $data->getDestFileName() . '.log';
     $thumbPath = $data->getDestDirLocalPath() . DIRECTORY_SEPARATOR . $data->getDestFileName() . '_Thumb.jpg';
     $ismContent = file_get_contents($ismPath);
     $offset = $entry->getThumbOffset();
     // entry getThumbOffset now takes the partner DefThumbOffset into consideration
     $finalFlavors = array();
     $addedFlavorParamsOutputsIds = array();
     foreach ($data->getFlavors() as $flavor) {
         // verifies that flavor asset created
         if (!$flavor->getFlavorAssetId()) {
             throw new APIException(APIErrors::INVALID_FLAVOR_ASSET_ID, $data->getFlavorAssetId());
         }
         $flavorAsset = flavorAssetPeer::retrieveById($flavor->getFlavorAssetId());
         // verifies that flavor asset exists
         if (!$flavorAsset) {
             throw new APIException(APIErrors::INVALID_FLAVOR_ASSET_ID, $flavor->getFlavorAssetId());
         }
         // increment flavor asset version (for file sync)
         $flavorAsset->incrementVersion();
         $flavorAsset->save();
         // syncing the media file
         $destFileSyncLocalPath = $flavor->getDestFileSyncLocalPath();
         if (!file_exists($destFileSyncLocalPath)) {
             continue;
         }
         $syncKey = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
         kFileSyncUtils::moveFromFile($destFileSyncLocalPath, $syncKey);
         // replacing the file name in the ism file
         $oldName = basename($flavor->getDestFileSyncLocalPath());
         $flavor->setDestFileSyncLocalPath(kFileSyncUtils::getLocalFilePathForKey($syncKey));
         KalturaLog::debug("Convert archived file to: " . $flavor->getDestFileSyncLocalPath());
         $newName = basename($flavor->getDestFileSyncLocalPath());
         KalturaLog::debug("Editing ISM [{$oldName}] to [{$newName}]");
         $ismContent = str_replace("src=\"{$oldName}\"", "src=\"{$newName}\"", $ismContent);
         // creating post convert job (without thumb)
         $jobSubType = BatchJob::BATCHJOB_SUB_TYPE_POSTCONVERT_FLAVOR;
         kJobsManager::addPostConvertJob($dbBatchJob, $jobSubType, $flavor->getDestFileSyncLocalPath(), $flavor->getFlavorAssetId(), $flavor->getFlavorParamsOutputId(), file_exists($thumbPath), $offset);
         $finalFlavors[] = $flavor;
         $addedFlavorParamsOutputsIds[] = $flavor->getFlavorParamsOutputId();
     }
     // adding flavor params ids to the entry
     $addedFlavorParamsOutputs = flavorParamsOutputPeer::retrieveByPKs($addedFlavorParamsOutputsIds);
     foreach ($addedFlavorParamsOutputs as $addedFlavorParamsOutput) {
         $entry->addFlavorParamsId($addedFlavorParamsOutput->getFlavorParamsId());
     }
     $ismVersion = $entry->getIsmVersion();
     // syncing the ismc file
     if (file_exists($ismcPath)) {
         $syncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_ISMC, $ismVersion);
         kFileSyncUtils::moveFromFile($ismcPath, $syncKey);
     }
     // replacing the ismc file name in the ism file
     $oldName = basename($ismcPath);
     $newName = basename(kFileSyncUtils::getLocalFilePathForKey($syncKey));
     KalturaLog::debug("Editing ISM [{$oldName}] to [{$newName}]");
     $ismContent = str_replace("content=\"{$oldName}\"", "content=\"{$newName}\"", $ismContent);
     $ismPath .= '.tmp';
     $bytesWritten = file_put_contents($ismPath, $ismContent);
     if (!$bytesWritten) {
         KalturaLog::err("Failed to update file [{$ismPath}]");
     }
     // syncing ism and lig files
     if (file_exists($ismPath)) {
         kFileSyncUtils::moveFromFile($ismPath, $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_ISM, $ismVersion));
     }
     if (file_exists($logPath)) {
         kFileSyncUtils::moveFromFile($logPath, $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_CONVERSION_LOG, $ismVersion));
     }
     // saving entry changes
     $entry->save();
     // save the data changes to the db
     $data->setFlavors($finalFlavors);
     $dbBatchJob->setData($data);
     $dbBatchJob->save();
     // send notification if needed
     $rootBatchJob = $dbBatchJob->getRootJob();
     if ($rootBatchJob && $rootBatchJob->getJobType() == BatchJobType::BULKDOWNLOAD) {
         $localPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
         $downloadUrl = $flavorAsset->getDownloadUrl();
         $notificationData = array("puserId" => $entry->getPuserId(), "entryId" => $entry->getId(), "entryIntId" => $entry->getIntId(), "entryVersion" => $entry->getVersion(), "archivedFile" => $localPath, "downoladPath" => $localPath, "conversionQuality" => $entry->getConversionQuality(), "downloadUrl" => $downloadUrl);
         $extraData = array("data" => json_encode($notificationData), "partner_id" => $entry->getPartnerId(), "puser_id" => $entry->getPuserId(), "entry_id" => $entry->getId(), "entry_int_id" => $entry->getIntId(), "entry_version" => $entry->getVersion(), "file_format" => $flavorAsset->getFileExt(), "archived_file" => $localPath, "downolad_path" => $localPath, "target" => $localPath, "conversion_quality" => $entry->getConversionQuality(), "download_url" => $downloadUrl, "status" => $entry->getStatus(), "abort" => $dbBatchJob->getAbort(), "progress" => $dbBatchJob->getProgress(), "message" => $dbBatchJob->getMessage(), "description" => $dbBatchJob->getDescription(), "updates_count" => $dbBatchJob->getUpdatesCount(), "job_type" => BatchJobType::DOWNLOAD, "status" => BatchJob::BATCHJOB_STATUS_FINISHED, "progress" => 100, "debug" => __LINE__);
         myNotificationMgr::createNotification(kNotificationJobData::NOTIFICATION_TYPE_BATCH_JOB_SUCCEEDED, $dbBatchJob, $dbBatchJob->getPartnerId(), null, null, $extraData, $dbBatchJob->getEntryId());
     }
     return $dbBatchJob;
 }