Skip to content

Commit de41ef8

Browse files
authored
Merge pull request #315 from nextcloud/fix/texttoimage-userfacingerrormessages
fix(TextToImageProvider): Use userFacingErrorMessages
2 parents 47036c0 + f7c71dc commit de41ef8

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/TaskProcessing/TextToImageProvider.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
use OCP\IAppConfig;
1717
use OCP\IL10N;
1818
use OCP\TaskProcessing\EShapeType;
19+
use OCP\TaskProcessing\Exception\ProcessingException;
20+
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
1921
use OCP\TaskProcessing\ISynchronousWatermarkingProvider;
2022
use OCP\TaskProcessing\ShapeDescriptor;
2123
use OCP\TaskProcessing\TaskTypes\TextToImage;
2224
use Psr\Log\LoggerInterface;
23-
use RuntimeException;
2425

2526
class TextToImageProvider implements ISynchronousWatermarkingProvider {
2627

@@ -110,19 +111,29 @@ public function process(?string $userId, array $input, callable $reportProgress,
110111
$startTime = time();
111112

112113
if (!isset($input['input']) || !is_string($input['input'])) {
113-
throw new RuntimeException('Invalid prompt');
114+
throw new ProcessingException('Invalid prompt');
114115
}
115116
$prompt = $input['input'];
116117

117118
$nbImages = 1;
118119
if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) {
119120
$nbImages = $input['numberOfImages'];
120121
}
122+
if ($nbImages > 12) {
123+
throw new UserFacingProcessingException('numberOfImages is out of bounds', userFacingMessage: $this->l->t('Cannot generate more than 12 images at once'));
124+
}
125+
if ($nbImages < 1) {
126+
throw new UserFacingProcessingException('numberOfImages is out of bounds', userFacingMessage: $this->l->t('Cannot generate less than 1 image'));
127+
}
121128

122129
$size = $this->appConfig->getValueString(Application::APP_ID, 'default_image_size', lazy: true) ?: Application::DEFAULT_DEFAULT_IMAGE_SIZE;
123130
if (isset($input['size']) && is_string($input['size']) && preg_match('/^\d+x\d+$/', $input['size'])) {
124131
$size = trim($input['size']);
125132
}
133+
[$x, $y] = explode('x', $size, 2);
134+
if ((int)$x > 4096 || (int)$y > 4096) {
135+
throw new UserFacingProcessingException('size is out of bounds', userFacingMessage: $this->l->t('Cannot generate images larger than 4096x4096'));
136+
}
126137

127138
if (isset($input['model']) && is_string($input['model'])) {
128139
$model = $input['model'];
@@ -150,7 +161,7 @@ public function process(?string $userId, array $input, callable $reportProgress,
150161

151162
if (empty($urls) && empty($b64s)) {
152163
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
153-
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
164+
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
154165
}
155166
$client = $this->clientService->newClient();
156167
$requestOptions = $this->openAiAPIService->getImageRequestOptions($userId);
@@ -172,7 +183,7 @@ public function process(?string $userId, array $input, callable $reportProgress,
172183
return $output;
173184
} catch (\Exception $e) {
174185
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage(), ['exception' => $e]);
175-
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
186+
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
176187
}
177188
}
178189
}

0 commit comments

Comments
 (0)