Merge pull request #15 from hcvcastro/master

Read discovery.xml capabilities
This commit is contained in:
Andras Timar 2016-03-10 23:10:49 +01:00
commit c7b2c42085
2 changed files with 60 additions and 25 deletions

View File

@ -50,6 +50,51 @@ class DocumentController extends Controller{
$this->logger = $logger; $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 * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
@ -89,29 +134,7 @@ class DocumentController extends Controller{
isset($parts['host']) ? $parts['host'] : '', isset($parts['host']) ? $parts['host'] : '',
isset($parts['port']) ? ":" . $parts['port'] : '', isset($parts['port']) ? ":" . $parts['port'] : '',
"/hosting/discovery" ); "/hosting/discovery" );
$this->requestDiscovery($wopiDiscovery);
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()));
}
} }
} }
@ -393,6 +416,10 @@ class DocumentController extends Controller{
*/ */
public function listAll(){ public function listAll(){
$found = Storage::getDocuments(); $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(); $fileIds = array();
$documents = 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]['icon'] = preg_replace('/\.png$/', '.svg', \OCP\Template::mimetype_icon($document['mimetype']));
$documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']); $documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']);
$documents[$key]['urlsrc'] = $this->getWopiSrcUrl($discovery, $document['mimetype'], 'edit');
$fileIds[] = $document['fileid']; $fileIds[] = $document['fileid'];
} }

View File

@ -30,6 +30,7 @@ $.widget('oc.documentGrid', {
a.css('background-image', 'url("'+document.icon+'")') a.css('background-image', 'url("'+document.icon+'")')
.attr('href', OC.generateUrl('apps/files/download{file}',{file:document.path})) .attr('href', OC.generateUrl('apps/files/download{file}',{file:document.path}))
.attr('original-title', document.path) .attr('original-title', document.path)
.attr('urlsrc', document.urlsrc)
.find('label').text(document.name) .find('label').text(document.name)
; ;
@ -190,12 +191,12 @@ var documentsMain = {
return; 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}', var url = OC.generateUrl('apps/richdocuments/wopi/files/{file_id}/contents?access_token={token}',
{file_id: documentsMain.fileId, token: encodeURIComponent(result.token)}); {file_id: documentsMain.fileId, token: encodeURIComponent(result.token)});
documentsMain.url = window.location.protocol + '//' + window.location.host + url; 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) + 'file_path=' + encodeURIComponent(documentsMain.url) +
'&host=' + 'ws://' + window.location.hostname + ':9980' + '&host=' + 'ws://' + window.location.hostname + ':9980' +
'&permission=' + 'view' + '&permission=' + 'view' +
@ -718,6 +719,12 @@ $(document).ready(function() {
return; 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(); documentsMain.prepareSession();
if ($(this).attr('data-id')){ if ($(this).attr('data-id')){
documentsMain.joinSession($(this).attr('data-id')); documentsMain.joinSession($(this).attr('data-id'));