2323use OCP \TaskProcessing \Task ;
2424use OCP \TaskProcessing \TaskTypes \AudioToText ;
2525use OCP \TaskProcessing \TaskTypes \TextToTextSummary ;
26+ use Psr \Log \LoggerInterface ;
2627use RuntimeException ;
2728
2829class TaskProcessingService {
2930
3031 public function __construct (
3132 private IManager $ taskProcessingManager ,
3233 private IRootFolder $ rootFolder ,
34+ private LoggerInterface $ logger ,
3335 ) {
3436 }
3537
@@ -68,12 +70,20 @@ public function getOutputFile(int $fileId): File {
6870 return $ node ;
6971 }
7072
73+ /**
74+ * @param int $fileId
75+ * @return string
76+ * @throws GenericFileException
77+ * @throws LockedException
78+ * @throws NotFoundException
79+ * @throws NotPermittedException
80+ */
7181 public function getOutputFileContent (int $ fileId ): string {
7282 $ file = $ this ->getOutputFile ($ fileId );
7383 return $ file ->getContent ();
7484 }
7585
76- public function isFileActionTaskTypeAuthorized (string $ taskTypeId ): bool {
86+ public function isFileActionTaskTypeSupported (string $ taskTypeId ): bool {
7787 $ authorizedTaskTypes = [AudioToText::ID , TextToTextSummary::ID ];
7888 if (class_exists ('OCP \\TaskProcessing \\TaskTypes \\TextToSpeech ' )) {
7989 $ authorizedTaskTypes [] = \OCP \TaskProcessing \TaskTypes \TextToSpeech::ID ;
@@ -89,35 +99,44 @@ public function isFileActionTaskTypeAuthorized(string $taskTypeId): bool {
8999 * @param string $taskTypeId
90100 * @return int The scheduled task ID
91101 * @throws Exception
92- * @throws GenericFileException
93- * @throws LockedException
94102 * @throws NotFoundException
95- * @throws NotPermittedException
96- * @throws PreConditionNotMetException
97- * @throws UnauthorizedException
98- * @throws ValidationException
99- * @throws NoUserException
100103 */
101104 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 ' );
104113 }
105- $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
106114 $ file = $ userFolder ->getFirstNodeById ($ fileId );
107115 if (!$ file instanceof File) {
116+ $ this ->logger ->warning ('Assistant runFileAction, the input file is not a file ' , ['file_id ' => $ fileId ]);
108117 throw new NotFoundException ('File is not a file ' );
109118 }
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+ }
113127 $ task = new Task (
114128 $ taskTypeId ,
115129 $ input ,
116130 Application::APP_ID ,
117131 $ userId ,
118132 'file-action: ' . $ fileId ,
119133 );
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+ }
121140 $ taskId = $ task ->getId ();
122141 if ($ taskId === null ) {
123142 throw new Exception ('The task could not be scheduled ' );
0 commit comments