import { getCockpitConfig } from './cockpitConfig.js' /** 设备图片与采集附件共用对象基址;绝对签名地址保持原样,不迁移凭据。 */ export function resolveDeviceImageUrl(value, config = getCockpitConfig()) { const raw = typeof value === 'string' ? value.trim() : '' if (!raw) return '' if (/^(blob:|data:image\/)/i.test(raw)) return raw const documentBase = typeof document !== 'undefined' ? document.baseURI : 'http://localhost/' const real = config?.dataSource === 'real' const imageBase = real ? config.images?.baseUrl : '' try { let path = raw if (/^https?:\/\//i.test(raw)) { const absolute = new URL(raw) if (absolute.username || absolute.password) return '' const prefix = ['/pipe/', '/minio/pipe/'].find((item) => absolute.pathname.startsWith(item)) // 查询串可能是签名的一部分;不能重写其主机、路径或编码。 if (!imageBase || absolute.search || !prefix) return raw path = absolute.pathname.slice(prefix.length) } else if (/^[a-z][a-z0-9+.-]*:/i.test(raw)) { return '' } path = path.replace(/\\/g, '/') if (imageBase) { const base = new URL(imageBase.replace(/\/+$/, '') + '/', documentBase) if (!['http:', 'https:'].includes(base.protocol) || base.username || base.password || base.search || base.hash) return '' return new URL(path.replace(/^\/+/, ''), base).href } const apiBase = real ? config.api?.baseUrl : '' const base = apiBase ? new URL(apiBase, documentBase).origin + '/' : documentBase return new URL(path, base).href } catch { return '' } }