deason 1 год назад
Родитель
Сommit
dff37d2f2b

+ 40 - 2
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/FssHelper.java

@@ -8,6 +8,8 @@ import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.security.MessageDigest;
 
 public class FssHelper {
@@ -24,14 +26,14 @@ public class FssHelper {
      *                 aliyun-2://abc/123.jpg
      *                 /abc/123.jpg
      *                 abc/123.jpg
-     * @return 最终访问地址
+     * @return 最终访问地址,如: https://xxx.xxx/abc/123.jpg
      */
     public static String finalFileUrl(String filePath) {
         if (StringUtils.isEmpty(filePath)) {
             return "";
         }
 
-        if (filePath.startsWith("http") || filePath.startsWith("https")) {
+        if (filePath.toLowerCase().startsWith("http")) {
             return filePath;
         }
 
@@ -44,6 +46,42 @@ public class FssHelper {
         return FssProperty.FSS_URL_PREFIX + FssProperty.FSS_SEPARATOR + filePath;
     }
 
+    /**
+     * 修正文件存储路径,若包含域名则去掉(兼容旧数据)
+     *
+     * @param filePath 文件存储路径,格式示例:
+     *                 https://xxx.xxx/abc/123.jpg
+     *                 http://127.0.0.1:8080/abc/123.jpg
+     *                 upyun-1://abc/123.jpg
+     *                 aliyun-1://abc/123.jpg
+     *                 aliyun-2://abc/123.jpg
+     *                 /abc/123.jpg
+     *                 abc/123.jpg
+     * @return 标准文件存储路径,如:/abc/123.jpg
+     */
+    public static String fixFilePath(String filePath) {
+        if (StringUtils.isEmpty(filePath)) {
+            return "";
+        }
+
+        if (filePath.toLowerCase().startsWith("http")) {
+            try {
+                URL url = new URL(filePath);
+                return url.getPath();
+            } catch (MalformedURLException e) {
+                log.error(filePath, e.getMessage(), e);
+                throw new RuntimeException("文件路径格式错误!");
+            }
+        }
+
+        filePath = filePath.replace("upyun-1://", "").replace("aliyun-1://", "").replace("aliyun-2://", "");
+
+        if (!filePath.startsWith("/")) {
+            return FssProperty.FSS_SEPARATOR + filePath;
+        }
+        return filePath;
+    }
+
     /**
      * 创建文件目录
      */