diff --git a/lib/Capabilities.php b/lib/Capabilities.php index 6db67c301a..243ca7d0be 100644 --- a/lib/Capabilities.php +++ b/lib/Capabilities.php @@ -9,6 +9,7 @@ namespace OCA\Richdocuments; use OCA\Richdocuments\Service\CapabilitiesService; +use OCA\Richdocuments\Service\DiscoveryService; use OCP\App\IAppManager; use OCP\Capabilities\ICapability; use OCP\IURLGenerator; @@ -97,6 +98,7 @@ public function __construct( private IAppManager $appManager, private ?string $userId, private IURLGenerator $urlGenerator, + private DiscoveryService $discoveryService, ) { } @@ -141,7 +143,7 @@ public function getCapabilities() { * @return list */ public function getDefaultMimetypes(): array { - $defaultMimetypes = self::MIMETYPES; + $defaultMimetypes = $this->discoveryService->getSupportedMimeTypes() ?: self::MIMETYPES; if (!$this->capabilitiesService->hasOtherOOXMLApps()) { array_push($defaultMimetypes, ...self::MIMETYPES_MSOFFICE); @@ -151,7 +153,7 @@ public function getDefaultMimetypes(): array { $defaultMimetypes[] = 'application/pdf'; } - return $defaultMimetypes; + return array_values(array_unique($defaultMimetypes)); } /** diff --git a/lib/Service/DiscoveryService.php b/lib/Service/DiscoveryService.php index 9a001b39a8..6b47620bc0 100644 --- a/lib/Service/DiscoveryService.php +++ b/lib/Service/DiscoveryService.php @@ -127,4 +127,34 @@ private function getParsed(): SimpleXMLElement { throw new \Exception('Could not parse discovery XML'); } } + + /** + * Dynamically get all supported mime types out of the discovery XML + * @todo Currently no categories given from endpoint + * + * @return array + * @throws \Exception + */ + public function getSupportedMimeTypes(): array { + $mimeTypes = []; + + try { + $parsed = $this->getParsed(); + } catch (\Exception $e) { + // @todo fallback to a default set instead of empty array + return $mimeTypes; + } + + foreach ($parsed->xpath('//net-zone/app') as $app) { + $mimeTypeName = (string)$app['name']; // (e.g. "application/pdf") + + // Filter (Settings, Capabilities, etc.) + if (!str_contains($mimeTypeName, '/')) { + continue; + } + $mimeTypes[] = $mimeTypeName; + } + + return array_values(array_unique($mimeTypes)); + } }