From 64bbdf7a73724986fac8bcf6e880fe32ef9ac495 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 18 Sep 2026 19:28:40 -0400 Subject: [PATCH] refac --- backend/open_webui/routers/images.py | 58 +++++++++---------- src/lib/apis/images/index.ts | 12 ++-- .../components/admin/Settings/Images.svelte | 23 +++++--- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/backend/open_webui/routers/images.py b/backend/open_webui/routers/images.py index a93623ee66..f7ea789622 100644 --- a/backend/open_webui/routers/images.py +++ b/backend/open_webui/routers/images.py @@ -329,38 +329,34 @@ def get_automatic1111_api_auth(image_config): return f'Basic {auth1111_base64_encoded_string}' -@router.get('/config/url/verify') -async def verify_url(request: Request, user=Depends(get_admin_user)): - image_config = await get_image_config() - if image_config.IMAGE_GENERATION_ENGINE == 'automatic1111': - try: - session = await get_session() - async with session.get( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/options', - headers={'authorization': get_automatic1111_api_auth(image_config)}, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - r.raise_for_status() - return True - except Exception: - raise HTTPException(status_code=400, detail=ERROR_MESSAGES.INVALID_URL) - elif image_config.IMAGE_GENERATION_ENGINE == 'comfyui': - headers = None - if image_config.COMFYUI_API_KEY: - headers = {'Authorization': f'Bearer {image_config.COMFYUI_API_KEY}'} - try: - session = await get_session() - async with session.get( - url=f'{image_config.COMFYUI_BASE_URL}/object_info', - headers=headers, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - r.raise_for_status() - return True - except Exception: - raise HTTPException(status_code=400, detail=ERROR_MESSAGES.INVALID_URL) +class ConnectionVerificationForm(BaseModel): + engine: str + url: str + key: str | None = None + + +@router.post('/verify') +async def verify_connection(form_data: ConnectionVerificationForm, user=Depends(get_admin_user)): + url = form_data.url.rstrip('/') + headers = {} + if form_data.engine == 'automatic1111': + url = f'{url}/sdapi/v1/options' + if form_data.key is not None: + headers['Authorization'] = f'Basic {base64.b64encode(form_data.key.encode("utf-8")).decode("utf-8")}' + elif form_data.engine == 'comfyui': + url = f'{url}/object_info' + if form_data.key: + headers['Authorization'] = f'Bearer {form_data.key}' else: - return True + raise HTTPException(status_code=400, detail='Unsupported image engine') + + try: + session = await get_session() + async with session.get(url=url, headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL) as r: + r.raise_for_status() + return True + except Exception: + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.INVALID_URL) @router.get('/models') diff --git a/src/lib/apis/images/index.ts b/src/lib/apis/images/index.ts index 5d87c13a18..ad2f79d731 100644 --- a/src/lib/apis/images/index.ts +++ b/src/lib/apis/images/index.ts @@ -67,16 +67,20 @@ export const updateConfig = async (token: string = '', config: object) => { return res; }; -export const verifyConfigUrl = async (token: string = '') => { +export const verifyConnection = async ( + token: string = '', + connection: { engine: string; url: string; key?: string | null } +) => { let error = null; - const res = await fetch(`${IMAGES_API_BASE_URL}/config/url/verify`, { - method: 'GET', + const res = await fetch(`${IMAGES_API_BASE_URL}/verify`, { + method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(token && { authorization: `Bearer ${token}` }) - } + }, + body: JSON.stringify(connection) }) .then(async (res) => { if (!res.ok) throw await res.json(); diff --git a/src/lib/components/admin/Settings/Images.svelte b/src/lib/components/admin/Settings/Images.svelte index 7dd024063b..831667244e 100644 --- a/src/lib/components/admin/Settings/Images.svelte +++ b/src/lib/components/admin/Settings/Images.svelte @@ -11,7 +11,7 @@ updateImageGenerationConfig, getConfig, updateConfig, - verifyConfigUrl + verifyConnection } from '$lib/apis/images'; import Spinner from '$lib/components/common/Spinner.svelte'; import SensitiveInput from '$lib/components/common/SensitiveInput.svelte'; @@ -443,8 +443,11 @@ type="button" aria-label={$i18n.t('settings.admin.images.verifyConnection.label')} on:click={async () => { - await updateConfigHandler(); - const res = await verifyConfigUrl(localStorage.token).catch((error) => { + const res = await verifyConnection(localStorage.token, { + engine: 'automatic1111', + url: config.AUTOMATIC1111_BASE_URL, + key: config.AUTOMATIC1111_API_AUTH + }).catch((error) => { toast.error(`${error}`); return null; }); @@ -511,8 +514,11 @@ type="button" aria-label={$i18n.t('settings.admin.images.verifyConnection.label')} on:click={async () => { - await updateConfigHandler(); - const res = await verifyConfigUrl(localStorage.token).catch((error) => { + const res = await verifyConnection(localStorage.token, { + engine: 'comfyui', + url: config.COMFYUI_BASE_URL, + key: config.COMFYUI_API_KEY + }).catch((error) => { toast.error(`${error}`); return null; }); @@ -821,8 +827,11 @@ type="button" aria-label={$i18n.t('settings.admin.images.verifyConnection.label')} on:click={async () => { - await updateConfigHandler(); - const res = await verifyConfigUrl(localStorage.token).catch((error) => { + const res = await verifyConnection(localStorage.token, { + engine: 'comfyui', + url: config.IMAGES_EDIT_COMFYUI_BASE_URL, + key: config.IMAGES_EDIT_COMFYUI_API_KEY + }).catch((error) => { toast.error(`${error}`); return null; });