123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- declare(strict_types=1);
- namespace OC\Core\Controller;
- 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\NoAdminRequired;
- use OCP\AppFramework\Http\Attribute\PublicPage;
- use OCP\AppFramework\Http\Attribute\UserRateLimit;
- use OCP\AppFramework\Http\DataDownloadResponse;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\Files\File;
- use OCP\Files\IRootFolder;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\TaskProcessing\EShapeType;
- use OCP\TaskProcessing\Exception\Exception;
- use OCP\TaskProcessing\Exception\UnauthorizedException;
- use OCP\TaskProcessing\Exception\ValidationException;
- use OCP\TaskProcessing\ShapeDescriptor;
- use OCP\TaskProcessing\Task;
- class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
- public function __construct(
- string $appName,
- IRequest $request,
- private \OCP\TaskProcessing\IManager $taskProcessingManager,
- private IL10N $l,
- private ?string $userId,
- private IRootFolder $rootFolder,
- ) {
- parent::__construct($appName, $request);
- }
-
-
-
- public function taskTypes(): DataResponse {
- $taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
- $serializedTaskTypes = [];
- foreach ($taskTypes as $key => $taskType) {
- $serializedTaskTypes[$key] = [
- 'name' => $taskType['name'],
- 'description' => $taskType['description'],
- 'inputShape' => array_map(fn (ShapeDescriptor $descriptor) =>
- $descriptor->jsonSerialize() + ['mandatory' => true], $taskType['inputShape'])
- + array_map(fn (ShapeDescriptor $descriptor) =>
- $descriptor->jsonSerialize() + ['mandatory' => false], $taskType['optionalInputShape']),
- 'outputShape' => array_map(fn (ShapeDescriptor $descriptor) =>
- $descriptor->jsonSerialize() + ['mandatory' => true], $taskType['outputShape'])
- + array_map(fn (ShapeDescriptor $descriptor) =>
- $descriptor->jsonSerialize() + ['mandatory' => false], $taskType['optionalOutputShape']),
- ];
- }
- return new DataResponse([
- 'types' => $serializedTaskTypes,
- ]);
- }
-
-
-
-
-
- public function schedule(array $input, string $type, string $appId, string $customId = ''): DataResponse {
- $task = new Task($type, $input, $appId, $this->userId, $customId);
- try {
- $this->taskProcessingManager->scheduleTask($task);
-
- $json = $task->jsonSerialize();
- return new DataResponse([
- 'task' => $json,
- ]);
- } catch (\OCP\TaskProcessing\Exception\PreConditionNotMetException) {
- return new DataResponse(['message' => $this->l->t('The given provider is not available')], Http::STATUS_PRECONDITION_FAILED);
- } catch (ValidationException $e) {
- return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
- } catch (UnauthorizedException $e) {
- return new DataResponse(['message' => 'User does not have access to the files mentioned in the task input'], Http::STATUS_UNAUTHORIZED);
- } catch (\OCP\TaskProcessing\Exception\Exception $e) {
- return new DataResponse(['message' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
-
- public function getTask(int $id): DataResponse {
- try {
- $task = $this->taskProcessingManager->getUserTask($id, $this->userId);
-
- $json = $task->jsonSerialize();
- return new DataResponse([
- 'task' => $json,
- ]);
- } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
- return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
- } catch (\RuntimeException $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
-
- public function deleteTask(int $id): DataResponse {
- try {
- $task = $this->taskProcessingManager->getUserTask($id, $this->userId);
- $this->taskProcessingManager->deleteTask($task);
- return new DataResponse(null);
- } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
- return new DataResponse(null);
- } catch (\OCP\TaskProcessing\Exception\Exception $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
-
- public function listTasksByApp(string $appId, ?string $customId = null): DataResponse {
- try {
- $tasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, $appId, $customId);
-
- $json = array_map(static function (Task $task) {
- return $task->jsonSerialize();
- }, $tasks);
- return new DataResponse([
- 'tasks' => $json,
- ]);
- } catch (Exception $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
-
- public function listTasks(?string $taskType, ?string $customId = null): DataResponse {
- try {
- $tasks = $this->taskProcessingManager->getUserTasks($this->userId, $taskType, $customId);
-
- $json = array_map(static function (Task $task) {
- return $task->jsonSerialize();
- }, $tasks);
- return new DataResponse([
- 'tasks' => $json,
- ]);
- } catch (Exception $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
-
-
- public function getFileContents(int $taskId, int $fileId): Http\DataDownloadResponse|DataResponse {
- try {
- $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
- $ids = $this->extractFileIdsFromTask($task);
- if (!in_array($fileId, $ids)) {
- return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
- }
- $node = $this->rootFolder->getFirstNodeById($fileId);
- if ($node === null) {
- $node = $this->rootFolder->getFirstNodeByIdInPath($fileId, '/' . $this->rootFolder->getAppDataDirectoryName() . '/');
- if (!$node instanceof File) {
- throw new \OCP\TaskProcessing\Exception\NotFoundException('Node is not a file');
- }
- } elseif (!$node instanceof File) {
- throw new \OCP\TaskProcessing\Exception\NotFoundException('Node is not a file');
- }
- return new Http\DataDownloadResponse($node->getContent(), $node->getName(), $node->getMimeType());
- } 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);
- }
- }
-
- private function extractFileIdsFromTask(Task $task): array {
- $ids = [];
- $taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
- if (!isset($taskTypes[$task->getTaskTypeId()])) {
- throw new \OCP\TaskProcessing\Exception\NotFoundException('Could not find task type');
- }
- $taskType = $taskTypes[$task->getTaskTypeId()];
- foreach ($taskType['inputShape'] + $taskType['optionalInputShape'] as $key => $descriptor) {
- if (in_array(EShapeType::getScalarType($descriptor->getShapeType()), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true)) {
-
- $inputSlot = $task->getInput()[$key];
- if (is_array($inputSlot)) {
- $ids += $inputSlot;
- } else {
- $ids[] = $inputSlot;
- }
- }
- }
- 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)) {
-
- $outputSlot = $task->getOutput()[$key];
- if (is_array($outputSlot)) {
- $ids += $outputSlot;
- } else {
- $ids[] = $outputSlot;
- }
- }
- }
- }
- return array_values($ids);
- }
-
-
-
- public function setProgress(int $taskId, float $progress): DataResponse {
- try {
- $this->taskProcessingManager->setTaskProgress($taskId, $progress);
- $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
-
- $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);
- }
- }
-
-
-
- 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);
-
- $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);
- }
- }
-
-
-
- public function cancelTask(int $taskId): DataResponse {
- try {
-
- $this->taskProcessingManager->getUserTask($taskId, $this->userId);
-
- $this->taskProcessingManager->cancelTask($taskId);
- $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
-
- $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);
- }
- }
- }
|