77
88namespace OCA \Assistant \Service ;
99
10+ use OC \User \NoUserException ;
11+ use OCA \Assistant \AppInfo \Application ;
1012use OCP \Files \File ;
1113use OCP \Files \GenericFileException ;
1214use OCP \Files \IRootFolder ;
1921use OCP \TaskProcessing \Exception \ValidationException ;
2022use OCP \TaskProcessing \IManager ;
2123use OCP \TaskProcessing \Task ;
24+ use OCP \TaskProcessing \TaskTypes \AudioToText ;
25+ use OCP \TaskProcessing \TaskTypes \TextToTextSummary ;
2226use RuntimeException ;
2327
2428class TaskProcessingService {
@@ -48,13 +52,10 @@ public function runTaskProcessingTask(Task $task): array {
4852
4953 /**
5054 * @param int $fileId
51- * @return string
55+ * @return File
5256 * @throws NotFoundException
53- * @throws GenericFileException
54- * @throws NotPermittedException
55- * @throws LockedException
5657 */
57- public function getOutputFileContent (int $ fileId ): string {
58+ public function getOutputFile (int $ fileId ): File {
5859 $ node = $ this ->rootFolder ->getFirstNodeById ($ fileId );
5960 if ($ node === null ) {
6061 $ node = $ this ->rootFolder ->getFirstNodeByIdInPath ($ fileId , '/ ' . $ this ->rootFolder ->getAppDataDirectoryName () . '/ ' );
@@ -64,6 +65,63 @@ public function getOutputFileContent(int $fileId): string {
6465 } elseif (!$ node instanceof File) {
6566 throw new NotFoundException ('Node is not a file ' );
6667 }
67- return $ node ->getContent ();
68+ return $ node ;
69+ }
70+
71+ public function getOutputFileContent (int $ fileId ): string {
72+ $ file = $ this ->getOutputFile ($ fileId );
73+ return $ file ->getContent ();
74+ }
75+
76+ public function isFileActionTaskTypeAuthorized (string $ taskTypeId ): bool {
77+ $ authorizedTaskTypes = [AudioToText::ID , TextToTextSummary::ID ];
78+ if (class_exists ('OCP \\TaskProcessing \\TaskTypes \\TextToSpeech ' )) {
79+ $ authorizedTaskTypes [] = \OCP \TaskProcessing \TaskTypes \TextToSpeech::ID ;
80+ }
81+ return in_array ($ taskTypeId , $ authorizedTaskTypes , true );
82+ }
83+
84+ /**
85+ * Execute a file action
86+ *
87+ * @param string $userId
88+ * @param int $fileId
89+ * @param string $taskTypeId
90+ * @return int The scheduled task ID
91+ * @throws Exception
92+ * @throws GenericFileException
93+ * @throws LockedException
94+ * @throws NotFoundException
95+ * @throws NotPermittedException
96+ * @throws PreConditionNotMetException
97+ * @throws UnauthorizedException
98+ * @throws ValidationException
99+ * @throws NoUserException
100+ */
101+ public function runFileAction (string $ userId , int $ fileId , string $ taskTypeId ): int {
102+ if (!$ this ->isFileActionTaskTypeAuthorized ($ taskTypeId )) {
103+ throw new PreConditionNotMetException ();
104+ }
105+ $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
106+ $ file = $ userFolder ->getFirstNodeById ($ fileId );
107+ if (!$ file instanceof File) {
108+ throw new NotFoundException ('File is not a file ' );
109+ }
110+ $ input = $ taskTypeId === AudioToText::ID
111+ ? ['input ' => $ fileId ]
112+ : ['input ' => $ file ->getContent ()];
113+ $ task = new Task (
114+ $ taskTypeId ,
115+ $ input ,
116+ Application::APP_ID . ':file-action ' ,
117+ $ userId ,
118+ 'file-action: ' . $ fileId ,
119+ );
120+ $ this ->taskProcessingManager ->scheduleTask ($ task );
121+ $ taskId = $ task ->getId ();
122+ if ($ taskId === null ) {
123+ throw new Exception ('The task could not be scheduled ' );
124+ }
125+ return $ taskId ;
68126 }
69127}
0 commit comments