Example #1
0
 function thingiverse_callback()
 {
     $this->assertLoggedIn();
     if ($this->args('code')) {
         $api = new ThingiverseAPI(THINGIVERSE_API_CLIENT_ID, THINGIVERSE_API_CLIENT_SECRET);
         $token = $api->exchange_token($this->args('code'));
         if ($token) {
             //save it!
             User::$me->set('thingiverse_token', $token);
             User::$me->save();
             //send us to our thingiverse page.
             $this->forwardToUrl("/thingiverse");
         } else {
             die("Failed to exchange token.");
         }
     }
 }
Example #2
0
 /**
  * @param $url
  * @return StorageInterface
  * @throws Exception
  */
 private function _handleThingiverseLinks($url)
 {
     $matches = array();
     if (preg_match("/thingiverse.com\\/thing:([0-9]+)/i", $url, $matches)) {
         $thing_id = $matches[1];
         if (!defined('THINGIVERSE_API_CLIENT_ID') && !defined('THINGIVERSE_API_CLIENT_SECRET')) {
             throw new Exception("This site has not set up the Thingiverse api.");
         }
         $thingiverse_token = User::$me->getThingiverseToken();
         if ($thingiverse_token === '') {
             $this->forwardToURL("/thingiverse/url/" . base64_encode(serialize($url)));
         }
         $api = new ThingiverseAPI(THINGIVERSE_API_CLIENT_ID, THINGIVERSE_API_CLIENT_SECRET, User::$me->getThingiverseToken());
         //load thingiverse file
         $zip_file = $api->download_thing($thing_id);
         if ($zip_file !== null && $zip_file->isZip()) {
             $storage_file = Storage::newFile();
             $storage_file->set('user_id', User::$me->id);
             $storage_file->set('source_url', $url);
             $storage_file->uploadNice($zip_file->getFile(), $zip_file->getName());
             FileUploadHandler::_handleZipFile($zip_file->getFile(), $storage_file);
             return $storage_file;
         } else {
             throw new Exception("We can't seem to access that file");
         }
     } else {
         throw new Exception("That is not a valid Thingiverse link");
     }
 }
Example #3
0
 public function url()
 {
     $this->assertLoggedIn();
     $this->setTitle("Create Job from URL");
     try {
         //did we get a url?
         $url = $this->args('url');
         if (!$url) {
             throw new Exception("You must pass in the URL parameter!");
         }
         $matches = array();
         if (preg_match("/thingiverse.com\\/thing:([0-9]+)/i", $url, $matches)) {
             $thing_id = $matches[1];
             //echo "found: $thing_id<Br/>";
             // TODO: We need to define a thingiverse api client ID, or get it when the user
             // authenticates it.
             $api = new ThingiverseAPI(THINGIVERSE_API_CLIENT_ID, THINGIVERSE_API_CLIENT_SECRET, User::$me->getThingiverseToken());
             //load thingiverse data.
             $thing = $api->make_call("/things/{$thing_id}");
             $files = $api->make_call("/things/{$thing_id}/files");
             //echo "<pre>";
             //print_r($thing);
             //print_r($files);
             //echo "</pre>";
             //open zip file.
             $zip_path = tempnam("/tmp", "BQ");
             $zip = new ZipArchive();
             if ($zip->open($zip_path, ZIPARCHIVE::CREATE)) {
                 //echo "opened $zip_path<br/>";
                 //pull in all our files.
                 foreach ($files as $row) {
                     if (preg_match("/\\.(stl|obj|amf|gcode)\$/i", $row->name)) {
                         $data = Utility::downloadUrl($row->public_url);
                         //echo "downloaded " . $data['realname'] . " to " . $data['localpath'] . "<br/>";
                         $zip->addFile($data['localpath'], $data['realname']);
                     }
                 }
                 $zip->close();
                 //create zip name.
                 $filename = basename($thing->name . ".zip");
                 $filename = str_replace(" ", "_", $filename);
                 $filename = preg_replace("/[^-_.[0-9a-zA-Z]/", "", $filename);
                 $path = "assets/" . StorageInterface::getNiceDir($filename);
                 //okay, upload it and handle it.
                 $file = Storage::newFile();
                 $file->set('user_id', User::$me->id);
                 $file->set('source_url', $url);
                 //echo "uploading $zip_path to $path<br/>";
                 $file->upload($zip_path, $path);
                 FileUploadHandler::_handleZipFile($zip_path, $file);
                 $this->forwardToUrl("/job/create/file:{$file->id}");
             } else {
                 throw new Exception("Unable to open zip {$zip_path} for writing.");
             }
         } else {
             $data = Utility::downloadUrl($url);
             //does it match?
             if (!preg_match("/\\.(stl|obj|amf|gcode|zip)\$/i", $data['realname'])) {
                 throw new Exception("The file <a href=\"" . $url . "\">{$data['realname']}</a> is not valid for printing.");
             }
             $file = Storage::newFile();
             $file->set('user_id', User::$me->id);
             $file->set('source_url', $url);
             $file->upload($data['localpath'], StorageInterface::getNiceDir($data['realname']));
             //is it a zip file?  do some magic on it.
             if (!preg_match("/\\.zip\$/i", $data['realname'])) {
                 FileUploadHandler::_handleZipFile($data['localpath'], $file);
             }
             Activity::log("uploaded a new file called " . $file->getLink() . ".");
             //send us to step 2.
             $this->forwardToUrl("/job/create/file:{$file->id}");
         }
     } catch (Exception $e) {
         $this->set('megaerror', $e->getMessage());
     }
 }