Merge pull request #15 from hcvcastro/master

Read discovery.xml capabilities
pull/1/head
Andras Timar 8 years ago
commit c7b2c42085

@ -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'];
}

@ -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'));

Loading…
Cancel
Save