diff options
author | Marcel Klehr <mklehr@gmx.net> | 2024-05-03 10:14:19 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2024-05-14 11:38:39 +0200 |
commit | 8e5662602a1dd264f54f249ce1cfc2f30be627fb (patch) | |
tree | e23252130bcc052ff062a05fab3e5b81913b278a /core/Controller/TaskProcessingApiController.php | |
parent | 7a947980db9e8824a3c5c0f32a85af3f07a9ada9 (diff) | |
download | nextcloud-server-8e5662602a1dd264f54f249ce1cfc2f30be627fb.tar.gz nextcloud-server-8e5662602a1dd264f54f249ce1cfc2f30be627fb.zip |
feat: Add ExApp endpoints
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'core/Controller/TaskProcessingApiController.php')
-rw-r--r-- | core/Controller/TaskProcessingApiController.php | 76 |
1 files changed, 71 insertions, 5 deletions
diff --git a/core/Controller/TaskProcessingApiController.php b/core/Controller/TaskProcessingApiController.php index 99d5755e10b..cd8b9673871 100644 --- a/core/Controller/TaskProcessingApiController.php +++ b/core/Controller/TaskProcessingApiController.php @@ -98,7 +98,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController { /** * This endpoint allows scheduling a task * - * @param array<array-key, mixed> $input Input text + * @param array<string, mixed> $input Input text * @param string $type Type of the task * @param string $appId ID of the app that will execute the task * @param string $identifier An arbitrary identifier for the task @@ -118,6 +118,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController { try { $this->taskProcessingManager->scheduleTask($task); + /** @var CoreTaskProcessingTask $json */ $json = $task->jsonSerialize(); return new DataResponse([ @@ -149,6 +150,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController { try { $task = $this->taskProcessingManager->getUserTask($id, $this->userId); + /** @var CoreTaskProcessingTask $json */ $json = $task->jsonSerialize(); return new DataResponse([ @@ -255,7 +257,7 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController { /** * @param Task $task - * @return array + * @return list<mixed> * @throws \OCP\TaskProcessing\Exception\NotFoundException */ private function extractFileIdsFromTask(Task $task) { @@ -270,11 +272,75 @@ class TaskProcessingApiController extends \OCP\AppFramework\OCSController { $ids[] = $task->getInput()[$key]; } } - foreach ($taskType['outputShape'] + $taskType['optionalOutputShape'] as $key => $descriptor) { - if (in_array(EShapeType::getScalarType($descriptor->getShapeType()), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true)) { - $ids[] = $task->getOutput()[$key]; + if ($task->getOutput() !== null) { + foreach ($taskType['outputShape'] + $taskType['optionalOutputShape'] as $key => $descriptor) { + if (in_array(EShapeType::getScalarType($descriptor->getShapeType()), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true)) { + $ids[] = $task->getOutput()[$key]; + } } } return $ids; } + + /** + * This endpoint sets the task progress + * + * @param int $taskId The id of the task + * @param float $progress The progress + * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}> + * + * 200: File content returned + * 404: Task not found + */ + #[NoAdminRequired] + #[ApiRoute(verb: 'POST', url: '/tasks/{taskId}/progress', root: '/taskprocessing')] + public function setProgress(int $taskId, float $progress): DataResponse { + try { + $this->taskProcessingManager->setTaskProgress($taskId, $progress); + $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId); + + /** @var CoreTaskProcessingTask $json */ + $json = $task->jsonSerialize(); + + return new DataResponse([ + 'task' => $json, + ]); + } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) { + return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND); + } catch (Exception $e) { + return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** + * This endpoint sets the task progress + * + * @param int $taskId The id of the task + * @param array<string,mixed>|null $output The resulting task output + * @param string|null $errorMessage An error message if the task failed + * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}> + * + * 200: File content returned + * 404: Task not found + */ + #[NoAdminRequired] + #[ApiRoute(verb: 'POST', url: '/tasks/{taskId}/result', root: '/taskprocessing')] + public function setResult(int $taskId, ?array $output = null, ?string $errorMessage = null): DataResponse { + try { + $this->taskProcessingManager->getUserTask($taskId, $this->userId); + $this->taskProcessingManager->setTaskResult($taskId, $errorMessage, $output); + $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId); + + /** @var CoreTaskProcessingTask $json */ + $json = $task->jsonSerialize(); + + return new DataResponse([ + 'task' => $json, + ]); + } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) { + return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND); + } catch (Exception $e) { + return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } } |