|
7 | 7 |
|
8 | 8 | namespace OCA\Assistant\Service;
|
9 | 9 |
|
| 10 | +use OC\User\NoUserException; |
| 11 | +use OCA\Assistant\AppInfo\Application; |
10 | 12 | use OCP\Files\File;
|
11 | 13 | use OCP\Files\GenericFileException;
|
12 | 14 | use OCP\Files\IRootFolder;
|
|
19 | 21 | use OCP\TaskProcessing\Exception\ValidationException;
|
20 | 22 | use OCP\TaskProcessing\IManager;
|
21 | 23 | use OCP\TaskProcessing\Task;
|
| 24 | +use OCP\TaskProcessing\TaskTypes\AudioToText; |
| 25 | +use OCP\TaskProcessing\TaskTypes\TextToTextSummary; |
22 | 26 | use RuntimeException;
|
23 | 27 |
|
24 | 28 | class TaskProcessingService {
|
@@ -66,4 +70,56 @@ public function getOutputFileContent(int $fileId): string {
|
66 | 70 | }
|
67 | 71 | return $node->getContent();
|
68 | 72 | }
|
| 73 | + |
| 74 | + public function isFileActionTaskTypeAuthorized(string $taskTypeId): bool { |
| 75 | + $authorizedTaskTypes = [AudioToText::ID, TextToTextSummary::ID]; |
| 76 | + if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech')) { |
| 77 | + $authorizedTaskTypes[] = \OCP\TaskProcessing\TaskTypes\TextToSpeech::ID; |
| 78 | + } |
| 79 | + return in_array($taskTypeId, $authorizedTaskTypes, true); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Execute a file action |
| 84 | + * |
| 85 | + * @param string $userId |
| 86 | + * @param int $fileId |
| 87 | + * @param string $taskTypeId |
| 88 | + * @return int The scheduled task ID |
| 89 | + * @throws Exception |
| 90 | + * @throws GenericFileException |
| 91 | + * @throws LockedException |
| 92 | + * @throws NotFoundException |
| 93 | + * @throws NotPermittedException |
| 94 | + * @throws PreConditionNotMetException |
| 95 | + * @throws UnauthorizedException |
| 96 | + * @throws ValidationException |
| 97 | + * @throws NoUserException |
| 98 | + */ |
| 99 | + public function runFileAction(string $userId, int $fileId, string $taskTypeId): int { |
| 100 | + if (!$this->isFileActionTaskTypeAuthorized($taskTypeId)) { |
| 101 | + throw new PreConditionNotMetException(); |
| 102 | + } |
| 103 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
| 104 | + $file = $userFolder->getFirstNodeById($fileId); |
| 105 | + if (!$file instanceof File) { |
| 106 | + throw new NotFoundException('File is not a file'); |
| 107 | + } |
| 108 | + $input = $taskTypeId === AudioToText::ID |
| 109 | + ? ['input' => $fileId] |
| 110 | + : ['input' => $file->getContent()]; |
| 111 | + $task = new Task( |
| 112 | + $taskTypeId, |
| 113 | + $input, |
| 114 | + Application::APP_ID . ':file-action:', |
| 115 | + $userId, |
| 116 | + 'file-action:' . $fileId, |
| 117 | + ); |
| 118 | + $this->taskProcessingManager->scheduleTask($task); |
| 119 | + $taskId = $task->getId(); |
| 120 | + if ($taskId === null) { |
| 121 | + throw new Exception('The task could not be scheduled'); |
| 122 | + } |
| 123 | + return $taskId; |
| 124 | + } |
69 | 125 | }
|
0 commit comments