From a92c6659163261c66241ba83f536c6c7dce25574 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Mon, 2 Sep 2013 19:54:23 +0300 Subject: [PATCH] Split controllers --- ajax/controller.php | 193 +----------------------------------- ajax/documentController.php | 54 ++++++++++ ajax/sessionController.php | 169 +++++++++++++++++++++++++++++++ ajax/userController.php | 15 +-- appinfo/app.php | 2 + appinfo/routes.php | 45 +++++---- 6 files changed, 255 insertions(+), 223 deletions(-) create mode 100644 ajax/documentController.php create mode 100644 ajax/sessionController.php diff --git a/ajax/controller.php b/ajax/controller.php index a6d0dde5..ef926eab 100644 --- a/ajax/controller.php +++ b/ajax/controller.php @@ -14,205 +14,16 @@ namespace OCA\Documents; class Controller { - /** - * Process partial/complete file download - * @param array $args - array containing session id as anelement with a key es_id - */ - public static function serve($args){ - self::preDispatch(false); - - $session = Session::getSession(@$args['es_id']); - $filename = isset($session['genesis_url']) ? $session['genesis_url'] : ''; - $documentsView = View::initDocumentsView($session['owner']); - $download = new Download($documentsView, $filename); - $download->sendResponse(); - } - - public static function startSession($args){ - $uid = self::preDispatch(); - try{ - $path = \OC\Files\Filesystem::getPath(@$_POST['fileid']); - if (!$path){ - throw new \Exception('No file has been passed'); - } - - $info = \OC\Files\Filesystem::getFileInfo($path); - if (!$info){ - // Is it shared? - //searchByMime returns incorrect path for shared items - // - if (substr($path, 0, 14) === '/Shared/files/'){ - // remove 'files/' from path as it's relative to '/Shared' - $path = '/Shared' . substr($path, 13); - $sharedInfo = \OC\Files\Filesystem::getFileInfo($path); - $fileId = $sharedInfo['fileid']; - - } - } else { - $fileId = $info['fileid']; - } - - - $session = Session::getSessionByFileId($fileId); - //If there is no existing session we need to start a new one - if (!$session || empty($session)){ - - $documentsView = View::initDocumentsView($uid); - $genesisPath = View::storeDocument($uid, $path); - - if (!$genesisPath){ - throw new \Exception('Unable to copy document. Check permissions and make sure you have enought free space.'); - } - - $hash = View::getHashByGenesis($uid, $genesisPath); - $session = Session::add($genesisPath, $hash, $fileId); - } - - $session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor()); - \OCP\JSON::success($session); - exit(); - } catch (\Exception $e){ - Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage()); - \OCP\JSON::error(); - exit(); - } - } - - public static function joinSession($args){ - $esId = @$args['es_id']; - $uid = self::preDispatch(); - try{ - if (!$esId){ - throw new \Exception('Session id is empty'); - } - - $session = Session::getSession($esId); - if (!$session || empty($session)){ - throw new \Exception('Session doesn\'t exist'); - } - - $session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor()); - \OCP\JSON::success($session); - exit(); - } catch (\Exception $e){ - Helper::warnLog('Joining a session failed. Reason:' . $e->getMessage()); - \OCP\JSON::error(array('message'=>$e->getMessage())); - exit(); - } - } - - /** - * Store the document content to its origin - */ - public static function save(){ - $uid = self::preDispatch(); - $sessionID = @$_SERVER['HTTP_WEBODF_SESSION_ID']; - $memberId = @$_SERVER['HTTP_WEBODF_MEMBER_ID']; - $sessionRevision = @$_SERVER['HTTP_WEBODF_SESSION_REVISION']; - $content = fopen('php://input','r'); - if ($sessionID && $content){ - $session = Session::getSession($sessionID); - $fileInfo = \OC\Files\Cache\Cache::getById($session['file_id']); - $path = $fileInfo[1]; - $view = new \OC\Files\View('/' . $session['owner']); - - $canWrite = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path); - if ($canWrite){ - $view->file_put_contents($path, $content); - } else { - // TODO: report an error, break a plate, burn a house, conquer the galaxy - } - } - } - - /** - * lists the documents the user has access to (including shared files, once the code in core has been fixed) - * also adds session and member info for these files - */ - public static function listDocuments(){ - self::preDispatch(); - - $documents = Storage::getDocuments(); - - $fileIds = array(); - foreach ($documents as $document) { - $fileIds[] = $document['fileid']; - } - - $sessions = Session::getSessionsByFileIds($fileIds); - - $members = array(); - foreach ($sessions as $session) { - $members[$session['es_id']] = Member::getMembersByEsId($session['es_id']); - } - - \OCP\JSON::success(array('documents' => $documents,'sessions' => $sessions,'members' => $members)); - } - - public static function listSessions(){ - self::preDispatch(); - $sessions = Session::getAll(); - if (!is_array($sessions)){ - $sessions = array(); - } - - $preparedSessions = array_map( - function($x){ - return ($x['es_id']); - }, $sessions - ); - \OCP\JSON::success(array( - "session_list" => $preparedSessions - )); - } - - public static function sessionInfo(){ - self::preDispatch(); - $items = @$_POST['items']; - $info = array(); - - if (is_array($items)){ - $info = Session::getInfoByFileid($items); - } - - \OCP\JSON::success(array( - "info" => $info - )); - } - - public static function listSessionsHtml(){ - self::preDispatch(); - $sessions = Session::getAll(); - if (!is_array($sessions)){ - $sessions = array(); - } - - $preparedSessions = array_map( - function($x){ - return ($x['es_id']); - }, $sessions - ); - - $invites = Invite::getAllInvites(); - if (!is_array($invites)){ - $invites = array(); - } - - $tmpl = new \OCP\Template('documents', 'part.sessions', ''); - $tmpl->assign('invites', $invites); - $tmpl->assign('sessions', $sessions); - echo $tmpl->fetchPage(); - } - /** * Do security precheck * @param bool callcheck - whether security token check is needed * @return string userId of the currently logged in user */ - protected static function preDispatch($callcheck = true){ + public static function preDispatch($callcheck = true){ if ($callcheck){ \OCP\JSON::callCheck(); } + \OCP\JSON::checkAppEnabled('documents'); \OCP\JSON::checkLoggedIn(); return \OCP\User::getUser(); } diff --git a/ajax/documentController.php b/ajax/documentController.php new file mode 100644 index 00000000..4468b500 --- /dev/null +++ b/ajax/documentController.php @@ -0,0 +1,54 @@ +sendResponse(); + } + + /** + * lists the documents the user has access to (including shared files, once the code in core has been fixed) + * also adds session and member info for these files + */ + public static function listAll(){ + self::preDispatch(); + + $documents = Storage::getDocuments(); + + $fileIds = array(); + foreach ($documents as $document) { + $fileIds[] = $document['fileid']; + } + + $sessions = Session::getSessionsByFileIds($fileIds); + + $members = array(); + foreach ($sessions as $session) { + $members[$session['es_id']] = Member::getMembersByEsId($session['es_id']); + } + + \OCP\JSON::success(array('documents' => $documents,'sessions' => $sessions,'members' => $members)); + } +} \ No newline at end of file diff --git a/ajax/sessionController.php b/ajax/sessionController.php new file mode 100644 index 00000000..9b121355 --- /dev/null +++ b/ajax/sessionController.php @@ -0,0 +1,169 @@ +getMessage()); + \OCP\JSON::error(); + exit(); + } + } + + public static function join($args){ + $esId = @$args['es_id']; + $uid = self::preDispatch(); + try{ + if (!$esId){ + throw new \Exception('Session id is empty'); + } + + $session = Session::getSession($esId); + if (!$session || empty($session)){ + throw new \Exception('Session doesn\'t exist'); + } + + $session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor()); + \OCP\JSON::success($session); + exit(); + } catch (\Exception $e){ + Helper::warnLog('Joining a session failed. Reason:' . $e->getMessage()); + \OCP\JSON::error(array('message'=>$e->getMessage())); + exit(); + } + } + + /** + * Store the document content to its origin + */ + public static function save(){ + $uid = self::preDispatch(); + $sessionID = @$_SERVER['HTTP_WEBODF_SESSION_ID']; + $memberId = @$_SERVER['HTTP_WEBODF_MEMBER_ID']; + $sessionRevision = @$_SERVER['HTTP_WEBODF_SESSION_REVISION']; + $content = fopen('php://input','r'); + if ($sessionID && $content){ + $session = Session::getSession($sessionID); + $fileInfo = \OC\Files\Cache\Cache::getById($session['file_id']); + $path = $fileInfo[1]; + $view = new \OC\Files\View('/' . $session['owner']); + + $canWrite = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path); + if ($canWrite){ + $view->file_put_contents($path, $content); + } else { + // TODO: report an error, break a plate, burn a house, conquer the galaxy + } + } + } + + public static function info(){ + self::preDispatch(); + $items = @$_POST['items']; + $info = array(); + + if (is_array($items)){ + $info = Session::getInfoByFileid($items); + } + + \OCP\JSON::success(array( + "info" => $info + )); + } + + public static function listAll(){ + self::preDispatch(); + $sessions = Session::getAll(); + if (!is_array($sessions)){ + $sessions = array(); + } + + $preparedSessions = array_map( + function($x){ + return ($x['es_id']); + }, $sessions + ); + \OCP\JSON::success(array( + "session_list" => $preparedSessions + )); + } + + public static function listAllHtml(){ + self::preDispatch(); + $sessions = Session::getAll(); + if (!is_array($sessions)){ + $sessions = array(); + } + + $preparedSessions = array_map( + function($x){ + return ($x['es_id']); + }, $sessions + ); + + $invites = Invite::getAllInvites(); + if (!is_array($invites)){ + $invites = array(); + } + + $tmpl = new \OCP\Template('documents', 'part.sessions', ''); + $tmpl->assign('invites', $invites); + $tmpl->assign('sessions', $sessions); + echo $tmpl->fetchPage(); + } + +} diff --git a/ajax/userController.php b/ajax/userController.php index 5470ea7b..bfe80350 100644 --- a/ajax/userController.php +++ b/ajax/userController.php @@ -12,7 +12,7 @@ namespace OCA\Documents; -class UserController { +class UserController extends Controller{ /** * Search users according to the pattern @@ -74,17 +74,4 @@ class UserController { echo $image->show(); } - /** - * Do security precheck - * @param bool callcheck - whether security token check is needed - * @return string userId of the currently logged in user - */ - protected static function preDispatch($callcheck = true){ - if ($callcheck){ - \OCP\JSON::callCheck(); - } - \OCP\JSON::checkLoggedIn(); - return \OCP\User::getUser(); - } - } \ No newline at end of file diff --git a/appinfo/app.php b/appinfo/app.php index 7b521659..d970d7a1 100755 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -35,6 +35,8 @@ OCP\App::addNavigationEntry(array( ); OC::$CLASSPATH['OCA\Documents\Controller'] = 'documents/ajax/controller.php'; +OC::$CLASSPATH['OCA\Documents\DocumentController'] = 'documents/ajax/documentController.php'; +OC::$CLASSPATH['OCA\Documents\SessionController'] = 'documents/ajax/sessionController.php'; OC::$CLASSPATH['OCA\Documents\UserController'] = 'documents/ajax/userController.php'; OC::$CLASSPATH['OCA\Documents\Download\Simple'] = 'documents/lib/download/simple.php'; OC::$CLASSPATH['OCA\Documents\Download\Range'] = 'documents/lib/download/range.php'; diff --git a/appinfo/routes.php b/appinfo/routes.php index ff9e4ec7..46eaf083 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -9,67 +9,76 @@ * later. */ +/** + * Document routes + */ $this->create('documents_genesis', 'ajax/genesis/{es_id}') ->post() - ->action('\OCA\Documents\Controller', 'serve') + ->action('\OCA\Documents\DocumentController', 'serve') ; $this->create('documents_genesis', 'ajax/genesis/{es_id}') ->get() - ->action('\OCA\Documents\Controller', 'serve') + ->action('\OCA\Documents\DocumentController', 'serve') +; + +$this->create('documents_documents_list', 'ajax/documents/list') + ->get() + ->action('\OCA\Documents\DocumentController', 'listAll') ; + +/** + * Session routes + */ $this->create('documents_session_start', 'ajax/session/start') ->get() - ->action('\OCA\Documents\Controller', 'startSession') + ->action('\OCA\Documents\SessionController', 'start') ; $this->create('documents_session_start', 'ajax/session/start') ->post() - ->action('\OCA\Documents\Controller', 'startSession') -; - -$this->create('documents_documents_list', 'ajax/documents/list') - ->get() - ->action('\OCA\Documents\Controller', 'listDocuments') + ->action('\OCA\Documents\SessionController', 'start') ; $this->create('documents_session_list', 'ajax/session/list') ->get() - ->action('\OCA\Documents\Controller', 'listSessions') + ->action('\OCA\Documents\SessionController', 'listAll') ; $this->create('documents_session_list', 'ajax/session/list') ->post() - ->action('\OCA\Documents\Controller', 'listSessions') + ->action('\OCA\Documents\SessionController', 'listAll') ; $this->create('documents_session_info', 'ajax/session/info') ->post() - ->action('\OCA\Documents\Controller', 'sessionInfo') + ->action('\OCA\Documents\SessionController', 'info') ; $this->create('documents_session_listhtml', 'ajax/session/listHtml') ->get() - ->action('\OCA\Documents\Controller', 'listSessionsHtml') + ->action('\OCA\Documents\SessionController', 'listAllHtml') ; $this->create('documents_session_listhtml', 'ajax/session/listHtml') ->post() - ->action('\OCA\Documents\Controller', 'listSessionsHtml') + ->action('\OCA\Documents\SessionController', 'listAllHtml') ; $this->create('documents_session_join', 'ajax/session/join/{es_id}') ->get() - ->action('\OCA\Documents\Controller', 'joinSession') + ->action('\OCA\Documents\SessionController', 'join') ; $this->create('documents_session_join', 'ajax/session/join/{es_id}') ->post() - ->action('\OCA\Documents\Controller', 'joinSession') + ->action('\OCA\Documents\SessionController', 'join') ; $this->create('documents_session_save', 'ajax/session/save') ->post() - ->action('\OCA\Documents\Controller', 'save') + ->action('\OCA\Documents\SessionController', 'save') ; - +/** + * User routes + */ $this->create('documents_user_avatar', 'ajax/user/avatar') ->get() ->action('\OCA\Documents\UserController', 'sendAvatar')