Skip to content

Commit

Permalink
feat: re-add ability to specify multiple conversion tuples per provider
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Jan 14, 2025
1 parent 23e4606 commit 1073cfd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/files/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getCapabilities(): array {

'file_conversions' => array_map(function (ConversionMimeTuple $mimeTuple) {
return $mimeTuple->jsonSerialize();
}, $this->fileConversionManager->getMimeTypes())
}, $this->fileConversionManager->getMimeTypes()),
],
];
}
Expand Down
10 changes: 6 additions & 4 deletions apps/testing/lib/Conversion/ConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public function __construct(
) {
}

public function getSupportedMimeType(): ConversionMimeTuple {
return new ConversionMimeTuple('image/jpeg', [
'image/png' => $this->l10n->t('Image (.png)'),
]);
public function getSupportedMimeTypes(): array {
return [
new ConversionMimeTuple('image/jpeg', [
'image/png' => $this->l10n->t('Image (.png)'),
])
];
}

public function convertFile(File $file, string $targetMimeType): mixed {
Expand Down
40 changes: 28 additions & 12 deletions lib/private/Files/Conversion/ConversionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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;
Expand Down Expand Up @@ -53,10 +54,13 @@ public function hasProviders(): bool {
}

public function getMimeTypes(): array {
/** @var list<ConversionMimeTuple> */
$mimeTypes = [];

foreach ($this->getProviders() as $provider) {
$mimeTypes[] = $provider->getSupportedMimeType();
/** @var list<ConversionMimeTuple> */
$providerMimeTypes = $provider->getSupportedMimeTypes();
$mimeTypes = array_merge($mimeTypes, $providerMimeTypes);
}

return $mimeTypes;
Expand All @@ -75,18 +79,9 @@ public function convert(File $file, string $targetMimeType, ?string $destination
}

$fileMimeType = $file->getMimetype();
$validProvider = null;
foreach ($this->getProviders() as $provider) {
['from' => $from, 'to' => $to] = $provider->getSupportedMimeType()->jsonSerialize();
$canConvert = ($from === $fileMimeType) && array_key_exists($targetMimeType, $to);
$validProvider = $this->getValidProvider($fileMimeType, $targetMimeType);

if ($canConvert) {
$validProvider = $provider;
break;
}
}

if ($validProvider) {
if ($validProvider !== null) {
$convertedFile = $validProvider->convertFile($file, $targetMimeType);

if ($destination !== null) {
Expand Down Expand Up @@ -133,4 +128,25 @@ public function getProviders(): array {
private function writeToDestination(string $destination, mixed $content): File {
return $this->rootFolder->newFile($destination, $content);
}

private function getValidProvider(string $fileMimeType, string $targetMimeType): ?IConversionProvider {
$validProvider = null;
foreach ($this->getProviders() as $provider) {
$suitableMimeTypes = array_filter(
$provider->getSupportedMimeTypes(),
function (ConversionMimeTuple $mimeTuple) use ($fileMimeType, $targetMimeType) {
['from' => $from, 'to' => $to] = $mimeTuple->jsonSerialize();

return ($from === $fileMimeType) && array_key_exists($targetMimeType, $to);
}
);

if (!empty($suitableMimeTypes)) {
$validProvider = $provider;
break;
}
}

return $validProvider;
}
}
6 changes: 4 additions & 2 deletions lib/public/Files/Conversion/IConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
*/
interface IConversionProvider {
/**
* Get the MIME type tuple this conversion provider supports
* Get an array of MIME type tuples this conversion provider supports
*
* @return list<ConversionMimeTuple>
*
* @since 31.0.0
*/
public function getSupportedMimeType(): ConversionMimeTuple;
public function getSupportedMimeTypes(): array;

/**
* Convert a file to a given MIME type
Expand Down

0 comments on commit 1073cfd

Please sign in to comment.