public function copy() { /* @var $new S3File */ $new = parent::copy(); ob_start(); //copy our new one to its own path. $newPath = parent::getNiceDir($new->get('path')); $this->client->copyObject(array('Bucket' => AMAZON_S3_BUCKET_NAME, 'Key' => $newPath, 'CopySource' => $this->get('bucket') . "/" . $this->get('path'), 'ACL' => 'public-read')); $new->set('path', $newPath); $new->set('bucket', AMAZON_S3_BUCKET_NAME); $new->save(); ob_end_clean(); return $new; }
public static function _handleZipFile($zip_path, $zip_file) { $za = new ZipArchive(); $za->open($zip_path); for ($i = 0; $i < $za->numFiles; $i++) { //look up file info. $filename = $za->getNameIndex($i); //okay, is it a supported file? if (preg_match('/(gcode|stl|obj|amf)$/i', $filename)) { $temp_file = tempnam("/tmp", "BQ"); copy("zip://" . $zip_path . "#" . $filename, $temp_file); //format for upload $filename = str_replace(" ", "_", $filename); $filename = preg_replace("/[^-_.[0-9a-zA-Z]/", "", $filename); $path = "assets/" . StorageInterface::getNiceDir($filename); //create our file $file = Storage::newFile(); $file->set('parent_id', $zip_file->id); $file->set('user_id', User::$me->id); $file->upload($temp_file, $path); } } //exit; }
public function api_devicescanresults() { //$old_scan_data = json::decode($this->token->get('device_data')); $scan_data = json::decode($this->args('scan_data')); //var_dump($scan_data); if (!empty($_FILES)) { // We currently don't want to delete the old data // //delete any old files if we have them. // if (!empty($old_scan_data->camera_files)) { // foreach ($old_scan_data->camera_files AS $id) { // $data_file = Storage::get($id); // $data_file->delete(); // } // } foreach ($_FILES as $file) { if (is_uploaded_file($file['tmp_name'])) { $filename = $file['name']; $filename = str_replace(" ", "_", $filename); $filename = preg_replace("/[^-_.[0-9a-zA-Z]/", "", $filename); $this->ensureGoodFile($file); //okay, we're good.. do it. $data_file = Storage::newFile(); $data_file->set('user_id', User::$me->id); $data_file->upload($file['tmp_name'], StorageInterface::getNiceDir($filename)); $scan_data->camera_files[] = $data_file->id; } } } //var_dump($scan_data); //var_dump($_FILES); $this->token->set('device_data', json::encode($scan_data)); $this->token->set('last_seen', date('Y-m-d H:i:s')); $this->token->save(); return True; }
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()); } }
public function uploadNice($srcPath, $name, $prefix = "") { $this->upload($srcPath, $prefix . StorageInterface::getNiceDir($name)); }