Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Desktop] Fix mirror validation in settings dialog #2375

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/components/install/mirror/MirrorItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
</div>
<UrlInput
v-model="modelValue"
:validate-url-fn="checkMirrorReachable"
:validate-url-fn="
(mirror: string) =>
checkMirrorReachable(mirror + (item.validationPathSuffix ?? ''))
"
@state-change="validationState = $event"
/>
</div>
Expand All @@ -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<{
Expand All @@ -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
})
Expand Down
21 changes: 18 additions & 3 deletions src/extensions/core/electronAdapter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}
],

Expand Down
14 changes: 14 additions & 0 deletions src/utils/networkUtil.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> => {
try {
Expand All @@ -10,3 +13,14 @@ export const checkUrlReachable = async (url: string): Promise<boolean> => {
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))
)
}