Ver código fonte

大屏修改

wwh 1 mês atrás
pai
commit
7e6766a78c

+ 52 - 2
baqing-admin/src/main/java/com/ruoyi/web/kb/support/KbFileUrlResolver.java

@@ -1,5 +1,7 @@
1 1
 package com.ruoyi.web.kb.support;
2 2
 
3
+import java.net.URI;
4
+import java.net.URISyntaxException;
3 5
 import com.ruoyi.common.exception.ServiceException;
4 6
 import com.ruoyi.common.utils.StringUtils;
5 7
 
@@ -8,6 +10,12 @@ import com.ruoyi.common.utils.StringUtils;
8 10
  */
9 11
 public final class KbFileUrlResolver
10 12
 {
13
+    /** 外网域名:同步知识库时若正文 URL 含此串,则改写为内网地址供知识库拉取 */
14
+    private static final String EXTRANET_HOST = "bqxm.alafarms.com";
15
+
16
+    /** 内网文件服务地址(含端口) */
17
+    private static final String INTRANET_HOST = "192.168.153.16:443";
18
+
11 19
     private KbFileUrlResolver()
12 20
     {
13 21
     }
@@ -25,7 +33,7 @@ public final class KbFileUrlResolver
25 33
         String p = contentFileUrl.trim();
26 34
         if (StringUtils.startsWithIgnoreCase(p, "http://") || StringUtils.startsWithIgnoreCase(p, "https://"))
27 35
         {
28
-            return p;
36
+            return rewriteExtranetToIntranetIfNeeded(contentFileUrl, p);
29 37
         }
30 38
         if (StringUtils.isEmpty(fileUrlBase))
31 39
         {
@@ -37,6 +45,48 @@ public final class KbFileUrlResolver
37 45
             base = base.substring(0, base.length() - 1);
38 46
         }
39 47
         String path = p.startsWith("/") ? p : "/" + p;
40
-        return base + path;
48
+        return rewriteExtranetToIntranetIfNeeded(contentFileUrl, base + path);
49
+    }
50
+
51
+    /**
52
+     * 正文 URL 含外网域名时,将解析结果中的域名替换为内网地址,便于知识库节点拉取文件。
53
+     */
54
+    static String rewriteExtranetToIntranetIfNeeded(String contentFileUrl, String resolvedUrl)
55
+    {
56
+        if (StringUtils.isEmpty(contentFileUrl) || StringUtils.isEmpty(resolvedUrl))
57
+        {
58
+            return resolvedUrl;
59
+        }
60
+        if (!contentFileUrl.contains(EXTRANET_HOST))
61
+        {
62
+            return resolvedUrl;
63
+        }
64
+        try
65
+        {
66
+            URI uri = new URI(resolvedUrl);
67
+            if (uri.getHost() == null || !EXTRANET_HOST.equalsIgnoreCase(uri.getHost()))
68
+            {
69
+                return resolvedUrl;
70
+            }
71
+            StringBuilder sb = new StringBuilder();
72
+            sb.append(uri.getScheme()).append("://").append(INTRANET_HOST);
73
+            if (uri.getRawPath() != null)
74
+            {
75
+                sb.append(uri.getRawPath());
76
+            }
77
+            if (uri.getRawQuery() != null)
78
+            {
79
+                sb.append('?').append(uri.getRawQuery());
80
+            }
81
+            if (uri.getRawFragment() != null)
82
+            {
83
+                sb.append('#').append(uri.getRawFragment());
84
+            }
85
+            return sb.toString();
86
+        }
87
+        catch (URISyntaxException e)
88
+        {
89
+            return resolvedUrl;
90
+        }
41 91
     }
42 92
 }

+ 58 - 0
baqing-admin/src/test/java/com/ruoyi/web/kb/support/KbFileUrlResolverTest.java

@@ -0,0 +1,58 @@
1
+package com.ruoyi.web.kb.support;
2
+
3
+import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.junit.jupiter.api.Assertions.assertThrows;
5
+
6
+import org.junit.jupiter.api.DisplayName;
7
+import org.junit.jupiter.api.Test;
8
+import com.ruoyi.common.exception.ServiceException;
9
+
10
+@DisplayName("KbFileUrlResolver")
11
+class KbFileUrlResolverTest
12
+{
13
+    @Test
14
+    @DisplayName("外网域名改写为内网地址")
15
+    void rewriteExtranetHost()
16
+    {
17
+        String url = "https://bqxm.alafarms.com/profile/2026/01/a.pdf";
18
+        String resolved = KbFileUrlResolver.resolveForKbUpload(url, null);
19
+        assertEquals("https://192.168.153.16:443/profile/2026/01/a.pdf", resolved);
20
+    }
21
+
22
+    @Test
23
+    @DisplayName("不含外网域名时不改写")
24
+    void noRewriteWhenHostAbsent()
25
+    {
26
+        String url = "https://other.example.com/file.pdf";
27
+        assertEquals(url, KbFileUrlResolver.resolveForKbUpload(url, null));
28
+    }
29
+
30
+    @Test
31
+    @DisplayName("相对路径拼接后按 contentFileUrl 是否含外网域名决定是否改写 base")
32
+    void relativePathWithExtranetInContentUrl()
33
+    {
34
+        String content = "https://bqxm.alafarms.com/profile/a.pdf";
35
+        assertEquals(
36
+                "https://192.168.153.16:443/profile/a.pdf",
37
+                KbFileUrlResolver.resolveForKbUpload(content, "https://ignored.example.com"));
38
+    }
39
+
40
+    @Test
41
+    @DisplayName("相对路径且 base 含外网域名时改写拼接结果")
42
+    void relativePathExtranetInBaseWhenContentContainsHost()
43
+    {
44
+        String content = "https://bqxm.alafarms.com/profile/a.pdf";
45
+        // absolute URL path — already covered; test relative with marker in content
46
+        String relativeContent = "/profile/b.pdf?from=bqxm.alafarms.com";
47
+        String resolved = KbFileUrlResolver.resolveForKbUpload(
48
+                relativeContent, "https://bqxm.alafarms.com");
49
+        assertEquals("https://192.168.153.16:443/profile/b.pdf?from=bqxm.alafarms.com", resolved);
50
+    }
51
+
52
+    @Test
53
+    @DisplayName("正文地址为空时抛错")
54
+    void emptyUrlThrows()
55
+    {
56
+        assertThrows(ServiceException.class, () -> KbFileUrlResolver.resolveForKbUpload("", "http://base"));
57
+    }
58
+}