Skip to content

Commit 97602f5

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

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;
@@ -96,6 +97,7 @@ public function __construct(
9697
private IAppManager $appManager,
9798
private ?string $userId,
9899
private IURLGenerator $urlGenerator,
100+
private DiscoveryService $discoveryService,
99101
) {
100102
}
101103

@@ -140,7 +142,7 @@ public function getCapabilities() {
140142
* @return list<string>
141143
*/
142144
public function getDefaultMimetypes(): array {
143-
$defaultMimetypes = self::MIMETYPES;
145+
$defaultMimetypes = $this->discoveryService->getSupportedMimeTypes() ?: self::MIMETYPES;
144146

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

153-
return $defaultMimetypes;
155+
return array_unique($defaultMimetypes);
154156
}
155157

156158
/**

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)