/** * Action to upload a new data file. */ public function uploadAction() { $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout()->disableLayout(); /* Load the session the file is being attached too. */ if (!($sID = $this->_getParam('session-id')) || !($session = Sahara_Database_Record_Session::load($sID))) { echo 'FAILED: Session not found.'; return; } /* Make sure the user is the person who ran the session. */ list($ns, $user) = explode(':', $this->_auth->getIdentity(), 2); if (!($session->user->namespace == $ns && $session->user->name == $user)) { echo 'FAILED: Not authorised to add file.'; return; } /* Upload the file. */ if (!($dest = $this->_config->upload->dir)) { $this->_logger->error("Download directory not configured, 'upload.dir' property."); throw new Exception('Download directory not configured.'); } /* Configure the file uploader. */ $adapter = new Zend_File_Transfer_Adapter_Http(); $adapter->setDestination($dest); if ($size = $this->_config->upload->size) { $adapter->addValidator('FilesSize', false, $size); } /* Receive the files. */ if (!$adapter->receive()) { $error = 'File validation has failed.'; foreach ($adapter->getMessages() as $k => $v) { switch ($k) { case 'fileUploadErrorIniSize': case 'fileUploadErrorFormSize': $error .= ' The file size was too large.'; break; default: $error .= ' ' . $v; break; } } echo "FAILED: {$error}"; return; } /* Stores the files. */ $names = array(); foreach ($adapter->getFileInfo() as $file => $info) { $sf = new Sahara_Database_Record_SessionFile(); $sf->session = $session; $sf->transferred = true; $sf->transfer = 'UPLOAD'; $sf->timestamp = new DateTime(); $path = $adapter->getFileName($file); $sf->name = implode('_', explode(' ', substr($path, strrpos($path, '/') + 1))); if (!($this->_config->ands && ($script = $this->_config->ands->fileops) && ($mounter = $this->_config->ands->mountuser) && ($mount = $this->_config->ands->mountpoint))) { $this->_logger->warn('ANDS file operations script or mount user not configured.'); $response['reason'] = 'Incorrect configuration.'; echo $this->view->json($response); return; } if (substr($mount, -1) != '/') { $mount .= '/'; } $sf->path = "/{$session->id}"; exec("sudo -u {$mounter} {$script} cp '{$path}' '{$mount}{$session->id}/{$sf->name}'", $output, $code); if ($code == 0) { $response['success'] = true; } else { $this->_logger->warn("Failed to copy '{$path}' to '{$sf->path}'. Code: {$code}, line: " . $output[0]); $response['reason'] = "File copy file to shared directory. Code: {$code}"; } unlink($path); try { $sf->save(); array_push($names, "{$sf->name}={$sf->id}"); } catch (Sahara_Database_Exception $ex) { $this->_logger->error("Failed to add file to session '{$session->id}':" . $ex->getMessage()); echo 'FAILED: Failed to add record: ' . $ex->getMessage(); return; } } echo "SUCCESS: " . implode(',', $names); }
/** * Action that adds a collection. */ public function addcollectionAction() { $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout()->disableLayout(); $response = array('success' => false, 'reason' => ''); if (!($pID = $this->_getParam('project')) || !($project = Sahara_Database_Record_Project::load($pID))) { $response['reason'] = 'Project not found.'; echo $this->view->json($response); return; } if (!($user = Sahara_Database_Record_User::getLoginUser())) { $response['reason'] = 'No user logged in.'; echo $this->view->json($response); return; } /* Make sure the project is owned by the user. */ if (!($user->namespace == $project->user->namespace && $user->name == $project->user->name)) { $response['reason'] = 'Not authorised to add collection for project.'; echo $this->view->json($response); return; } /* Add collection. */ $collection = new Sahara_Database_Record_Collection(); $collection->user_managed = true; $collection->project = $project; /* Add sessions to collection. */ if (!($ses = $this->_getParam('sessions'))) { $response['reason'] = 'Param not provided.'; echo $this->view->json($response); return; } foreach (explode(',', $ses) as $sID) { if (!($session = Sahara_Database_Record_Session::load($sID))) { $response['reason'] = "Session {$sID} not found."; echo $this->view->json($response); return; } if (!($user->namespace == $session->user->namespace && $user->name == $session->user->name)) { $response['reason'] = 'Not authorised to add session to collection.'; echo $this->view->json($response); return; } $collection->sessions = $session; } try { $collection->save(); } catch (Sahara_Database_Exception $ex) { throw $ex; $this->_logger->error('Failed to add colletion: ' . $ex->getMessage()); $response['reason'] = 'Failed to add collection.'; echo $this->view->json($response); return; } /* Call the Scheduling Server to trigger metadata generation. */ $context = stream_context_create(array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query(array('collection' => $collection->id))))); echo file_get_contents('http://' . $this->_config->SchedulingServer->hostname . ':' . $this->_config->SchedulingServer->port . '/SchedulingServer-ANDS/publish', false, $context); }