Skip to content

Commit

Permalink
Merge pull request #50219 from nextcloud/fix/file-conversion-missing-…
Browse files Browse the repository at this point in the history
…extension
  • Loading branch information
provokateurin authored Jan 16, 2025
2 parents bca2685 + 1497639 commit d1b5488
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 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\ConversionMimeProvider;
use OCP\Files\Conversion\IConversionManager;
use OCP\Files\Conversion\IConversionProvider;
use OCP\Files\File;
Expand Down Expand Up @@ -61,22 +60,6 @@ public function getProviders(): array {
return $providers;
}

/**
* @param string $mime
* @return list<ConversionMimeProvider>
*/
private function getProvidersForMime(string $mime): array {
$mimeTypes = $this->getProviders();
$filtered = array_filter(
$mimeTypes,
function (ConversionMimeProvider $mimeProvider) use ($mime) {
return $mimeProvider->getFrom() === $mime;
}
);

return array_values($filtered);
}

public function convert(File $file, string $targetMimeType, ?string $destination = null): string {
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No file conversion providers available');
Expand All @@ -92,16 +75,16 @@ public function convert(File $file, string $targetMimeType, ?string $destination
$fileMimeType = $file->getMimetype();
$validProvider = $this->getValidProvider($fileMimeType, $targetMimeType);

$targetExtension = '';
foreach ($this->getProvidersForMime($fileMimeType) as $mimeProvider) {
if ($mimeProvider->getTo() === $targetMimeType) {
$targetExtension = $mimeProvider->getExtension();
break;
}
}

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

$targetExtension = '';
foreach ($validProvider->getSupportedMimeTypes() as $mimeProvider) {
if ($mimeProvider->getTo() === $targetMimeType) {
$targetExtension = $mimeProvider->getExtension();
break;
}
}

// If destination not provided, we use the same path
// as the original file, but with the new extension
Expand All @@ -122,10 +105,6 @@ public function convert(File $file, string $targetMimeType, ?string $destination
* @return list<IConversionProvider>
*/
private function getRegisteredProviders(): array {
if (count($this->providers) > 0) {
return $this->providers;
}

$context = $this->coordinator->getRegistrationContext();
foreach ($context->getFileConversionProviders() as $providerRegistration) {
$class = $providerRegistration->getService();
Expand Down

0 comments on commit d1b5488

Please sign in to comment.