From ec949029fa0868ccb684c12d20b2bc396dd08b00 Mon Sep 17 00:00:00 2001 From: zimplexing Date: Thu, 17 Jul 2025 21:14:03 +0800 Subject: [PATCH] feat: enhance saveSettings function to process API base URL and ensure valid format --- stores/settingsStore.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/stores/settingsStore.ts b/stores/settingsStore.ts index 3381947..576d8a0 100644 --- a/stores/settingsStore.ts +++ b/stores/settingsStore.ts @@ -68,14 +68,35 @@ export const useSettingsStore = create((set, get) => ({ setVideoSource: (config) => set({ videoSource: config }), saveSettings: async () => { const { apiBaseUrl, m3uUrl, remoteInputEnabled, videoSource } = get(); + + let processedApiBaseUrl = apiBaseUrl.trim(); + if (processedApiBaseUrl.endsWith("/")) { + processedApiBaseUrl = processedApiBaseUrl.slice(0, -1); + } + + if (!/^https?:\/\//i.test(processedApiBaseUrl)) { + const hostPart = processedApiBaseUrl.split("/")[0]; + // Simple check for IP address format. + const isIpAddress = /^((\d{1,3}\.){3}\d{1,3})(:\d+)?$/.test(hostPart); + // Check if the domain includes a port. + const hasPort = /:\d+/.test(hostPart); + + if (isIpAddress || hasPort) { + processedApiBaseUrl = "http://" + processedApiBaseUrl; + } else { + processedApiBaseUrl = "https://" + processedApiBaseUrl; + } + } + await SettingsManager.save({ - apiBaseUrl, + apiBaseUrl: processedApiBaseUrl, m3uUrl, remoteInputEnabled, videoSource, }); - api.setBaseUrl(apiBaseUrl); - set({ isModalVisible: false }); + api.setBaseUrl(processedApiBaseUrl); + // Also update the URL in the state so the input field shows the processed URL + set({ isModalVisible: false, apiBaseUrl: processedApiBaseUrl }); await get().fetchServerConfig(); }, showModal: () => set({ isModalVisible: true }),