TaskProcessingApiController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OCA\Core\ResponseDefinitions;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  13. use OCP\AppFramework\Http\Attribute\PublicPage;
  14. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  15. use OCP\AppFramework\Http\DataDownloadResponse;
  16. use OCP\AppFramework\Http\DataResponse;
  17. use OCP\Files\File;
  18. use OCP\Files\IRootFolder;
  19. use OCP\IL10N;
  20. use OCP\IRequest;
  21. use OCP\TaskProcessing\EShapeType;
  22. use OCP\TaskProcessing\Exception\Exception;
  23. use OCP\TaskProcessing\Exception\UnauthorizedException;
  24. use OCP\TaskProcessing\Exception\ValidationException;
  25. use OCP\TaskProcessing\ShapeDescriptor;
  26. use OCP\TaskProcessing\Task;
  27. /**
  28. * @psalm-import-type CoreTaskProcessingTask from ResponseDefinitions
  29. * @psalm-import-type CoreTaskProcessingTaskType from ResponseDefinitions
  30. */
  31. class TaskProcessingApiController extends \OCP\AppFramework\OCSController {
  32. public function __construct(
  33. string $appName,
  34. IRequest $request,
  35. private \OCP\TaskProcessing\IManager $taskProcessingManager,
  36. private IL10N $l,
  37. private ?string $userId,
  38. private IRootFolder $rootFolder,
  39. ) {
  40. parent::__construct($appName, $request);
  41. }
  42. /**
  43. * Returns all available TaskProcessing task types
  44. *
  45. * @return DataResponse<Http::STATUS_OK, array{types: array<string, CoreTaskProcessingTaskType>}, array{}>
  46. *
  47. * 200: Task types returned
  48. */
  49. #[PublicPage]
  50. #[ApiRoute(verb: 'GET', url: '/tasktypes', root: '/taskprocessing')]
  51. public function taskTypes(): DataResponse {
  52. $taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
  53. $serializedTaskTypes = [];
  54. foreach ($taskTypes as $key => $taskType) {
  55. $serializedTaskTypes[$key] = [
  56. 'name' => $taskType['name'],
  57. 'description' => $taskType['description'],
  58. 'inputShape' => array_map(fn (ShapeDescriptor $descriptor) =>
  59. $descriptor->jsonSerialize() + ['mandatory' => true], $taskType['inputShape'])
  60. + array_map(fn (ShapeDescriptor $descriptor) =>
  61. $descriptor->jsonSerialize() + ['mandatory' => false], $taskType['optionalInputShape']),
  62. 'outputShape' => array_map(fn (ShapeDescriptor $descriptor) =>
  63. $descriptor->jsonSerialize() + ['mandatory' => true], $taskType['outputShape'])
  64. + array_map(fn (ShapeDescriptor $descriptor) =>
  65. $descriptor->jsonSerialize() + ['mandatory' => false], $taskType['optionalOutputShape']),
  66. ];
  67. }
  68. return new DataResponse([
  69. 'types' => $serializedTaskTypes,
  70. ]);
  71. }
  72. /**
  73. * Schedules a task
  74. *
  75. * @param array<string, mixed> $input Task's input parameters
  76. * @param string $type Type of the task
  77. * @param string $appId ID of the app that will execute the task
  78. * @param string $customId An arbitrary identifier for the task
  79. *
  80. * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_BAD_REQUEST|Http::STATUS_PRECONDITION_FAILED|Http::STATUS_UNAUTHORIZED, array{message: string}, array{}>
  81. *
  82. * 200: Task scheduled successfully
  83. * 400: Scheduling task is not possible
  84. * 412: Scheduling task is not possible
  85. * 401: Cannot schedule task because it references files in its input that the user doesn't have access to
  86. */
  87. #[PublicPage]
  88. #[UserRateLimit(limit: 20, period: 120)]
  89. #[AnonRateLimit(limit: 5, period: 120)]
  90. #[ApiRoute(verb: 'POST', url: '/schedule', root: '/taskprocessing')]
  91. public function schedule(array $input, string $type, string $appId, string $customId = ''): DataResponse {
  92. $task = new Task($type, $input, $appId, $this->userId, $customId);
  93. try {
  94. $this->taskProcessingManager->scheduleTask($task);
  95. /** @var CoreTaskProcessingTask $json */
  96. $json = $task->jsonSerialize();
  97. return new DataResponse([
  98. 'task' => $json,
  99. ]);
  100. } catch (\OCP\TaskProcessing\Exception\PreConditionNotMetException) {
  101. return new DataResponse(['message' => $this->l->t('The given provider is not available')], Http::STATUS_PRECONDITION_FAILED);
  102. } catch (ValidationException $e) {
  103. return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
  104. } catch (UnauthorizedException $e) {
  105. return new DataResponse(['message' => 'User does not have access to the files mentioned in the task input'], Http::STATUS_UNAUTHORIZED);
  106. } catch (\OCP\TaskProcessing\Exception\Exception $e) {
  107. return new DataResponse(['message' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
  108. }
  109. }
  110. /**
  111. * Gets a task including status and result
  112. *
  113. * Tasks are removed 1 week after receiving their last update
  114. *
  115. * @param int $id The id of the task
  116. *
  117. * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  118. *
  119. * 200: Task returned
  120. * 404: Task not found
  121. */
  122. #[PublicPage]
  123. #[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/taskprocessing')]
  124. public function getTask(int $id): DataResponse {
  125. try {
  126. $task = $this->taskProcessingManager->getUserTask($id, $this->userId);
  127. /** @var CoreTaskProcessingTask $json */
  128. $json = $task->jsonSerialize();
  129. return new DataResponse([
  130. 'task' => $json,
  131. ]);
  132. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  133. return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
  134. } catch (\RuntimeException $e) {
  135. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  136. }
  137. }
  138. /**
  139. * Deletes a task
  140. *
  141. * @param int $id The id of the task
  142. *
  143. * @return DataResponse<Http::STATUS_OK, null, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  144. *
  145. * 200: Task deleted
  146. */
  147. #[NoAdminRequired]
  148. #[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/taskprocessing')]
  149. public function deleteTask(int $id): DataResponse {
  150. try {
  151. $task = $this->taskProcessingManager->getUserTask($id, $this->userId);
  152. $this->taskProcessingManager->deleteTask($task);
  153. return new DataResponse(null);
  154. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  155. return new DataResponse(null);
  156. } catch (\OCP\TaskProcessing\Exception\Exception $e) {
  157. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  158. }
  159. }
  160. /**
  161. * Returns tasks for the current user filtered by the appId and optional customId
  162. *
  163. * @param string $appId ID of the app
  164. * @param string|null $customId An arbitrary identifier for the task
  165. * @return DataResponse<Http::STATUS_OK, array{tasks: CoreTaskProcessingTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  166. *
  167. * 200: Tasks returned
  168. */
  169. #[NoAdminRequired]
  170. #[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/taskprocessing')]
  171. public function listTasksByApp(string $appId, ?string $customId = null): DataResponse {
  172. try {
  173. $tasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, $appId, $customId);
  174. /** @var CoreTaskProcessingTask[] $json */
  175. $json = array_map(static function (Task $task) {
  176. return $task->jsonSerialize();
  177. }, $tasks);
  178. return new DataResponse([
  179. 'tasks' => $json,
  180. ]);
  181. } catch (Exception $e) {
  182. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  183. }
  184. }
  185. /**
  186. * Returns tasks for the current user filtered by the optional taskType and optional customId
  187. *
  188. * @param string|null $taskType The task type to filter by
  189. * @param string|null $customId An arbitrary identifier for the task
  190. * @return DataResponse<Http::STATUS_OK, array{tasks: CoreTaskProcessingTask[]}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  191. *
  192. * 200: Tasks returned
  193. */
  194. #[NoAdminRequired]
  195. #[ApiRoute(verb: 'GET', url: '/tasks', root: '/taskprocessing')]
  196. public function listTasks(?string $taskType, ?string $customId = null): DataResponse {
  197. try {
  198. $tasks = $this->taskProcessingManager->getUserTasks($this->userId, $taskType, $customId);
  199. /** @var CoreTaskProcessingTask[] $json */
  200. $json = array_map(static function (Task $task) {
  201. return $task->jsonSerialize();
  202. }, $tasks);
  203. return new DataResponse([
  204. 'tasks' => $json,
  205. ]);
  206. } catch (Exception $e) {
  207. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  208. }
  209. }
  210. /**
  211. * Returns the contents of a file referenced in a task
  212. *
  213. * @param int $taskId The id of the task
  214. * @param int $fileId The file id of the file to retrieve
  215. * @return DataDownloadResponse<Http::STATUS_OK, string, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
  216. *
  217. * 200: File content returned
  218. * 404: Task or file not found
  219. */
  220. #[NoAdminRequired]
  221. #[Http\Attribute\NoCSRFRequired]
  222. #[ApiRoute(verb: 'GET', url: '/tasks/{taskId}/file/{fileId}', root: '/taskprocessing')]
  223. public function getFileContents(int $taskId, int $fileId): Http\DataDownloadResponse|DataResponse {
  224. try {
  225. $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  226. $ids = $this->extractFileIdsFromTask($task);
  227. if (!in_array($fileId, $ids)) {
  228. return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
  229. }
  230. $node = $this->rootFolder->getFirstNodeById($fileId);
  231. if ($node === null) {
  232. $node = $this->rootFolder->getFirstNodeByIdInPath($fileId, '/' . $this->rootFolder->getAppDataDirectoryName() . '/');
  233. if (!$node instanceof File) {
  234. throw new \OCP\TaskProcessing\Exception\NotFoundException('Node is not a file');
  235. }
  236. } elseif (!$node instanceof File) {
  237. throw new \OCP\TaskProcessing\Exception\NotFoundException('Node is not a file');
  238. }
  239. return new Http\DataDownloadResponse($node->getContent(), $node->getName(), $node->getMimeType());
  240. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  241. return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
  242. } catch (Exception $e) {
  243. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  244. }
  245. }
  246. /**
  247. * @param Task $task
  248. * @return list<int>
  249. * @throws \OCP\TaskProcessing\Exception\NotFoundException
  250. */
  251. private function extractFileIdsFromTask(Task $task): array {
  252. $ids = [];
  253. $taskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
  254. if (!isset($taskTypes[$task->getTaskTypeId()])) {
  255. throw new \OCP\TaskProcessing\Exception\NotFoundException('Could not find task type');
  256. }
  257. $taskType = $taskTypes[$task->getTaskTypeId()];
  258. foreach ($taskType['inputShape'] + $taskType['optionalInputShape'] as $key => $descriptor) {
  259. if (in_array(EShapeType::getScalarType($descriptor->getShapeType()), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true)) {
  260. /** @var int|list<int> $inputSlot */
  261. $inputSlot = $task->getInput()[$key];
  262. if (is_array($inputSlot)) {
  263. $ids += $inputSlot;
  264. } else {
  265. $ids[] = $inputSlot;
  266. }
  267. }
  268. }
  269. if ($task->getOutput() !== null) {
  270. foreach ($taskType['outputShape'] + $taskType['optionalOutputShape'] as $key => $descriptor) {
  271. if (in_array(EShapeType::getScalarType($descriptor->getShapeType()), [EShapeType::File, EShapeType::Image, EShapeType::Audio, EShapeType::Video], true)) {
  272. /** @var int|list<int> $outputSlot */
  273. $outputSlot = $task->getOutput()[$key];
  274. if (is_array($outputSlot)) {
  275. $ids += $outputSlot;
  276. } else {
  277. $ids[] = $outputSlot;
  278. }
  279. }
  280. }
  281. }
  282. return array_values($ids);
  283. }
  284. /**
  285. * Sets the task progress
  286. *
  287. * @param int $taskId The id of the task
  288. * @param float $progress The progress
  289. * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
  290. *
  291. * 200: Progress updated successfully
  292. * 404: Task not found
  293. */
  294. #[NoAdminRequired]
  295. #[ApiRoute(verb: 'POST', url: '/tasks/{taskId}/progress', root: '/taskprocessing')]
  296. public function setProgress(int $taskId, float $progress): DataResponse {
  297. try {
  298. $this->taskProcessingManager->setTaskProgress($taskId, $progress);
  299. $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  300. /** @var CoreTaskProcessingTask $json */
  301. $json = $task->jsonSerialize();
  302. return new DataResponse([
  303. 'task' => $json,
  304. ]);
  305. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  306. return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
  307. } catch (Exception $e) {
  308. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  309. }
  310. }
  311. /**
  312. * Sets the task result
  313. *
  314. * @param int $taskId The id of the task
  315. * @param array<string,mixed>|null $output The resulting task output
  316. * @param string|null $errorMessage An error message if the task failed
  317. * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
  318. *
  319. * 200: Result updated successfully
  320. * 404: Task not found
  321. */
  322. #[NoAdminRequired]
  323. #[ApiRoute(verb: 'POST', url: '/tasks/{taskId}/result', root: '/taskprocessing')]
  324. public function setResult(int $taskId, ?array $output = null, ?string $errorMessage = null): DataResponse {
  325. try {
  326. // Check if the current user can access the task
  327. $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  328. // set result
  329. $this->taskProcessingManager->setTaskResult($taskId, $errorMessage, $output);
  330. $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  331. /** @var CoreTaskProcessingTask $json */
  332. $json = $task->jsonSerialize();
  333. return new DataResponse([
  334. 'task' => $json,
  335. ]);
  336. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  337. return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
  338. } catch (Exception $e) {
  339. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  340. }
  341. }
  342. /**
  343. * Cancels a task
  344. *
  345. * @param int $taskId The id of the task
  346. * @return DataResponse<Http::STATUS_OK, array{task: CoreTaskProcessingTask}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
  347. *
  348. * 200: Task canceled successfully
  349. * 404: Task not found
  350. */
  351. #[NoAdminRequired]
  352. #[ApiRoute(verb: 'POST', url: '/tasks/{taskId}/cancel', root: '/taskprocessing')]
  353. public function cancelTask(int $taskId): DataResponse {
  354. try {
  355. // Check if the current user can access the task
  356. $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  357. // set result
  358. $this->taskProcessingManager->cancelTask($taskId);
  359. $task = $this->taskProcessingManager->getUserTask($taskId, $this->userId);
  360. /** @var CoreTaskProcessingTask $json */
  361. $json = $task->jsonSerialize();
  362. return new DataResponse([
  363. 'task' => $json,
  364. ]);
  365. } catch (\OCP\TaskProcessing\Exception\NotFoundException $e) {
  366. return new DataResponse(['message' => $this->l->t('Not found')], Http::STATUS_NOT_FOUND);
  367. } catch (Exception $e) {
  368. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  369. }
  370. }
  371. }