23
23
use OCP \TaskProcessing \Task ;
24
24
use OCP \TaskProcessing \TaskTypes \AudioToText ;
25
25
use OCP \TaskProcessing \TaskTypes \TextToTextSummary ;
26
+ use Psr \Log \LoggerInterface ;
26
27
use RuntimeException ;
27
28
28
29
class TaskProcessingService {
29
30
30
31
public function __construct (
31
32
private IManager $ taskProcessingManager ,
32
33
private IRootFolder $ rootFolder ,
34
+ private LoggerInterface $ logger ,
33
35
) {
34
36
}
35
37
@@ -68,12 +70,20 @@ public function getOutputFile(int $fileId): File {
68
70
return $ node ;
69
71
}
70
72
73
+ /**
74
+ * @param int $fileId
75
+ * @return string
76
+ * @throws GenericFileException
77
+ * @throws LockedException
78
+ * @throws NotFoundException
79
+ * @throws NotPermittedException
80
+ */
71
81
public function getOutputFileContent (int $ fileId ): string {
72
82
$ file = $ this ->getOutputFile ($ fileId );
73
83
return $ file ->getContent ();
74
84
}
75
85
76
- public function isFileActionTaskTypeAuthorized (string $ taskTypeId ): bool {
86
+ public function isFileActionTaskTypeSupported (string $ taskTypeId ): bool {
77
87
$ authorizedTaskTypes = [AudioToText::ID , TextToTextSummary::ID ];
78
88
if (class_exists ('OCP \\TaskProcessing \\TaskTypes \\TextToSpeech ' )) {
79
89
$ authorizedTaskTypes [] = \OCP \TaskProcessing \TaskTypes \TextToSpeech::ID ;
@@ -89,35 +99,44 @@ public function isFileActionTaskTypeAuthorized(string $taskTypeId): bool {
89
99
* @param string $taskTypeId
90
100
* @return int The scheduled task ID
91
101
* @throws Exception
92
- * @throws GenericFileException
93
- * @throws LockedException
94
102
* @throws NotFoundException
95
- * @throws NotPermittedException
96
- * @throws PreConditionNotMetException
97
- * @throws UnauthorizedException
98
- * @throws ValidationException
99
- * @throws NoUserException
100
103
*/
101
104
public function runFileAction (string $ userId , int $ fileId , string $ taskTypeId ): int {
102
- if (!$ this ->isFileActionTaskTypeAuthorized ($ taskTypeId )) {
103
- throw new PreConditionNotMetException ();
105
+ if (!$ this ->isFileActionTaskTypeSupported ($ taskTypeId )) {
106
+ throw new Exception ('Invalid task type for file action ' );
107
+ }
108
+ try {
109
+ $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
110
+ } catch (NoUserException |NotPermittedException $ e ) {
111
+ $ this ->logger ->warning ('Assistant runFileAction, the user folder could not be obtained ' , ['exception ' => $ e ]);
112
+ throw new Exception ('The user folder could not be obtained ' );
104
113
}
105
- $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
106
114
$ file = $ userFolder ->getFirstNodeById ($ fileId );
107
115
if (!$ file instanceof File) {
116
+ $ this ->logger ->warning ('Assistant runFileAction, the input file is not a file ' , ['file_id ' => $ fileId ]);
108
117
throw new NotFoundException ('File is not a file ' );
109
118
}
110
- $ input = $ taskTypeId === AudioToText::ID
111
- ? ['input ' => $ fileId ]
112
- : ['input ' => $ file ->getContent ()];
119
+ try {
120
+ $ input = $ taskTypeId === AudioToText::ID
121
+ ? ['input ' => $ fileId ]
122
+ : ['input ' => $ file ->getContent ()];
123
+ } catch (NotPermittedException |GenericFileException |LockedException $ e ) {
124
+ $ this ->logger ->warning ('Assistant runFileAction, impossible to read the file action input file ' , ['exception ' => $ e ]);
125
+ throw new Exception ('Impossible to read the file action input file ' );
126
+ }
113
127
$ task = new Task (
114
128
$ taskTypeId ,
115
129
$ input ,
116
130
Application::APP_ID ,
117
131
$ userId ,
118
132
'file-action: ' . $ fileId ,
119
133
);
120
- $ this ->taskProcessingManager ->scheduleTask ($ task );
134
+ try {
135
+ $ this ->taskProcessingManager ->scheduleTask ($ task );
136
+ } catch (PreConditionNotMetException |ValidationException |Exception |UnauthorizedException $ e ) {
137
+ $ this ->logger ->warning ('Assistant runFileAction, impossible to schedule the task ' , ['exception ' => $ e ]);
138
+ throw new Exception ('Impossible to schedule the task ' );
139
+ }
121
140
$ taskId = $ task ->getId ();
122
141
if ($ taskId === null ) {
123
142
throw new Exception ('The task could not be scheduled ' );
0 commit comments