示例#1
0
 public function api_webcamupdate()
 {
     if ((int) $this->args('job_id')) {
         $job = new Job($this->args('job_id'));
         if (!$job->isHydrated()) {
             throw new Exception("Job does not exist.");
         }
         $bot = $job->getBot();
         if (!$bot->isHydrated()) {
             throw new Exception("Bot does not exist.");
         }
         if (!$job->getQueue()->isMine()) {
             throw new Exception("This job is not in your queue.");
         }
     } else {
         if ((int) $this->args('bot_id')) {
             $bot = new Bot($this->args('bot_id'));
             if (!$bot->isHydrated()) {
                 throw new Exception("Bot does not exist.");
             }
             $job = new Job();
         } else {
             throw new Exception("You must pass in either a bot or job id.");
         }
     }
     if (!$bot->isMine()) {
         throw new Exception("This is not your bot.");
     }
     if (!empty($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
         //upload our file
         $file = $_FILES['file'];
         $this->ensureGoodFile($file);
         //does it match?
         if (!preg_match("/\\.jpg\$/i", $file['name'])) {
             throw new Exception("The file must end in .jpg");
         }
         //is it a real image?
         $size = getimagesize($file['tmp_name']);
         if (!$size[0] || !$size[1]) {
             throw new Exception("The file is not a valid image.");
         }
         //okay, we're good.. do it.
         if ($job->isHydrated()) {
             $data_file = Storage::newFile();
         } else {
             $data_file = $bot->getWebcamImage();
         }
         $data_file->set('user_id', User::$me->id);
         $data_file->upload($file['tmp_name'], StorageInterface::getNiceDir($file['name']));
         //if we have a job, save our new image.
         if ($job->isHydrated()) {
             $job->addWebcamImage($data_file->id);
         }
         //always pull the latest image in for the bot.
         $bot->set('webcam_image_id', $data_file->id);
     } else {
         throw new Exception("No file uploaded.");
     }
     //did we get temperatures?
     $this->_updateTemperatures($this->args('temperatures'), $bot, $job);
     //update our job info.
     if ($this->args('progress') && $job->isHydrated()) {
         $job->set('progress', (double) $this->args('progress'));
     }
     //only save the job if its real.
     if ($job->isHydrated()) {
         $job->save();
     }
     //update our bot info
     $bot = $this->_markBotAsSeen($bot);
     //what kind of data to send back.
     if ($job->isHydrated()) {
         return $job->getAPIData();
     } else {
         return $bot->getAPIData();
     }
 }