diff --git a/apps/testing/lib/Conversion/ConversionProvider.php b/apps/testing/lib/Conversion/ConversionProvider.php index daa2d89288dbb..de6eecfa2ddde 100644 --- a/apps/testing/lib/Conversion/ConversionProvider.php +++ b/apps/testing/lib/Conversion/ConversionProvider.php @@ -12,19 +12,18 @@ use OCP\Files\Conversion\ConversionMimeTuple; use OCP\Files\Conversion\IConversionProvider; use OCP\Files\File; +use OCP\IL10N; class ConversionProvider implements IConversionProvider { - - public function getName(): string { - return 'testing'; + public function __construct( + private IL10N $l10n, + ) { } - public function getSupportedMimeTypes(): array { - $jpegConversions = new ConversionMimeTuple('image/jpeg', [ - 'image/png', + public function getSupportedMimeType(): ConversionMimeTuple { + return new ConversionMimeTuple('image/jpeg', [ + 'image/png' => $this->l10n->t('Image (.png)'), ]); - - return [$jpegConversions]; } public function convertFile(File $file, string $targetMimeType): mixed { diff --git a/lib/private/Files/Conversion/ConversionManager.php b/lib/private/Files/Conversion/ConversionManager.php index b500aca925afd..7c53148f0d556 100644 --- a/lib/private/Files/Conversion/ConversionManager.php +++ b/lib/private/Files/Conversion/ConversionManager.php @@ -11,7 +11,6 @@ use OC\AppFramework\Bootstrap\Coordinator; use OC\SystemConfig; -use OCP\Files\Conversion\ConversionMimeTuple; use OCP\Files\Conversion\IConversionManager; use OCP\Files\Conversion\IConversionProvider; use OCP\Files\File; @@ -49,10 +48,10 @@ public function getMimeTypes(): array { $mimeTypes = []; foreach ($this->getProviders() as $provider) { - $mimeTypes[] = $provider->getSupportedMimeTypes(); + array_push($mimeTypes, $provider->getSupportedMimeType()); } - return array_merge([], ...$mimeTypes); + return array_merge([], $mimeTypes); } public function convert(File $file, string $targetMimeType, ?string $destination = null): string { @@ -68,29 +67,29 @@ public function convert(File $file, string $targetMimeType, ?string $destination } $fileMimeType = $file->getMimetype(); + $validProvider = null; foreach ($this->getProviders() as $provider) { - $availableProviderConversions = array_filter( - $provider->getSupportedMimeTypes(), - function (ConversionMimeTuple $mimeTuple) use ($fileMimeType, $targetMimeType) { - ['from' => $from, 'to' => $to] = $mimeTuple->jsonSerialize(); + ['from' => $from, 'to' => $to] = $provider->getSupportedMimeType()->jsonSerialize(); + $canConvert = ($from === $fileMimeType) && array_key_exists($targetMimeType, $to); - return $from === $fileMimeType && in_array($targetMimeType, $to); - } - ); + if ($canConvert) { + $validProvider = $provider; + break; + } + } - if (!empty($availableProviderConversions)) { - $convertedFile = $provider->convertFile($file, $targetMimeType); + if ($validProvider) { + $convertedFile = $validProvider->convertFile($file, $targetMimeType); - if ($destination !== null) { - $convertedFile = $this->writeToDestination($destination, $convertedFile); - return $convertedFile->getInternalPath(); - } + if ($destination !== null) { + $convertedFile = $this->writeToDestination($destination, $convertedFile); + return $convertedFile->getInternalPath(); + } - $tmp = $this->tempManager->getTemporaryFile(); - file_put_contents($tmp, $convertedFile); + $tmp = $this->tempManager->getTemporaryFile(); + file_put_contents($tmp, $convertedFile); - return $tmp; - } + return $tmp; } throw new RuntimeException('Could not convert file'); diff --git a/lib/public/Files/Conversion/ConversionMimeTuple.php b/lib/public/Files/Conversion/ConversionMimeTuple.php index d2d058c272b73..e06520dbe71b3 100644 --- a/lib/public/Files/Conversion/ConversionMimeTuple.php +++ b/lib/public/Files/Conversion/ConversionMimeTuple.php @@ -20,7 +20,7 @@ class ConversionMimeTuple implements JsonSerializable { /** * @param string $from The original MIME type of a file - * @param array $to The desired MIME type for the file + * @param array $to The desired MIME type for the file mapped to its translated name * * @since 31.0.0 */ diff --git a/lib/public/Files/Conversion/IConversionProvider.php b/lib/public/Files/Conversion/IConversionProvider.php index 8e00e4ad32dea..1b9d1f8af8033 100644 --- a/lib/public/Files/Conversion/IConversionProvider.php +++ b/lib/public/Files/Conversion/IConversionProvider.php @@ -19,22 +19,13 @@ */ interface IConversionProvider { /** - * Get the name of the provider + * Get the MIME type tuple this conversion provider supports * - * @return string + * @return ConversionMimeTuple * * @since 31.0.0 */ - public function getName(): string; - - /** - * Get an array of MIME types which are available for conversion - * - * @return array - * - * @since 31.0.0 - */ - public function getSupportedMimeTypes(): array; + public function getSupportedMimeType(): ConversionMimeTuple; /** * Convert a file to a given MIME type