-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): basic file conversion api
Signed-off-by: Elizabeth Danzberger <[email protected]>
- Loading branch information
Showing
10 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
|
||
namespace OC\Core\Controller; | ||
|
||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\ApiRoute; | ||
use OCP\AppFramework\Http\Attribute\UserRateLimit; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\Conversion\IConversionManager; | ||
use OCP\Files\File; | ||
use OCP\Files\IRootFolder; | ||
use OCP\IL10N; | ||
use OCP\IRequest; | ||
|
||
class ConversionApiController extends OCSController { | ||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private IConversionManager $conversionManager, | ||
private IRootFolder $rootFolder, | ||
private IL10N $l10n, | ||
private ?string $userId, | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
#[UserRateLimit(limit: 25, period: 120)] | ||
#[ApiRoute(verb: 'POST', url: '/convert', root: '/conversion')] | ||
public function convert(int $fileId, string $targetMimeType, ?string $destination = null): DataResponse { | ||
$userFolder = $this->rootFolder->getUserFolder($this->userId); | ||
$file = $userFolder->getFirstNodeById($fileId); | ||
|
||
if (!($file instanceof File)) { | ||
return new DataResponse([ | ||
'message' => $this->l10n->t('File not found'), | ||
], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
try { | ||
$destination = $userFolder->getFullpath($destination); | ||
$convertedFile = $this->conversionManager->convert($file, $targetMimeType, $destination); | ||
} catch (\Exception $e) { | ||
return new DataResponse([ | ||
'message' => $e->getMessage(), | ||
], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
return new DataResponse([ | ||
'path' => $convertedFile, | ||
], Http::STATUS_CREATED); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Conversion; | ||
|
||
use OC\AppFramework\Bootstrap\Coordinator; | ||
use OCP\Conversion\ConversionMimeTuple; | ||
use OCP\Conversion\IConversionManager; | ||
use OCP\Conversion\IConversionProvider; | ||
use OCP\Files\File; | ||
use OCP\Files\IRootFolder; | ||
use OCP\ITempManager; | ||
use OCP\PreConditionNotMetException; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
class ConversionManager implements IConversionManager { | ||
/** @var ?IConversionProvider[] */ | ||
private ?array $providers = null; | ||
|
||
public function __construct( | ||
private Coordinator $coordinator, | ||
private ContainerInterface $serverContainer, | ||
private IRootFolder $rootFolder, | ||
private ITempManager $tempManager, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function hasProviders(): bool { | ||
$context = $this->coordinator->getRegistrationContext(); | ||
return !empty($context->getConversionProviders()); | ||
} | ||
|
||
public function getMimeTypes(): array { | ||
$mimeTypes = []; | ||
|
||
foreach ($this->getProviders() as $provider) { | ||
$mimeTypes[] = $provider->getSupportedMimeTypes(); | ||
} | ||
|
||
return $mimeTypes; | ||
} | ||
|
||
public function convert(File $file, string $targetMimeType, ?string $destination = null): string { | ||
if (!$this->hasProviders()) { | ||
throw new PreConditionNotMetException('No conversion providers available'); | ||
} | ||
|
||
$fileMimeType = $file->getMimetype(); | ||
foreach ($this->getProviders() as $provider) { | ||
$availableProviderConversions = array_filter( | ||
$provider->getSupportedMimeTypes(), | ||
function (ConversionMimeTuple $mimeTuple) use ($fileMimeType, $targetMimeType) { | ||
['from' => $from, 'to' => $to] = $mimeTuple->jsonSerialize(); | ||
|
||
return $from === $fileMimeType && $to === $targetMimeType; | ||
} | ||
); | ||
|
||
if (!empty($availableProviderConversions)) { | ||
$convertedFile = $provider->convertFile($file, $targetMimeType); | ||
|
||
if ($destination !== null) { | ||
$convertedFile = $this->writeToDestination($destination, $convertedFile); | ||
return $convertedFile->getInternalPath(); | ||
} | ||
|
||
$tmp = $this->tempManager->getTemporaryFile(); | ||
file_put_contents($tmp, $convertedFile); | ||
|
||
return $tmp; | ||
} | ||
} | ||
|
||
throw new RuntimeException('Could not convert file'); | ||
} | ||
|
||
public function getProviders(): array { | ||
if ($this->providers !== null) { | ||
return $this->providers; | ||
} | ||
|
||
$context = $this->coordinator->getRegistrationContext(); | ||
$this->providers = []; | ||
|
||
foreach ($context->getConversionProviders() as $providerRegistration) { | ||
$class = $providerRegistration->getService(); | ||
|
||
try { | ||
$this->providers[$class] = $this->serverContainer->get($class); | ||
} catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) { | ||
$this->logger->error('Failed to load conversion provider ' . $class, [ | ||
'exception' => $e, | ||
]); | ||
} | ||
} | ||
|
||
return $this->providers; | ||
} | ||
|
||
private function writeToDestination(string $destination, mixed $content): File { | ||
return $this->rootFolder->newFile($destination, $content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCP\Conversion; | ||
|
||
use JsonSerializable; | ||
|
||
/** | ||
* A tuple-like object representing both an original and target | ||
* MIME type for a file conversion | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
class ConversionMimeTuple implements JsonSerializable { | ||
/** | ||
* @param string $from The original MIME type of a file | ||
* @param string $to The desired MIME type for the file | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function __construct( | ||
private string $from, | ||
private string $to, | ||
) { | ||
} | ||
|
||
public function jsonSerialize(): array { | ||
return [ | ||
'from' => $this->from, | ||
'to' => $this->to, | ||
]; | ||
} | ||
} |
Oops, something went wrong.