Skip to content

Commit 1d5ac94

Browse files
committed
feat: accept name / id as mediaSource prop for Download/Image API endpoint
1 parent 0b5341b commit 1d5ac94

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

_build/js/src/ui/localChat/modalActions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const sendMessage = async (
3030
const message: Prompt = providedMessage
3131
? providedMessage.trim()
3232
: globalState.modal.messageInput.value.trim();
33+
3334
if (!message || globalState.modal.isLoading) {
3435
return;
3536
}

core/components/modai/lexicon/en/default.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@
107107
$_lang['modai.error.unsupported_content_type'] = 'Content type [[+type]] is not supported';
108108
$_lang['modai.error.invalid_data_url'] = 'Invalid data URL';
109109
$_lang['modai.error.failed_to_fetch_image'] = 'Failed to fetch image';
110+
$_lang['modai.error.source_not_found'] = 'MediaSource not found.';
111+
$_lang['modai.error.source_init failed'] = 'Failed to init MediaSource.';

core/components/modai/src/API/Download/Image.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function post(ServerRequestInterface $request): void
2020
$field = $this->modx->getOption('field', $data, '');
2121
$namespace = $this->modx->getOption('namespace', $data, 'modai');
2222
$resource = (int)$this->modx->getOption('resource', $data, 0);
23-
$mediaSource = (int)$this->modx->getOption('mediaSource', $data, 0);
23+
$mediaSource = $this->modx->getOption('mediaSource', $data, '');
2424

2525
if (empty($mediaSource)) {
2626
$mediaSource = Settings::getImageSetting($this->modx, $field, 'media_source', $namespace);
@@ -55,10 +55,23 @@ public function post(ServerRequestInterface $request): void
5555
throw new LexiconException('modai.error.image_download_domain');
5656
}
5757

58-
$source = modMediaSource::getDefaultSource($this->modx, $mediaSource);
58+
59+
if (is_int($mediaSource) || ctype_digit((string)$mediaSource)) {
60+
$source = $this->modx->getObject(modMediaSource::class, [
61+
'id' => (int)$mediaSource,
62+
]);
63+
} else {
64+
$source = $this->modx->getObject(modMediaSource::class, [
65+
'name' => $mediaSource,
66+
]);
67+
}
68+
69+
if (!$source) {
70+
throw new LexiconException('modai.error.source_not_found');
71+
}
5972

6073
if (!$source->initialize()) {
61-
throw new LexiconException('error');
74+
throw new LexiconException('modai.error.source_init failed');
6275
}
6376

6477
$path = Settings::getImageSetting($this->modx, $field, 'path');

0 commit comments

Comments
 (0)