Skip to content

Commit

Permalink
feat: add localized MIME type names to conversion 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 13, 2025
1 parent 958dec1 commit 4901f72
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 41 deletions.
15 changes: 7 additions & 8 deletions apps/testing/lib/Conversion/ConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 19 additions & 20 deletions lib/private/Files/Conversion/ConversionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Files/Conversion/ConversionMimeTuple.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class ConversionMimeTuple implements JsonSerializable {
/**
* @param string $from The original MIME type of a file
* @param array<string> $to The desired MIME type for the file
* @param array<string,string> $to The desired MIME type for the file mapped to its translated name
*
* @since 31.0.0
*/
Expand Down
15 changes: 3 additions & 12 deletions lib/public/Files/Conversion/IConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConversionMimeTuple>
*
* @since 31.0.0
*/
public function getSupportedMimeTypes(): array;
public function getSupportedMimeType(): ConversionMimeTuple;

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

0 comments on commit 4901f72

Please sign in to comment.