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 {
@@ -48,13 +52,10 @@ public function runTaskProcessingTask(Task $task): array {
48
52
49
53
/**
50
54
* @param int $fileId
51
- * @return string
55
+ * @return File
52
56
* @throws NotFoundException
53
- * @throws GenericFileException
54
- * @throws NotPermittedException
55
- * @throws LockedException
56
57
*/
57
- public function getOutputFileContent (int $ fileId ): string {
58
+ public function getOutputFile (int $ fileId ): File {
58
59
$ node = $ this ->rootFolder ->getFirstNodeById ($ fileId );
59
60
if ($ node === null ) {
60
61
$ node = $ this ->rootFolder ->getFirstNodeByIdInPath ($ fileId , '/ ' . $ this ->rootFolder ->getAppDataDirectoryName () . '/ ' );
@@ -64,6 +65,63 @@ public function getOutputFileContent(int $fileId): string {
64
65
} elseif (!$ node instanceof File) {
65
66
throw new NotFoundException ('Node is not a file ' );
66
67
}
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 ;
68
126
}
69
127
}
0 commit comments