deviceImage.js 1.7 KB

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