Completed
Push — master ( 0c1be8...d5220d )
by Marcel
34:22
created
core/Controller/TextToImageApiController.php 2 patches
Indentation   +197 added lines, -197 removed lines patch added patch discarded remove patch
@@ -35,201 +35,201 @@
 block discarded – undo
35 35
  * @psalm-import-type CoreTextToImageTask from ResponseDefinitions
36 36
  */
37 37
 class TextToImageApiController extends OCSController {
38
-	public function __construct(
39
-		string $appName,
40
-		IRequest $request,
41
-		private IManager $textToImageManager,
42
-		private IL10N $l,
43
-		private ?string $userId,
44
-		private AppData $appData,
45
-	) {
46
-		parent::__construct($appName, $request);
47
-	}
48
-
49
-	/**
50
-	 * Check whether this feature is available
51
-	 *
52
-	 * @return DataResponse<Http::STATUS_OK, array{isAvailable: bool}, array{}>
53
-	 *
54
-	 * 200: Returns availability status
55
-	 */
56
-	#[NoAdminRequired]
57
-	#[ApiRoute(verb: 'GET', url: '/is_available', root: '/text2image')]
58
-	public function isAvailable(): DataResponse {
59
-		return new DataResponse([
60
-			'isAvailable' => $this->textToImageManager->hasProviders(),
61
-		]);
62
-	}
63
-
64
-	/**
65
-	 * This endpoint allows scheduling a text to image task
66
-	 *
67
-	 * @param string $input Input text
68
-	 * @param string $appId ID of the app that will execute the task
69
-	 * @param string $identifier An arbitrary identifier for the task
70
-	 * @param int $numberOfImages The number of images to generate
71
-	 *
72
-	 * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
73
-	 *
74
-	 * 200: Task scheduled successfully
75
-	 * 412: Scheduling task is not possible
76
-	 */
77
-	#[NoAdminRequired]
78
-	#[UserRateLimit(limit: 20, period: 120)]
79
-	#[ApiRoute(verb: 'POST', url: '/schedule', root: '/text2image')]
80
-	public function schedule(string $input, string $appId, string $identifier = '', int $numberOfImages = 8): DataResponse {
81
-		$task = new Task($input, $appId, $numberOfImages, $this->userId, $identifier);
82
-		try {
83
-			try {
84
-				$this->textToImageManager->runOrScheduleTask($task);
85
-			} catch (TaskFailureException) {
86
-				// Task status was already updated by the manager, nothing to do here
87
-			}
88
-
89
-			$json = $task->jsonSerialize();
90
-
91
-			return new DataResponse([
92
-				'task' => $json,
93
-			]);
94
-		} catch (PreConditionNotMetException) {
95
-			return new DataResponse(['message' => $this->l->t('No text to image provider is available')], Http::STATUS_PRECONDITION_FAILED);
96
-		} catch (Exception) {
97
-			return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
98
-		}
99
-	}
100
-
101
-	/**
102
-	 * This endpoint allows checking the status and results of a task.
103
-	 * Tasks are removed 1 week after receiving their last update.
104
-	 *
105
-	 * @param int $id The id of the task
106
-	 *
107
-	 * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
108
-	 *
109
-	 * 200: Task returned
110
-	 * 404: Task not found
111
-	 */
112
-	#[NoAdminRequired]
113
-	#[BruteForceProtection(action: 'text2image')]
114
-	#[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/text2image')]
115
-	public function getTask(int $id): DataResponse {
116
-		try {
117
-			$task = $this->textToImageManager->getUserTask($id, $this->userId);
118
-
119
-			$json = $task->jsonSerialize();
120
-
121
-			return new DataResponse([
122
-				'task' => $json,
123
-			]);
124
-		} catch (TaskNotFoundException) {
125
-			$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
126
-			$res->throttle(['action' => 'text2image']);
127
-			return $res;
128
-		} catch (\RuntimeException) {
129
-			return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
130
-		}
131
-	}
132
-
133
-	/**
134
-	 * This endpoint allows downloading the resulting image of a task
135
-	 *
136
-	 * @param int $id The id of the task
137
-	 * @param int $index The index of the image to retrieve
138
-	 *
139
-	 * @return FileDisplayResponse<Http::STATUS_OK, array{'Content-Type': string}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
140
-	 *
141
-	 * 200: Image returned
142
-	 * 404: Task or image not found
143
-	 */
144
-	#[NoAdminRequired]
145
-	#[BruteForceProtection(action: 'text2image')]
146
-	#[ApiRoute(verb: 'GET', url: '/task/{id}/image/{index}', root: '/text2image')]
147
-	public function getImage(int $id, int $index): DataResponse|FileDisplayResponse {
148
-		try {
149
-			$task = $this->textToImageManager->getUserTask($id, $this->userId);
150
-			try {
151
-				$folder = $this->appData->getFolder('text2image');
152
-			} catch (NotFoundException) {
153
-				$res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
154
-				$res->throttle(['action' => 'text2image']);
155
-				return $res;
156
-			}
157
-			$file = $folder->getFolder((string)$task->getId())->getFile((string)$index);
158
-			$info = getimagesizefromstring($file->getContent());
159
-
160
-			return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => image_type_to_mime_type($info[2])]);
161
-		} catch (TaskNotFoundException) {
162
-			$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
163
-			$res->throttle(['action' => 'text2image']);
164
-			return $res;
165
-		} catch (\RuntimeException) {
166
-			return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
167
-		} catch (NotFoundException) {
168
-			$res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
169
-			$res->throttle(['action' => 'text2image']);
170
-			return $res;
171
-		}
172
-	}
173
-
174
-	/**
175
-	 * This endpoint allows to delete a scheduled task for a user
176
-	 *
177
-	 * @param int $id The id of the task
178
-	 *
179
-	 * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
180
-	 *
181
-	 * 200: Task returned
182
-	 * 404: Task not found
183
-	 */
184
-	#[NoAdminRequired]
185
-	#[BruteForceProtection(action: 'text2image')]
186
-	#[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/text2image')]
187
-	public function deleteTask(int $id): DataResponse {
188
-		try {
189
-			$task = $this->textToImageManager->getUserTask($id, $this->userId);
190
-
191
-			$this->textToImageManager->deleteTask($task);
192
-
193
-			$json = $task->jsonSerialize();
194
-
195
-			return new DataResponse([
196
-				'task' => $json,
197
-			]);
198
-		} catch (TaskNotFoundException) {
199
-			$res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
200
-			$res->throttle(['action' => 'text2image']);
201
-			return $res;
202
-		} catch (\RuntimeException) {
203
-			return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
204
-		}
205
-	}
206
-
207
-
208
-	/**
209
-	 * This endpoint returns a list of tasks of a user that are related
210
-	 * with a specific appId and optionally with an identifier
211
-	 *
212
-	 * @param string $appId ID of the app
213
-	 * @param string|null $identifier An arbitrary identifier for the task
214
-	 * @return DataResponse<Http::STATUS_OK, array{tasks: list<CoreTextToImageTask>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
215
-	 *
216
-	 * 200: Task list returned
217
-	 */
218
-	#[NoAdminRequired]
219
-	#[AnonRateLimit(limit: 5, period: 120)]
220
-	#[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/text2image')]
221
-	public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
222
-		try {
223
-			$tasks = $this->textToImageManager->getUserTasksByApp($this->userId, $appId, $identifier);
224
-			$json = array_values(array_map(static function (Task $task) {
225
-				return $task->jsonSerialize();
226
-			}, $tasks));
227
-
228
-			return new DataResponse([
229
-				'tasks' => $json,
230
-			]);
231
-		} catch (\RuntimeException) {
232
-			return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
233
-		}
234
-	}
38
+    public function __construct(
39
+        string $appName,
40
+        IRequest $request,
41
+        private IManager $textToImageManager,
42
+        private IL10N $l,
43
+        private ?string $userId,
44
+        private AppData $appData,
45
+    ) {
46
+        parent::__construct($appName, $request);
47
+    }
48
+
49
+    /**
50
+     * Check whether this feature is available
51
+     *
52
+     * @return DataResponse<Http::STATUS_OK, array{isAvailable: bool}, array{}>
53
+     *
54
+     * 200: Returns availability status
55
+     */
56
+    #[NoAdminRequired]
57
+    #[ApiRoute(verb: 'GET', url: '/is_available', root: '/text2image')]
58
+    public function isAvailable(): DataResponse {
59
+        return new DataResponse([
60
+            'isAvailable' => $this->textToImageManager->hasProviders(),
61
+        ]);
62
+    }
63
+
64
+    /**
65
+     * This endpoint allows scheduling a text to image task
66
+     *
67
+     * @param string $input Input text
68
+     * @param string $appId ID of the app that will execute the task
69
+     * @param string $identifier An arbitrary identifier for the task
70
+     * @param int $numberOfImages The number of images to generate
71
+     *
72
+     * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
73
+     *
74
+     * 200: Task scheduled successfully
75
+     * 412: Scheduling task is not possible
76
+     */
77
+    #[NoAdminRequired]
78
+    #[UserRateLimit(limit: 20, period: 120)]
79
+    #[ApiRoute(verb: 'POST', url: '/schedule', root: '/text2image')]
80
+    public function schedule(string $input, string $appId, string $identifier = '', int $numberOfImages = 8): DataResponse {
81
+        $task = new Task($input, $appId, $numberOfImages, $this->userId, $identifier);
82
+        try {
83
+            try {
84
+                $this->textToImageManager->runOrScheduleTask($task);
85
+            } catch (TaskFailureException) {
86
+                // Task status was already updated by the manager, nothing to do here
87
+            }
88
+
89
+            $json = $task->jsonSerialize();
90
+
91
+            return new DataResponse([
92
+                'task' => $json,
93
+            ]);
94
+        } catch (PreConditionNotMetException) {
95
+            return new DataResponse(['message' => $this->l->t('No text to image provider is available')], Http::STATUS_PRECONDITION_FAILED);
96
+        } catch (Exception) {
97
+            return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
98
+        }
99
+    }
100
+
101
+    /**
102
+     * This endpoint allows checking the status and results of a task.
103
+     * Tasks are removed 1 week after receiving their last update.
104
+     *
105
+     * @param int $id The id of the task
106
+     *
107
+     * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
108
+     *
109
+     * 200: Task returned
110
+     * 404: Task not found
111
+     */
112
+    #[NoAdminRequired]
113
+    #[BruteForceProtection(action: 'text2image')]
114
+    #[ApiRoute(verb: 'GET', url: '/task/{id}', root: '/text2image')]
115
+    public function getTask(int $id): DataResponse {
116
+        try {
117
+            $task = $this->textToImageManager->getUserTask($id, $this->userId);
118
+
119
+            $json = $task->jsonSerialize();
120
+
121
+            return new DataResponse([
122
+                'task' => $json,
123
+            ]);
124
+        } catch (TaskNotFoundException) {
125
+            $res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
126
+            $res->throttle(['action' => 'text2image']);
127
+            return $res;
128
+        } catch (\RuntimeException) {
129
+            return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
130
+        }
131
+    }
132
+
133
+    /**
134
+     * This endpoint allows downloading the resulting image of a task
135
+     *
136
+     * @param int $id The id of the task
137
+     * @param int $index The index of the image to retrieve
138
+     *
139
+     * @return FileDisplayResponse<Http::STATUS_OK, array{'Content-Type': string}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
140
+     *
141
+     * 200: Image returned
142
+     * 404: Task or image not found
143
+     */
144
+    #[NoAdminRequired]
145
+    #[BruteForceProtection(action: 'text2image')]
146
+    #[ApiRoute(verb: 'GET', url: '/task/{id}/image/{index}', root: '/text2image')]
147
+    public function getImage(int $id, int $index): DataResponse|FileDisplayResponse {
148
+        try {
149
+            $task = $this->textToImageManager->getUserTask($id, $this->userId);
150
+            try {
151
+                $folder = $this->appData->getFolder('text2image');
152
+            } catch (NotFoundException) {
153
+                $res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
154
+                $res->throttle(['action' => 'text2image']);
155
+                return $res;
156
+            }
157
+            $file = $folder->getFolder((string)$task->getId())->getFile((string)$index);
158
+            $info = getimagesizefromstring($file->getContent());
159
+
160
+            return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => image_type_to_mime_type($info[2])]);
161
+        } catch (TaskNotFoundException) {
162
+            $res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
163
+            $res->throttle(['action' => 'text2image']);
164
+            return $res;
165
+        } catch (\RuntimeException) {
166
+            return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
167
+        } catch (NotFoundException) {
168
+            $res = new DataResponse(['message' => $this->l->t('Image not found')], Http::STATUS_NOT_FOUND);
169
+            $res->throttle(['action' => 'text2image']);
170
+            return $res;
171
+        }
172
+    }
173
+
174
+    /**
175
+     * This endpoint allows to delete a scheduled task for a user
176
+     *
177
+     * @param int $id The id of the task
178
+     *
179
+     * @return DataResponse<Http::STATUS_OK, array{task: CoreTextToImageTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
180
+     *
181
+     * 200: Task returned
182
+     * 404: Task not found
183
+     */
184
+    #[NoAdminRequired]
185
+    #[BruteForceProtection(action: 'text2image')]
186
+    #[ApiRoute(verb: 'DELETE', url: '/task/{id}', root: '/text2image')]
187
+    public function deleteTask(int $id): DataResponse {
188
+        try {
189
+            $task = $this->textToImageManager->getUserTask($id, $this->userId);
190
+
191
+            $this->textToImageManager->deleteTask($task);
192
+
193
+            $json = $task->jsonSerialize();
194
+
195
+            return new DataResponse([
196
+                'task' => $json,
197
+            ]);
198
+        } catch (TaskNotFoundException) {
199
+            $res = new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
200
+            $res->throttle(['action' => 'text2image']);
201
+            return $res;
202
+        } catch (\RuntimeException) {
203
+            return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
204
+        }
205
+    }
206
+
207
+
208
+    /**
209
+     * This endpoint returns a list of tasks of a user that are related
210
+     * with a specific appId and optionally with an identifier
211
+     *
212
+     * @param string $appId ID of the app
213
+     * @param string|null $identifier An arbitrary identifier for the task
214
+     * @return DataResponse<Http::STATUS_OK, array{tasks: list<CoreTextToImageTask>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
215
+     *
216
+     * 200: Task list returned
217
+     */
218
+    #[NoAdminRequired]
219
+    #[AnonRateLimit(limit: 5, period: 120)]
220
+    #[ApiRoute(verb: 'GET', url: '/tasks/app/{appId}', root: '/text2image')]
221
+    public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
222
+        try {
223
+            $tasks = $this->textToImageManager->getUserTasksByApp($this->userId, $appId, $identifier);
224
+            $json = array_values(array_map(static function (Task $task) {
225
+                return $task->jsonSerialize();
226
+            }, $tasks));
227
+
228
+            return new DataResponse([
229
+                'tasks' => $json,
230
+            ]);
231
+        } catch (\RuntimeException) {
232
+            return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
233
+        }
234
+    }
235 235
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
 	#[NoAdminRequired]
