function main() { $this->assertLoggedIn(); if ($this->args('payload')) { $_SESSION['thing_url'] = unserialize(base64_decode($this->args('payload'))); } if (User::$me->get('thingiverse_token')) { // We were uploading a file before we were interrupted if (isset($_SESSION['thing_url'])) { $this->forwardToURL('/upload/url'); } $this->setTitle("Thingiverse + BotQueue = :D"); $api = new ThingiverseAPI(THINGIVERSE_API_CLIENT_ID, THINGIVERSE_API_CLIENT_SECRET, User::$me->getThingiverseToken()); $this->set('my_info', $api->make_call('/users/me')); } else { $this->setTitle("Link Thingiverse to BotQueue"); } }
function thingiverse() { $this->assertLoggedIn(); if (User::$me->get('thingiverse_token')) { $this->setTitle("Thingiverse + BotQueue = :D"); $api = new ThingiverseAPI(THINGIVERSE_API_CLIENT_ID, THINGIVERSE_API_CLIENT_SECRET, User::$me->get('thingiverse_token')); $this->set('thing', $api->make_call('/things/82335')); $this->set('files', $api->make_call('/things/82335/files')); $this->set('my_info', $api->make_call('/users/me')); } else { $this->setTitle("Link Thingiverse to BotQueue"); } }
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()); } }