87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import {
|
|
dataSourceConfig
|
|
} from '@/config/dataSource'
|
|
|
|
const isAbsoluteHttpUrl = (url: string) => /^https?:\/\//i.test(url)
|
|
|
|
const defaultSameOriginAssetHosts = [
|
|
['1', '92', '206', '90'].join('.')
|
|
]
|
|
|
|
const defaultLegacyAudioHosts = [
|
|
['47', '120', '48', '148'].join('.')
|
|
]
|
|
|
|
const configuredSameOriginAssetHosts = () => [
|
|
dataSourceConfig.publicSameOriginAssetHost,
|
|
...defaultSameOriginAssetHosts
|
|
].filter(Boolean)
|
|
|
|
const configuredLegacyAudioHosts = () => [
|
|
dataSourceConfig.publicLegacyAudioHost,
|
|
...defaultLegacyAudioHosts
|
|
].filter(Boolean)
|
|
|
|
const resolveSdkMinioProxyBaseUrl = () => {
|
|
const apiBaseUrl = dataSourceConfig.apiBaseUrl.trim().replace(/\/+$/, '')
|
|
const normalizedApiBaseUrl = apiBaseUrl.endsWith('/app-api')
|
|
? apiBaseUrl
|
|
: `${apiBaseUrl}/app-api`
|
|
return `${normalizedApiBaseUrl}/gis/sdk/minio`
|
|
}
|
|
|
|
const toSdkMinioProxyUrl = (assetPath: string) => `${resolveSdkMinioProxyBaseUrl()}${assetPath}`
|
|
|
|
export const normalizeSameOriginPublicUrl = (url: string | null | undefined) => {
|
|
const value = url?.trim()
|
|
|
|
// The SDK proxy is public and provides the same disk and HTTP cache used by
|
|
// the kiosk client. Guide images and audio should use it instead of bypassing
|
|
// the cache through the MinIO public endpoint.
|
|
if (value?.startsWith('/minio/museum-assets/')) {
|
|
return toSdkMinioProxyUrl(value.replace(/^\/minio/, ''))
|
|
}
|
|
|
|
if (value?.startsWith('/museum-assets/') || value?.startsWith('/tts-audio/')) {
|
|
return toSdkMinioProxyUrl(value)
|
|
}
|
|
|
|
if (!value || !isAbsoluteHttpUrl(value)) return value || ''
|
|
|
|
try {
|
|
const parsed = new URL(value)
|
|
const isSameServer = configuredSameOriginAssetHosts().includes(parsed.hostname)
|
|
|
|
if (
|
|
isSameServer
|
|
&& parsed.port === '9000'
|
|
&& (
|
|
parsed.pathname.startsWith('/museum-assets/')
|
|
|| parsed.pathname.startsWith('/tts-audio/')
|
|
)
|
|
) {
|
|
return toSdkMinioProxyUrl(`${parsed.pathname}${parsed.search}${parsed.hash}`)
|
|
}
|
|
|
|
if (
|
|
isSameServer
|
|
&& parsed.port === '48080'
|
|
&& parsed.pathname.startsWith('/yudao-server/')
|
|
) {
|
|
return `${parsed.pathname}${parsed.search}${parsed.hash}`
|
|
}
|
|
|
|
if (
|
|
configuredLegacyAudioHosts().includes(parsed.hostname)
|
|
&& parsed.port === '19000'
|
|
&& parsed.pathname.startsWith('/nhm/audio/')
|
|
) {
|
|
return `${parsed.pathname}${parsed.search}${parsed.hash}`
|
|
}
|
|
} catch {
|
|
return value
|
|
}
|
|
|
|
return value
|
|
}
|