|
| 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 | +namespace OCA\Files\Controller; |
| 11 | + |
| 12 | +use OC\Files\Utils\PathHelper; |
| 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\OCSForbiddenException; |
| 20 | +use OCP\AppFramework\OCS\OCSNotFoundException; |
| 21 | +use OCP\AppFramework\OCSController; |
| 22 | +use OCP\Files\Conversion\IConversionManager; |
| 23 | +use OCP\Files\File; |
| 24 | +use OCP\Files\IRootFolder; |
| 25 | +use OCP\IL10N; |
| 26 | +use OCP\IRequest; |
| 27 | + |
| 28 | +class ConversionApiController extends OCSController { |
| 29 | + public function __construct( |
| 30 | + string $appName, |
| 31 | + IRequest $request, |
| 32 | + private IConversionManager $fileConversionManager, |
| 33 | + private IRootFolder $rootFolder, |
| 34 | + private IL10N $l10n, |
| 35 | + private ?string $userId, |
| 36 | + ) { |
| 37 | + parent::__construct($appName, $request); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Converts a file from one MIME type to another |
| 42 | + * |
| 43 | + * @param int $fileId ID of the file to be converted |
| 44 | + * @param string $targetMimeType The MIME type to which you want to convert the file |
| 45 | + * @param string|null $destination The target path of the converted file. Written to a temporary file if left empty |
| 46 | + * |
| 47 | + * @return DataResponse<Http::STATUS_CREATED, array{path: string}, array{}> |
| 48 | + * |
| 49 | + * 201: File was converted and written to the destination or temporary file |
| 50 | + * |
| 51 | + * @throws OCSException The file was unable to be converted |
| 52 | + * @throws OCSNotFoundException The file to be converted was not found |
| 53 | + */ |
| 54 | + #[NoAdminRequired] |
| 55 | + #[UserRateLimit(limit: 25, period: 120)] |
| 56 | + #[ApiRoute(verb: 'POST', url: '/api/v1/convert')] |
| 57 | + public function convert(int $fileId, string $targetMimeType, ?string $destination = null): DataResponse { |
| 58 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
| 59 | + $file = $userFolder->getFirstNodeById($fileId); |
| 60 | + |
| 61 | + if (!($file instanceof File)) { |
| 62 | + throw new OCSNotFoundException($this->l10n->t('The file cannot be found')); |
| 63 | + } |
| 64 | + |
| 65 | + if ($destination !== null) { |
| 66 | + $destination = PathHelper::normalizePath($destination); |
| 67 | + $parentDir = dirname($destination); |
| 68 | + |
| 69 | + if (!$userFolder->nodeExists($parentDir)) { |
| 70 | + throw new OCSNotFoundException($this->l10n->t('The destination path does not exist: %1$s', [$parentDir])); |
| 71 | + } |
| 72 | + |
| 73 | + if (!$userFolder->get($parentDir)->isCreatable()) { |
| 74 | + throw new OCSForbiddenException(); |
| 75 | + } |
| 76 | + |
| 77 | + $destination = $userFolder->getFullPath($destination); |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + $convertedFile = $this->fileConversionManager->convert($file, $targetMimeType, $destination); |
| 82 | + } catch (\Exception $e) { |
| 83 | + throw new OCSException($e->getMessage()); |
| 84 | + } |
| 85 | + |
| 86 | + $convertedFileRelativePath = $userFolder->getRelativePath($convertedFile); |
| 87 | + if ($convertedFileRelativePath === null) { |
| 88 | + throw new OCSNotFoundException($this->l10n->t('Could not get relative path to converted file')); |
| 89 | + } |
| 90 | + |
| 91 | + return new DataResponse([ |
| 92 | + 'path' => $convertedFileRelativePath, |
| 93 | + ], Http::STATUS_CREATED); |
| 94 | + } |
| 95 | +} |
0 commit comments