diff options
Diffstat (limited to 'core/Controller/TextToImageApiController.php')
-rw-r--r-- | core/Controller/TextToImageApiController.php | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/core/Controller/TextToImageApiController.php b/core/Controller/TextToImageApiController.php index 9d97a538750..d2c3e1ec288 100644 --- a/core/Controller/TextToImageApiController.php +++ b/core/Controller/TextToImageApiController.php @@ -3,39 +3,25 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net> - * - * @author Marcel Klehr <mklehr@gmx.net> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Controller; +use OC\Core\ResponseDefinitions; use OC\Files\AppData\AppData; -use OCA\Core\ResponseDefinitions; use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\AnonRateLimit; +use OCP\AppFramework\Http\Attribute\ApiRoute; use OCP\AppFramework\Http\Attribute\BruteForceProtection; use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\Attribute\PublicPage; use OCP\AppFramework\Http\Attribute\UserRateLimit; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\AppFramework\OCSController; use OCP\DB\Exception; use OCP\Files\NotFoundException; use OCP\IL10N; @@ -49,7 +35,7 @@ use OCP\TextToImage\Task; /** * @psalm-import-type CoreTextToImageTask from ResponseDefinitions */ -class TextToImageApiController extends \OCP\AppFramework\OCSController { +class TextToImageApiController extends OCSController { public function __construct( string $appName, IRequest $request, @@ -69,6 +55,7 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { * 200: Returns availability status */ #[PublicPage] + #[ApiRoute(verb: 'GET', url: '/is_available', root: '/text2image')] public function isAvailable(): DataResponse { return new DataResponse([ 'isAvailable' => $this->textToImageManager->hasProviders(), @@ -91,6 +78,7 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { #[PublicPage] #[UserRateLimit(limit: 20, period: 120)] #[AnonRateLimit(limit: 5, period: 120)] + #[ApiRoute(verb: 'POST', url: '/schedule', root: '/text2image')] public function schedule(string $input, string $appId, string $identifier = '', int $numberOfImages = 8): DataResponse { $task = new Task($input, $appId, $numberOfImages, $this->userId, $identifier); try { @@ -125,6 +113,7 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { */ #[PublicPage] #[BruteForceProtection(action: 'text2image')] + #[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/text2image')] public function getTask(int $id): DataResponse { try { $task = $this->textToImageManager->getUserTask($id, $this->userId); @@ -156,17 +145,18 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { */ #[PublicPage] #[BruteForceProtection(action: 'text2image')] + #[ApiRoute(verb: 'GET', url: '/task/{id}/image/{index}', root: '/text2image')] public function getImage(int $id, int $index): DataResponse|FileDisplayResponse { try { $task = $this->textToImageManager->getUserTask($id, $this->userId); try { $folder = $this->appData->getFolder('text2image'); - } catch(NotFoundException) { + } catch (NotFoundException) { $res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND); $res->throttle(['action' => 'text2image']); return $res; } - $file = $folder->getFolder((string) $task->getId())->getFile((string) $index); + $file = $folder->getFolder((string)$task->getId())->getFile((string)$index); $info = getimagesizefromstring($file->getContent()); return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => image_type_to_mime_type($info[2])]); @@ -195,6 +185,7 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { */ #[NoAdminRequired] #[BruteForceProtection(action: 'text2image')] + #[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/text2image')] public function deleteTask(int $id): DataResponse { try { $task = $this->textToImageManager->getUserTask($id, $this->userId); @@ -222,19 +213,19 @@ class TextToImageApiController extends \OCP\AppFramework\OCSController { * * @param string $appId ID of the app * @param string|null $identifier An arbitrary identifier for the task - * @return DataResponse<Http::STATUS_OK, array{tasks: CoreTextToImageTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> + * @return DataResponse<Http::STATUS_OK, array{tasks: list<CoreTextToImageTask>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> * - * 200: Task list returned + * 200: Task list returned */ #[NoAdminRequired] #[AnonRateLimit(limit: 5, period: 120)] + #[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/text2image')] public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse { try { $tasks = $this->textToImageManager->getUserTasksByApp($this->userId, $appId, $identifier); - /** @var CoreTextToImageTask[] $json */ - $json = array_map(static function (Task $task) { + $json = array_values(array_map(static function (Task $task) { return $task->jsonSerialize(); - }, $tasks); + }, $tasks)); return new DataResponse([ 'tasks' => $json, |