From 766710cf37b9c05c39cdffcdd821920e53c918e4 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Wed, 29 Jan 2025 14:57:36 -0800 Subject: [PATCH] [Desktop] Fix mirror validation in settings dialog (#2375) --- src/components/install/mirror/MirrorItem.vue | 18 ++++++----------- src/extensions/core/electronAdapter.ts | 21 +++++++++++++++++--- src/utils/networkUtil.ts | 14 +++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/components/install/mirror/MirrorItem.vue b/src/components/install/mirror/MirrorItem.vue index 47056c3cf..2aa134a83 100644 --- a/src/components/install/mirror/MirrorItem.vue +++ b/src/components/install/mirror/MirrorItem.vue @@ -10,7 +10,10 @@ @@ -20,8 +23,8 @@ import { computed, onMounted, ref, watch } from 'vue' import { UVMirror } from '@/constants/uvMirrors' -import { electronAPI } from '@/utils/envUtil' -import { isValidUrl, normalizeI18nKey } from '@/utils/formatUtil' +import { normalizeI18nKey } from '@/utils/formatUtil' +import { checkMirrorReachable } from '@/utils/networkUtil' import { ValidationState } from '@/utils/validationUtil' const { item } = defineProps<{ @@ -39,15 +42,6 @@ const normalizedSettingId = computed(() => { return normalizeI18nKey(item.settingId) }) -const checkMirrorReachable = async (mirror: string) => { - return ( - isValidUrl(mirror) && - (await electronAPI().NetWork.canAccessUrl( - mirror + (item.validationPathSuffix ?? '') - )) - ) -} - onMounted(() => { modelValue.value = item.mirror }) diff --git a/src/extensions/core/electronAdapter.ts b/src/extensions/core/electronAdapter.ts index 673bc04ce..abbef6f24 100644 --- a/src/extensions/core/electronAdapter.ts +++ b/src/extensions/core/electronAdapter.ts @@ -1,8 +1,10 @@ +import { PYTHON_MIRROR } from '@/constants/uvMirrors' import { t } from '@/i18n' import { app } from '@/scripts/app' import { useDialogService } from '@/services/dialogService' import { useWorkflowStore } from '@/stores/workflowStore' import { electronAPI as getElectronAPI, isElectron } from '@/utils/envUtil' +import { checkMirrorReachable } from '@/utils/networkUtil' ;(async () => { if (!isElectron()) return @@ -60,21 +62,34 @@ import { electronAPI as getElectronAPI, isElectron } from '@/utils/envUtil' name: 'Python Install Mirror', tooltip: `Managed Python installations are downloaded from the Astral python-build-standalone project. This variable can be set to a mirror URL to use a different source for Python installations. The provided URL will replace https://github.com/astral-sh/python-build-standalone/releases/download in, e.g., https://github.com/astral-sh/python-build-standalone/releases/download/20240713/cpython-3.12.4%2B20240713-aarch64-apple-darwin-install_only.tar.gz. Distributions can be read from a local directory by using the file:// URL scheme.`, type: 'url', - defaultValue: '' + defaultValue: '', + attrs: { + validateUrlFn(mirror: string) { + return checkMirrorReachable( + mirror + PYTHON_MIRROR.validationPathSuffix + ) + } + } }, { id: 'Comfy-Desktop.UV.PypiInstallMirror', name: 'Pypi Install Mirror', tooltip: `Default pip install mirror`, type: 'url', - defaultValue: '' + defaultValue: '', + attrs: { + validateUrlFn: checkMirrorReachable + } }, { id: 'Comfy-Desktop.UV.TorchInstallMirror', name: 'Torch Install Mirror', tooltip: `Pip install mirror for pytorch`, type: 'url', - defaultValue: '' + defaultValue: '', + attrs: { + validateUrlFn: checkMirrorReachable + } } ], diff --git a/src/utils/networkUtil.ts b/src/utils/networkUtil.ts index 65ea23bd8..a4a93d4f3 100644 --- a/src/utils/networkUtil.ts +++ b/src/utils/networkUtil.ts @@ -1,5 +1,8 @@ import axios from 'axios' +import { electronAPI } from './envUtil' +import { isValidUrl } from './formatUtil' + const VALID_STATUS_CODES = [200, 201, 301, 302, 307, 308] export const checkUrlReachable = async (url: string): Promise => { try { @@ -10,3 +13,14 @@ export const checkUrlReachable = async (url: string): Promise => { return false } } + +/** + * Check if a mirror is reachable from the electron App. + * @param mirror - The mirror to check. + * @returns True if the mirror is reachable, false otherwise. + */ +export const checkMirrorReachable = async (mirror: string) => { + return ( + isValidUrl(mirror) && (await electronAPI().NetWork.canAccessUrl(mirror)) + ) +}