Skip to content

Commit

Permalink
feat: add preferred apps/providers and use them first
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Jan 13, 2025
1 parent 4901f72 commit ac0843f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/private/Files/Conversion/ConversionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@
use Throwable;

class ConversionManager implements IConversionManager {
/** @var ?IConversionProvider[] */
private ?array $providers = null;
/** @var string[] */
private array $preferredApps = [
'richdocuments',
];

/** @var IConversionProvider[] */
private array $preferredProviders = [];

/** @var IConversionProvider[] */
private array $providers = [];

public function __construct(
private Coordinator $coordinator,
Expand All @@ -51,7 +59,7 @@ public function getMimeTypes(): array {
array_push($mimeTypes, $provider->getSupportedMimeType());
}

return array_merge([], $mimeTypes);
return $mimeTypes;
}

public function convert(File $file, string $targetMimeType, ?string $destination = null): string {
Expand Down Expand Up @@ -96,17 +104,21 @@ public function convert(File $file, string $targetMimeType, ?string $destination
}

public function getProviders(): array {
if ($this->providers !== null) {
if (count($this->providers) > 0) {
return $this->providers;
}

$context = $this->coordinator->getRegistrationContext();
$this->providers = [];

foreach ($context->getFileConversionProviders() as $providerRegistration) {
$class = $providerRegistration->getService();
$appId = $providerRegistration->getAppId();

try {
if (in_array($appId, $this->preferredApps)) {
$this->preferredProviders[$class] = $this->serverContainer->get($class);
continue;
}

$this->providers[$class] = $this->serverContainer->get($class);
} catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
$this->logger->error('Failed to load file conversion provider ' . $class, [
Expand All @@ -115,7 +127,8 @@ public function getProviders(): array {
}
}

return $this->providers;
// return $this->providers;
return array_merge([], $this->preferredProviders, $this->providers);
}

private function writeToDestination(string $destination, mixed $content): File {
Expand Down

0 comments on commit ac0843f

Please sign in to comment.