|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +namespace OC\Core\Controller; |
| 12 | + |
| 13 | +use OCP\AppFramework\Http; |
| 14 | +use OCP\AppFramework\Http\Attribute\ApiRoute; |
| 15 | +use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
| 16 | +use OCP\AppFramework\Http\Attribute\UserRateLimit; |
| 17 | +use OCP\AppFramework\Http\DataResponse; |
| 18 | +use OCP\AppFramework\OCS\OCSException; |
| 19 | +use OCP\AppFramework\OCS\OCSNotFoundException; |
| 20 | +use OCP\AppFramework\OCSController; |
| 21 | +use OCP\Conversion\IConversionManager; |
| 22 | +use OCP\Files\File; |
| 23 | +use OCP\Files\IRootFolder; |
| 24 | +use OCP\IRequest; |
| 25 | + |
| 26 | +class ConversionApiController extends OCSController { |
| 27 | + public function __construct( |
| 28 | + string $appName, |
| 29 | + IRequest $request, |
| 30 | + private IConversionManager $conversionManager, |
| 31 | + private IRootFolder $rootFolder, |
| 32 | + private ?string $userId, |
| 33 | + ) { |
| 34 | + parent::__construct($appName, $request); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Converts a file from one MIME type to another |
| 39 | + * |
| 40 | + * @param int $fileId ID of the file to be converted |
| 41 | + * @param string $targetMimeType The MIME type to which you want to convert the file |
| 42 | + * @param string|null $destination The target path of the converted file. Written to a temporary file if left empty |
| 43 | + * |
| 44 | + * @return DataResponse<Http::STATUS_CREATED, array{path: string}, array{}> |
| 45 | + * |
| 46 | + * 201: File was converted and written to the destination or temporary file |
| 47 | + * |
| 48 | + * @throws OCSException The file was unable to be converted |
| 49 | + * @throws OCSNotFoundException The file to be converted was not found |
| 50 | + */ |
| 51 | + #[NoAdminRequired] |
| 52 | + #[UserRateLimit(limit: 25, period: 120)] |
| 53 | + #[ApiRoute(verb: 'POST', url: '/convert', root: '/conversion')] |
| 54 | + public function convert(int $fileId, string $targetMimeType, ?string $destination = null): DataResponse { |
| 55 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
| 56 | + $file = $userFolder->getFirstNodeById($fileId); |
| 57 | + |
| 58 | + if (!($file instanceof File)) { |
| 59 | + throw new OCSNotFoundException(); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + if ($destination !== null) { |
| 64 | + $destination = $userFolder->getFullpath($destination); |
| 65 | + } |
| 66 | + |
| 67 | + $convertedFile = $this->conversionManager->convert($file, $targetMimeType, $destination); |
| 68 | + } catch (\Exception $e) { |
| 69 | + throw new OCSException($e->getMessage()); |
| 70 | + } |
| 71 | + |
| 72 | + return new DataResponse([ |
| 73 | + 'path' => $convertedFile, |
| 74 | + ], Http::STATUS_CREATED); |
| 75 | + } |
| 76 | +} |
0 commit comments