| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { useGlobSetting } from '@jeesite/core/hooks/setting';
- import { toLocalMapUrl } from '@jeesite/core/utils/map/offlineMap';
- const { ctxPath } = useGlobSetting();
- const ABSOLUTE_IMAGE_RE = /^(data:image\/|https?:\/\/|blob:|file:|localmap:|\/\/)/i;
- const RAW_BASE64_RE = /^[A-Za-z0-9+/=\s]+$/;
- const WINDOWS_ABSOLUTE_PATH_RE = /^[A-Za-z]:[\\/]/;
- const WINDOWS_UNC_PATH_RE = /^\\\\[^\\]+\\[^\\]+/;
- const appendCtxPath = (path: string) => {
- const normalizedPath = path.startsWith('/') ? path : `/${path}`;
- return ctxPath ? `${ctxPath}${normalizedPath}` : normalizedPath;
- };
- export const normalizeAvatarSrc = (value?: string | null) => {
- const source = String(value ?? '').trim();
- if (!source) return '';
- if (/^file:\/\//i.test(source)) return toLocalMapUrl(source);
- if (ABSOLUTE_IMAGE_RE.test(source)) return source;
- if (WINDOWS_ABSOLUTE_PATH_RE.test(source) || WINDOWS_UNC_PATH_RE.test(source)) {
- return toLocalMapUrl(source);
- }
- if (source.startsWith('/ctxPath/')) {
- return appendCtxPath(source.replace(/^\/ctxPath\/?/i, ''));
- }
- if (source.startsWith('ctxPath/')) {
- return appendCtxPath(source.replace(/^ctxPath\/?/i, ''));
- }
- const isLikelyBase64 =
- RAW_BASE64_RE.test(source) &&
- !source.startsWith('/') &&
- !source.includes('.') &&
- !source.includes('?') &&
- !source.includes('&');
- if (isLikelyBase64) {
- return `data:image/png;base64,${source}`;
- }
- return appendCtxPath(source);
- };
|