Blame view

稻城亚丁小程序/admin-web/src/utils/mediaUrl.ts 2.35 KB
3a3dc915   王天杨   feat: 稻城亚丁项目批量更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  function adminApiBase(): string {
    const env = import.meta.env.VITE_API_BASE;
    const envBase = typeof env === "string" ? env.trim().replace(/\/+$/, "") : "";
    return envBase;
  }
  
  /**
   * 管理后台下载/水印打包用:经后端 admin/v1/uploads/raw 拉取二进制,避免前端页与静态资源跨域时 fetch 被 CORS 拦截。
   * 展示用仍用 {@link resolveAdminMediaUrl}(img 标签不受同源策略限制)。
   */
  export function resolveAdminUploadBlobFetchUrl(displayUrl: string): string {
    const raw = typeof displayUrl === "string" ? displayUrl.trim() : "";
    if (!raw) return "";
    let path = "";
    try {
      if (raw.startsWith("http://") || raw.startsWith("https://")) {
        path = new URL(raw).pathname;
      } else if (raw.startsWith("/uploads/")) {
        path = raw.split("?")[0] ?? raw;
      }
    } catch {
      return raw;
    }
    if (!path.startsWith("/uploads/")) {
      return raw;
    }
    const base = adminApiBase();
    if (!base) {
      return raw;
    }
    return `${base}/admin/v1/uploads/raw?path=${encodeURIComponent(path)}`;
  }
  
bc518174   王天杨   提交两个项目文件
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  /**
   * 管理后台展示用:将 /uploads/... 转为浏览器可请求的绝对地址。
   * 未配置 VITE_API_BASE 时使用当前页面 origin(开发环境由 Vite 代理 /uploads)。
   */
  export function resolveAdminMediaUrl(path: string | null | undefined): string {
    const raw = typeof path === "string" ? path.trim() : "";
    if (!raw) return "";
    if (raw.startsWith("blob:") || raw.startsWith("data:")) {
      return "";
    }
    if (raw.startsWith("http://") || raw.startsWith("https://")) {
      return raw;
    }
    const uploads = raw.startsWith("/uploads/")
      ? raw
      : raw.startsWith("uploads/")
        ? `/${raw}`
        : "";
    if (uploads) {
3a3dc915   王天杨   feat: 稻城亚丁项目批量更新
53
      const envBase = adminApiBase();
bc518174   王天杨   提交两个项目文件
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
      const base =
        envBase ||
        (typeof window !== "undefined" && window.location?.origin
          ? window.location.origin.replace(/\/+$/, "")
          : "");
      return base ? `${base}${uploads}` : uploads;
    }
    if (raw.startsWith("/") && typeof window !== "undefined" && window.location?.origin) {
      return `${window.location.origin.replace(/\/+$/, "")}${raw}`;
    }
    return raw;
  }
  
  /** 持久化字段:去掉浏览器临时地址,避免写入数据库 */
  export function sanitizeAvatarForSave(path: string): string {
    const t = path.trim();
    if (!t || t.startsWith("blob:") || t.startsWith("data:")) {
      return "";
    }
    return t;
  }