From 14eda814f53020adf3b9cbb2a397159aba257e5d Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Thu, 10 Mar 2016 08:00:52 -0400 Subject: [PATCH 1/2] Read discovery.xml capabilities --- controller/documentcontroller.php | 74 +++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index 9d33fe72..591e1718 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -50,6 +50,51 @@ class DocumentController extends Controller{ $this->logger = $logger; } + /** + * @param \SimpleXMLElement $discovery + * @param string $mimetype + * @param string $action + */ + private function getWopiSrcUrl($discovery, $mimetype, $action) { + if(is_null($discovery) || $discovery == false) { + return null; + } + + $result = $discovery->xpath(sprintf('/wopi-discovery/net-zone/app[@name=\'%s\']/action[@name=\'%s\']', $mimetype, $action)); + if ($result && count($result) > 0) { + return (string)$result[0]['urlsrc']; + } + + return null; + } + + /** + * @param string $wopiDiscovery + */ + private function requestDiscovery($wopiDiscovery) { + try { + $wopiClient = \OC::$server->getHTTPClientService()->newClient(); + $xmlBody = $wopiClient->get($wopiDiscovery)->getBody(); + if ($xmlBody) { + $loadEntities = libxml_disable_entity_loader(true); + $data = simplexml_load_string($xmlBody); + libxml_disable_entity_loader($loadEntities); + if ($data !== false) { + $this->cache->set('discovery.xml', $xmlBody, 3600); + } + else { + $this->logger->debug('failure discovery.xml not well-formed XML string'); + } + } + else { + $this->logger->debug('failure response discovery.xml'); + } + } catch (\Exception $e) { + $this->logger->debug( + sprintf('Error getting discovery.xml: %s', $e->getMessage())); + } + } + /** * @NoAdminRequired * @NoCSRFRequired @@ -89,29 +134,7 @@ class DocumentController extends Controller{ isset($parts['host']) ? $parts['host'] : '', isset($parts['port']) ? ":" . $parts['port'] : '', "/hosting/discovery" ); - - try { - $wopiClient = \OC::$server->getHTTPClientService()->newClient(); - $xml = $wopiClient->get($wopiDiscovery)->getBody(); - if ($xml) { - $loadEntities = libxml_disable_entity_loader(true); - $data = simplexml_load_string($xml); - libxml_disable_entity_loader($loadEntities); - if ($data !== false) { - // default ttl - $this->cache->set('discovery.xml', $xml); - } - else { - $this->logger->error('failure discovery.xml not well-formed XML string'); - } - } - else { - $this->logger->error('failure response discovery.xml'); - } - } catch (\Exception $e) { - $this->logger->error( - sprintf('Error getting discovery.xml: %s', $e->getMessage())); - } + $this->requestDiscovery($wopiDiscovery); } } @@ -393,6 +416,10 @@ class DocumentController extends Controller{ */ public function listAll(){ $found = Storage::getDocuments(); + $data = $this->cache->get('discovery.xml'); + $loadEntities = libxml_disable_entity_loader(true); + $discovery = simplexml_load_string($data); + libxml_disable_entity_loader($loadEntities); $fileIds = array(); $documents = array(); @@ -404,6 +431,7 @@ class DocumentController extends Controller{ } $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, $document['mimetype'], 'edit'); $fileIds[] = $document['fileid']; } From 3a2d2a219f1dc94e64b26b79ad8e86c37b764874 Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Thu, 10 Mar 2016 10:41:07 -0400 Subject: [PATCH 2/2] JS handle urlsrc attribute --- js/documents.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/js/documents.js b/js/documents.js index a9ea2ebd..11ddbe3e 100644 --- a/js/documents.js +++ b/js/documents.js @@ -30,6 +30,7 @@ $.widget('oc.documentGrid', { a.css('background-image', 'url("'+document.icon+'")') .attr('href', OC.generateUrl('apps/files/download{file}',{file:document.path})) .attr('original-title', document.path) + .attr('urlsrc', document.urlsrc) .find('label').text(document.name) ; @@ -190,12 +191,12 @@ var documentsMain = { return; } - //TODO: Get WOPISrc from the discovery XML. + var urlsrc = $('li[data-id='+ documentsMain.fileId +']>a').attr('urlsrc'); var url = OC.generateUrl('apps/richdocuments/wopi/files/{file_id}/contents?access_token={token}', {file_id: documentsMain.fileId, token: encodeURIComponent(result.token)}); documentsMain.url = window.location.protocol + '//' + window.location.host + url; - var viewer = window.location.protocol + '//' + window.location.host + '/loleaflet/dist/loleaflet.html?' + + var viewer = urlsrc + 'file_path=' + encodeURIComponent(documentsMain.url) + '&host=' + 'ws://' + window.location.hostname + ':9980' + '&permission=' + 'view' + @@ -718,6 +719,12 @@ $(document).ready(function() { return; } + var item = $(this).find('a'); + if (item.attr('urlsrc') === undefined) { + OC.Notification.showTemporary(t('richdocuments', 'Failed to open ' + item.attr('original-title') + ', file not supported.')); + return; + } + documentsMain.prepareSession(); if ($(this).attr('data-id')){ documentsMain.joinSession($(this).attr('data-id'));