public function index(SS_HTTPRequest $request)
 {
     if ($request->isPOST()) {
         $update = json_decode($request->getBody());
         $joblog = TranscodeJob::get()->filter('JobID', (int) $update->id)->first();
         // return if status already is done (some protection)
         if ($joblog->JobStatus !== "started") {
             return "Error: job status not started";
         }
         // save full update into log object -- no, may contain passwords etc. -- well, fixed but still...
         //format_id
         // load files into appropriate relations
         $transcodable = $joblog->Transcodable();
         $transcodable->loadTranscodedFiles();
         if (count(get_object_vars($update->errors))) {
             $joblog->JobErrorMessage = json_encode($update->errors);
             $joblog->JobStatus = "error";
         } else {
             if ($transcodable->transcodingComplete()) {
                 // set status to done when complete...
                 $joblog->JobErrorMessage = "";
                 $joblog->JobStatus = "done";
             }
         }
         // write logfile
         $joblog->write();
     } else {
         // this shouldn't happen
         return "Well hello there...";
     }
     return "Updated";
 }
 /** 
  * Transcode missing formats from source
  * @param type $missingOnly
  */
 public function transcode($missingOnly = true)
 {
     // get source in preferred order
     $source = false;
     if (!$source && $this->SourceID) {
         $source = $this->Source();
     }
     if (!$source && $this->MP3ID) {
         $source = $this->MP3();
     }
     if (!$source && $this->OGGID) {
         $source = $this->OGG();
     }
     if (!$source || !$source->exists()) {
         Session::set('AudioNotification', array('error' => _t('Transcodable.MissingAudioSource', 'Could not find any audio to use as source for transcoding')));
         return false;
     }
     // Build heywatch configuration
     $hw_config = "# Audio config for Heywatch\n\t\t\t\nset source  = aud_source\nset webhook = aud_webhook\n";
     // Add missing files;
     if (!$this->MP3ID) {
         $hw_config .= "\n-> mp3 = aud_uploadaud_pathaud_name.mp4";
     }
     if (!$this->OGGID) {
         $hw_config .= "\n-> ogg = aud_uploadaud_pathaud_name.ogg";
     }
     $ext = pathinfo($source->getFilename(), PATHINFO_EXTENSION);
     // if we're on localhost, use development webhook, else use real one
     if (Config::inst()->get('Transcoding', 'transcode_development_webhook')) {
         $whook = Config::inst()->get('Transcoding', 'transcode_development_webhook');
     } else {
         $whook = Transcode_Controller::staticAbsoluteLink();
     }
     $replacements = array('aud_webhook' => $whook, 'aud_upload' => Config::inst()->get('Transcoding', 'transcode_upload_method'), 'aud_path' => Config::inst()->get('Transcoding', 'transcode_relative_audio_path'), 'aud_source' => $source->getAbsoluteURL(), 'aud_name' => basename($source->getFilename(), "." . $ext));
     $hw_config = strtr($hw_config, $replacements);
     //		$conf = file_get_contents("heywatch.conf");
     //		Debug::dump($hw_config);
     $joblog = TranscodeJob::getOrCreateForTranscodable($this->ID);
     $joblog->TranscodableClass = $this->ClassName;
     $job = HeyWatch::submit($hw_config, Config::inst()->get('Transcoding', 'transcode_api_key'));
     if ($job->{"status"} == "ok") {
         // job created
         $joblog->JobStatus = "started";
         $joblog->JobID = $job->{"id"};
         // Feedback to user
         Session::set('VideoNotification', array('good' => 'Transcoding started'));
     } else {
         // job not created...
         $joblog->JobStatus = "error";
         //$joblog->JobErrorCode = $job->{"error_code"};
         $joblog->JobErrorMessage = $job->{"error_message"};
         // Feedback to user
         Session::set('VideoNotification', array('bad' => 'Transcoding error'));
     }
     $joblog->write();
 }