Skip to content

Commit 2ffa5bf

Browse files
committed
feat(capabilities): dynamic mimetype discovery from wopi xml
Signed-off-by: Florian Wagner <f.wagner81@gmail.com>
1 parent 6784af9 commit 2ffa5bf

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

lib/Capabilities.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Richdocuments;
1010

1111
use OCA\Richdocuments\Service\CapabilitiesService;
12+
use OCA\Richdocuments\Service\DiscoveryService;
1213
use OCP\App\IAppManager;
1314
use OCP\Capabilities\ICapability;
1415
use OCP\IURLGenerator;
@@ -97,6 +98,7 @@ public function __construct(
9798
private IAppManager $appManager,
9899
private ?string $userId,
99100
private IURLGenerator $urlGenerator,
101+
private DiscoveryService $discoveryService,
100102
) {
101103
}
102104

@@ -141,7 +143,7 @@ public function getCapabilities() {
141143
* @return list<string>
142144
*/
143145
public function getDefaultMimetypes(): array {
144-
$defaultMimetypes = self::MIMETYPES;
146+
$defaultMimetypes = $this->discoveryService->getSupportedMimeTypes() ?: self::MIMETYPES;
145147

146148
if (!$this->capabilitiesService->hasOtherOOXMLApps()) {
147149
array_push($defaultMimetypes, ...self::MIMETYPES_MSOFFICE);
@@ -151,7 +153,7 @@ public function getDefaultMimetypes(): array {
151153
$defaultMimetypes[] = 'application/pdf';
152154
}
153155

154-
return $defaultMimetypes;
156+
return array_unique($defaultMimetypes);
155157
}
156158

157159
/**

lib/Service/DiscoveryService.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,35 @@ private function getParsed(): SimpleXMLElement {
127127
throw new \Exception('Could not parse discovery XML');
128128
}
129129
}
130+
131+
/**
132+
* Dynamically get all supported mime types out of the discovery XML
133+
* @todo Currently no categories given from endpoint
134+
*
135+
* @return array
136+
* @throws \Exception
137+
*/
138+
public function getSupportedMimeTypes(): array
139+
{
140+
$mimeTypes = [];
141+
142+
try {
143+
$parsed = $this->getParsed();
144+
} catch (\Exception $e) {
145+
// @todo fallback to a default set instead of empty array
146+
return $mimeTypes;
147+
}
148+
149+
foreach ($parsed->xpath('//net-zone/app') as $app) {
150+
$mimeTypeName = (string)$app['name']; // (e.g. "application/pdf")
151+
152+
// Filter (Settings, Capabilities, etc.)
153+
if (!str_contains($mimeTypeName, '/')) {
154+
continue;
155+
}
156+
$mimeTypes[] = $mimeTypeName;
157+
}
158+
159+
return array_unique($mimeTypes);
160+
}
130161
}

0 commit comments

Comments
 (0)