Exemple #1
0
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
require_once dirname(__FILE__) . "/includes/global_deploy_config.php";
require_once dirname(__FILE__) . '/includes/kaltura/kaltura_functions.php';
require_once dirname(__FILE__) . "/includes/common.inc.php";
// override max_execution_time for this script because uploads to Kaltura can take a very long time
$max_execution_time = 60 * 60;
set_time_limit($max_execution_time);
writeToLog("\n-------------- Starting clas_dir/upload_to_kaltura.php -----------------\n");
// restrict this script to run from command line only
$sapi_type = php_sapi_name();
if ('cli' != substr($sapi_type, 0, 3)) {
    exit;
} else {
}
$title = stripslashes($argv[1]);
$description = stripslashes($argv[2]);
$userID = $argv[3];
$file = $argv[4];
$CopyrightTerm1 = $argv[5];
$CopyrightTerm2 = $argv[6];
$CopyrightTerm3 = $argv[7];
$CopyrightTerm4 = $argv[8];
writeToLog("{$file}: title {$title} -> upload started at " . date("H:i:s") . "\n");
// store custom data in 'tags' field since we aren't using the tags field
$data = "{$serverVersion},{$userID}" . ",{$CopyrightTerm1}" . ",{$CopyrightTerm2}" . ",{$CopyrightTerm3}" . ",{$CopyrightTerm4}" . "";
$fileToUpload = $uploadPath . $file;
writeToLog("calling uploadToKaltura({$fileToUpload}, {$title}, {$description}, {$data})" . "\n");
// die;
$entryID = uploadToKaltura($fileToUpload, $title, $description, $data);
writeToLog("{$file}: title {$title} -> upload finished at " . date("H:i:s") . ", result entry_id {$entryID}\n");
function uploadToKaltura($filePath, $title, $description, $data, $uploadIteration = 0, $type = "VIDEO")
{
    writeToLog("--- in uploadToKaltura({$filePath}, {$title}, {$description}, {$data}, {$uploadIteration}, {$type}) ---");
    try {
        echo "\nin uploadToKaltura iteration {$uploadIteration}: Begin uploading (entering try catch block)...";
        writeToLog("\nin uploadToKaltura iteration {$uploadIteration}: Begin uploading (entering try catch block)...");
        $client = getKalturaClient();
        writeToLog("\nin uploadToKaltura iteration {$uploadIteration}: filePath:" . $filePath);
        $token = $client->baseEntry->upload($filePath);
        $entry = new KalturaMediaEntry();
        $entry->name = $title;
        $entry->description = $description;
        $entry->tags = $data;
        if ($type == "VIDEO") {
            $entry->mediaType = KalturaMediaType::VIDEO;
        } else {
            if ($type == "AUDIO") {
                $entry->mediaType = KalturaMediaType::AUDIO;
            } else {
                if ($type == "IMAGE") {
                    $entry->mediaType = KalturaMediaType::IMAGE;
                } else {
                    $entry->mediaType = KalturaMediaType::VIDEO;
                }
            }
        }
        $newEntry = $client->media->addFromUploadedFile($entry, $token);
        if (is_null($newEntry)) {
            throw new Exception("Kaltura Exception iteration {$uploadIteration}: addFromUploadedFile returned null");
        }
        echo "\nin uploadToKaltura iteration {$uploadIteration}: DONE! Uploaded a new Video entry " . $newEntry->id;
        writeToLog("in uploadToKaltura iteration {$uploadIteration}: DONE! Uploaded a new Video entry " . $newEntry->id);
    } catch (Exception $ex) {
        writeToLog("in uploadToKaltura: uploadError, iteration {$uploadIteration}: " . $ex->getMessage());
        $uploadIteration++;
        if ($uploadIteration >= KALTURA_UPLOAD_RETRY_TIMES) {
            die($ex->getMessage());
        } else {
            return uploadToKaltura($filePath, $title, $description, $data, $uploadIteration, $type);
        }
    }
    return $newEntry->id;
}