145 145
 	#[BruteForceProtection(action: 'text2image')]
146 146
 	#[ApiRoute(verb: 'GET', url: '/task/{id}/image/{index}', root: '/text2image')]
147
-	public function getImage(int $id, int $index): DataResponse|FileDisplayResponse {
147
+	public function getImage(int $id, int $index): DataResponse | FileDisplayResponse {
148 148
 		try {
149 149
 			$task = $this->textToImageManager->getUserTask($id, $this->userId);
150 150
 			try {
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
 				$res->throttle(['action' => 'text2image']);
155 155
 				return $res;
156 156
 			}
157
-			$file = $folder->getFolder((string)$task->getId())->getFile((string)$index);
157
+			$file = $folder->getFolder((string) $task->getId())->getFile((string) $index);
158 158
 			$info = getimagesizefromstring($file->getContent());
159 159
 
160 160
 			return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => image_type_to_mime_type($info[2])]);
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
 	public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
222 222
 		try {
223 223
 			$tasks = $this->textToImageManager->getUserTasksByApp($this->userId, $appId, $identifier);
224
-			$json = array_values(array_map(static function (Task $task) {
224
+			$json = array_values(array_map(static function(Task $task) {
225 225
 				return $task->jsonSerialize();
226 226
 			}, $tasks));
227 227
 
Please login to merge, or discard this patch.