diff --git a/appinfo/routes.php b/appinfo/routes.php index 408bf0c9..0cca478a 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -30,6 +30,7 @@ $application->registerRoutes($this, [ ['name' => 'document#create', 'url' => 'ajax/documents/create', 'verb' => 'POST'], ['name' => 'document#serve', 'url' => 'ajax/genesis/{esId}', 'verb' => 'GET'], ['name' => 'document#rename', 'url' => 'ajax/documents/rename/{fileId}', 'verb' => 'POST'], + ['name' => 'document#get', 'url' => 'ajax/documents/get/{fileId}', 'verb' => 'GET'], ['name' => 'document#listAll', 'url' => 'ajax/documents/list', 'verb' => 'GET'], ['name' => 'document#download', 'url' => 'ajax/download.php', 'verb' => 'GET'], //documents - for WOPI access diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index f0813394..c92d8469 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -140,6 +140,70 @@ class DocumentController extends Controller { return $discovery; } + /** Prepare document(s) structure + */ + private function prepareDocuments($rawDocuments){ + $discovery_parsed = null; + try { + $discovery = $this->getDiscovery(); + + $loadEntities = libxml_disable_entity_loader(true); + $discovery_parsed = simplexml_load_string($discovery); + libxml_disable_entity_loader($loadEntities); + + if ($discovery_parsed === false) { + $this->cache->remove('discovery.xml'); + $wopiRemote = $this->settings->getAppValue('richdocuments', 'wopi_url'); + + return array( + 'status' => 'error', + 'message' => $this->l10n->t('Collabora Online: discovery.xml from "%s" is not a well-formed XML string.', array($wopiRemote)), + 'hint' => $this->l10n->t('Please contact the "%s" administrator.', array($wopiRemote)) + ); + } + } + catch (ResponseException $e) { + return array( + 'status' => 'error', + 'message' => $e->getMessage(), + 'hint' => $e->getHint() + ); + } + + $fileIds = array(); + $documents = array(); + $lolang = strtolower(str_replace('_', '-', $this->settings->getUserValue($this->uid, 'core', 'lang', 'en'))); + foreach ($rawDocuments as $key=>$document) { + if (is_object($document)){ + $documents[] = $document->getData(); + } else { + $documents[$key] = $document; + } + $documents[$key]['icon'] = preg_replace('/\.png$/', '.svg', \OCP\Template::mimetype_icon($document['mimetype'])); + $documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']); + $documents[$key]['urlsrc'] = $this->getWopiSrcUrl($discovery_parsed, $document['mimetype'], 'edit'); + $documents[$key]['lolang'] = $lolang; + $fileIds[] = $document['fileid']; + } + + usort($documents, function($a, $b){ + return @$b['mtime']-@$a['mtime']; + }); + + $session = new Db\Session(); + $sessions = $session->getCollectionBy('file_id', $fileIds); + + $members = array(); + $member = new Db\Member(); + foreach ($sessions as $session) { + $members[$session['es_id']] = $member->getActiveCollection($session['es_id']); + } + + return array( + 'status' => 'success', 'documents' => $documents,'sessions' => $sessions,'members' => $members + ); + } + /** * @NoAdminRequired * @NoCSRFRequired @@ -428,70 +492,22 @@ class DocumentController extends Controller { /** * @NoAdminRequired - * 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 + * Get file information about single document with fileId */ - public function listAll(){ - $found = Storage::getDocuments(); + public function get($fileId){ + $documents = array(); + $documents[0] = Storage::getDocumentById($fileId); - $discovery_parsed = null; - try { - $discovery = $this->getDiscovery(); - - $loadEntities = libxml_disable_entity_loader(true); - $discovery_parsed = simplexml_load_string($discovery); - libxml_disable_entity_loader($loadEntities); - - if ($discovery_parsed === false) { - $this->cache->remove('discovery.xml'); - $wopiRemote = $this->settings->getAppValue('richdocuments', 'wopi_url'); - - return array( - 'status' => 'error', - 'message' => $this->l10n->t('Collabora Online: discovery.xml from "%s" is not a well-formed XML string.', array($wopiRemote)), - 'hint' => $this->l10n->t('Please contact the "%s" administrator.', array($wopiRemote)) - ); - } - } - catch (ResponseException $e) { - return array( - 'status' => 'error', - 'message' => $e->getMessage(), - 'hint' => $e->getHint() - ); - } - - $fileIds = array(); - $documents = array(); - $lolang = strtolower(str_replace('_', '-', $this->settings->getUserValue($this->uid, 'core', 'lang', 'en'))); - foreach ($found as $key=>$document) { - if (is_object($document)){ - $documents[] = $document->getData(); - } else { - $documents[$key] = $document; - } - $documents[$key]['icon'] = preg_replace('/\.png$/', '.svg', \OCP\Template::mimetype_icon($document['mimetype'])); - $documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']); - $documents[$key]['urlsrc'] = $this->getWopiSrcUrl($discovery_parsed, $document['mimetype'], 'edit'); - $documents[$key]['lolang'] = $lolang; - $fileIds[] = $document['fileid']; - } - - usort($documents, function($a, $b){ - return @$b['mtime']-@$a['mtime']; - }); - - $session = new Db\Session(); - $sessions = $session->getCollectionBy('file_id', $fileIds); + return $this->prepareDocuments($documents); + } - $members = array(); - $member = new Db\Member(); - foreach ($sessions as $session) { - $members[$session['es_id']] = $member->getActiveCollection($session['es_id']); - } - return array( - 'status' => 'success', 'documents' => $documents,'sessions' => $sessions,'members' => $members - ); + /** + * @NoAdminRequired + * 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 function listAll(){ + return $this->prepareDocuments(Storage::getDocuments()); } } diff --git a/js/documents.js b/js/documents.js index 0ad57a26..15a5a84b 100644 --- a/js/documents.js +++ b/js/documents.js @@ -12,9 +12,9 @@ $.widget('oc.documentGrid', { }, - render : function(){ + render : function(fileId){ var that = this; - jQuery.when(this._load()) + jQuery.when(this._load(fileId)) .then(function(){ that._render(); }); @@ -78,10 +78,17 @@ $.widget('oc.documentGrid', { } }, - _load : function (){ + _load : function (fileId){ var that = this; var def = new $.Deferred(); - $.getJSON(OC.generateUrl('apps/richdocuments/ajax/documents/list')) + var url = 'apps/richdocuments/ajax/documents/list'; + var dataObj = {}; + if (fileId){ + url = 'apps/richdocuments/ajax/documents/get/{fileId}'; + dataObj = { fileId: fileId }; + } + + $.getJSON(OC.generateUrl(url, dataObj)) .done(function (result) { if (!result || result.status === 'error') { documentsMain.loadError = true; @@ -349,7 +356,7 @@ var documentsMain = { } } - documentsMain.show(); + documentsMain.show(fileId); if (fileId) { documentsMain.overlay.documentOverlay('show'); @@ -671,12 +678,12 @@ var documentsMain = { } }, - show: function(){ + show: function(fileId){ if (documentsMain.isGuest){ return; } documentsMain.UI.showProgress(t('richdocuments', 'Loading documents...')); - documentsMain.docs.documentGrid('render'); + documentsMain.docs.documentGrid('render', fileId); documentsMain.UI.hideProgress(); } }; diff --git a/lib/storage.php b/lib/storage.php index ce65d39c..51cf6b66 100644 --- a/lib/storage.php +++ b/lib/storage.php @@ -72,6 +72,26 @@ class Storage { return $list; } + public static function getDocumentById($fileId){ + $root = \OC::$server->getUserFolder(); + $ret = array(); + + // If type of fileId is a string, then it + // doesn't work for shared documents, lets cast to int everytime + $document = $root->getById((int)$fileId)[0]; + if ($document === null){ + error_log('File with file id, ' . $fileId . ', not found'); + return $ret; + } + + $ret['mimetype'] = $document->getMimeType(); + $ret['path'] = $root->getRelativePath($document->getPath()); + $ret['name'] = $document->getName(); + $ret['fileid'] = $fileId; + + return $ret; + } + public static function resolvePath($fileId){ $list = array_filter( self::searchDocuments(),