diff --git a/ajax/sessionController.php b/ajax/sessionController.php index 80a86e0b..dc038684 100644 --- a/ajax/sessionController.php +++ b/ajax/sessionController.php @@ -14,32 +14,13 @@ namespace OCA\Documents; class SessionController extends Controller{ - public static function start($args){ + public static function join($args){ $uid = self::preDispatch(); + $fileId = intval(@$args['file_id']); 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']; - } - - + $path = Storage::getFilePath($fileId); $session = Session::getSessionByFileId($fileId); + //If there is no existing session we need to start a new one if (!$session || empty($session)){ @@ -63,29 +44,6 @@ class SessionController extends Controller{ 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 @@ -111,9 +69,7 @@ class SessionController extends Controller{ throw new \Exception('Session does not exist'); } - $fileInfo = \OC\Files\Cache\Cache::getById($session['file_id']); - $path = $fileInfo[1]; - + $path = Storage::getFilePath($session['file_id']); $view = new \OC\Files\View('/' . $session['owner']); $isWritable = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path); diff --git a/appinfo/routes.php b/appinfo/routes.php index 6e2abe0e..94a14350 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -31,19 +31,9 @@ $this->create('documents_documents_list', 'ajax/documents/list') ->action('\OCA\Documents\DocumentController', 'listAll') ; - /** * Session routes */ -$this->create('documents_session_start', 'ajax/session/start') - ->get() - ->action('\OCA\Documents\SessionController', 'start') -; -$this->create('documents_session_start', 'ajax/session/start') - ->post() - ->action('\OCA\Documents\SessionController', 'start') -; - $this->create('documents_session_list', 'ajax/session/list') ->get() ->action('\OCA\Documents\SessionController', 'listAll') @@ -67,11 +57,11 @@ $this->create('documents_session_listhtml', 'ajax/session/listHtml') ->action('\OCA\Documents\SessionController', 'listAllHtml') ; -$this->create('documents_session_join', 'ajax/session/join/{es_id}') +$this->create('documents_session_join', 'ajax/session/join/{file_id}') ->get() ->action('\OCA\Documents\SessionController', 'join') ; -$this->create('documents_session_join', 'ajax/session/join/{es_id}') +$this->create('documents_session_join', 'ajax/session/join/{file_id}') ->post() ->action('\OCA\Documents\SessionController', 'join') ; diff --git a/js/documents.js b/js/documents.js index 819853d8..b5681fc1 100644 --- a/js/documents.js +++ b/js/documents.js @@ -90,13 +90,23 @@ var documentsMain = { onStartup: function() { "use strict"; documentsMain.UI.init(); + + // Does anything indicate that we need to autostart a session? + var esId = parent.location.hash.replace(/\W*/g, ''); + if (!esId){ + documentsMain.show(); + } else { + documentsMain.UI.showOverlay(); + } + + OC.addScript('documents', '3rdparty/webodf/dojo-amalgamation', function() { OC.addScript('documents', '3rdparty/webodf/webodf-debug').done(function() { // preload stuff in the background require({}, ["dojo/ready"], function(ready) { ready(function() { require({}, ["webodf/editor/Editor"], function(Editor) { - var esId = parent.location.hash.replace(/\W*/g, ''); + if (esId){ documentsMain.prepareSession(); documentsMain.joinSession(esId); @@ -121,10 +131,11 @@ var documentsMain = { initSession: function(response) { "use strict"; - runtime.assert(response.status, "Server error"); - if (response.status==='error'){ + + if (!response || !response.status || response.status==='error'){ OC.Notification.show(t('documents', 'Failed to load this document. Please check if it can be opened with an external odt editor. This might also mean it has been unshared or deleted recently.')); documentsMain.prepareGrid(); + documentsMain.show(); setTimeout(OC.Notification.hide, 7000); return; } @@ -145,27 +156,17 @@ var documentsMain = { documentsMain.webodfEditorInstance.openSession(response.es_id, memberId, function() { documentsMain.webodfEditorInstance.startEditing(); documentsMain.UI.hideOverlay(); - parent.location.hash = response.es_id; + parent.location.hash = response.file_id; }); }); }); }, - startSession: function(fileid) { - "use strict"; - console.log('starting session for fileid '+fileid); + joinSession: function(fileId) { + console.log('joining session '+fileId); $.post( - OC.Router.generate('documents_session_start'), - {'fileid': fileid}, - documentsMain.initSession - ); - }, - - joinSession: function(esId) { - console.log('joining session '+esId); - $.post( - OC.Router.generate('documents_session_join') + '/' + esId, + OC.Router.generate('documents_session_join') + '/' + fileId, {}, documentsMain.initSession ); @@ -327,11 +328,8 @@ $(document).ready(function() { return; } documentsMain.prepareSession(); - - if ($(this).attr('data-esid')){ - documentsMain.joinSession($(this).attr('data-esid')); - } else if ($(this).attr('data-id')){ - documentsMain.startSession($(this).attr('data-id')); + if ($(this).attr('data-id')){ + documentsMain.joinSession($(this).attr('data-id')); } }); @@ -344,7 +342,6 @@ $(document).ready(function() { $('.add-document').on('click', '.add', documentsMain.onCreate); - documentsMain.show(); var file_upload_start = $('#file_upload_start'); file_upload_start.on('fileuploaddone', documentsMain.show); //TODO when ending a session as the last user close session? diff --git a/lib/storage.php b/lib/storage.php index ec1c6cab..27d7fcf0 100755 --- a/lib/storage.php +++ b/lib/storage.php @@ -41,6 +41,36 @@ class Storage { return $list; } + /** + * @brief Retrieve path by fileId + * @param int $fileId + * @throws \Exception + */ + public static function getFilePath($fileId){ + if (!$fileId){ + throw new \Exception('No valid file has been passed'); + } + + $fileInfo = \OC\Files\Cache\Cache::getById($fileId); + $path = @$fileInfo[1]; + + if (!$path){ + throw new \Exception('File not found in cache'); + } + + // Strip /files infront of the path + $normalizedPath = preg_replace('/^\/?files/', '', $path); + if (!\OC\Files\Filesystem::file_exists($path) + && \OC\Files\Filesystem::file_exists('/Shared' . $normalizedPath) + && \OC\Files\Filesystem::is_file('/Shared' . $normalizedPath) + ){ + // this file is shared + $normalizedPath = '/Shared' . $normalizedPath; + } + + return $normalizedPath; + } + /** * @brief Cleanup session data on removing the document * @param array diff --git a/lib/view.php b/lib/view.php index 92bdeb65..78074a36 100644 --- a/lib/view.php +++ b/lib/view.php @@ -41,6 +41,10 @@ class View extends \OC\Files\View{ throw new \Exception('Original document doesn\'t exist any more'); } + if (!$view->is_file($relPath)){ + throw new \Exception('Object ' . $relPath . ' is not a file.'); + } + $newName = '/' . sha1($view->file_get_contents($relPath)) . '.odt'; $view->copy($relPath, self::DOCUMENTS_DIRNAME . $newName);