From 13e60a60488e71b28bb947bd15acb6de01366a7e Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 15 一月 2024 09:36:55 +0800
Subject: [PATCH] update 优化 oss 下载使用临时文件避免线程冲突

---
 ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java |   20 +++-----------------
 1 files changed, 3 insertions(+), 17 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
index 6c402dc..eb1656c 100644
--- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
+++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
@@ -34,7 +34,6 @@
 import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.time.Duration;
 
 /**
@@ -242,16 +241,13 @@
      * @throws OssException 濡傛灉涓嬭浇澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
     public Path fileDownload(String path) {
-        // 浠庤矾寰勪腑绉婚櫎 URL 鍓嶇紑
-        String url = removeBaseUrl(path);
-
-        // 鏋勫缓涓存椂鏂囦欢璺緞 鏂囦欢鍚嶅繀椤绘槸鍞竴涓嶅瓨鍦ㄧ殑锛岃矾寰勫繀椤绘槸瀛樺湪鐨�
-        Path tempFilePath = Paths.get(extractFileName(url));
+        // 鏋勫缓涓存椂鏂囦欢
+        Path tempFilePath = FileUtils.createTempFile().toPath();
         // 浣跨敤 S3TransferManager 涓嬭浇鏂囦欢
         FileDownload downloadFile = transferManager.downloadFile(
             x -> x.getObjectRequest(
                     y -> y.bucket(properties.getBucketName())
-                        .key(url)
+                        .key(removeBaseUrl(path))
                         .build())
                 .addTransferListener(LoggingTransferListener.create())
                 .destination(tempFilePath)
@@ -449,16 +445,6 @@
      */
     public String removeBaseUrl(String path) {
         return path.replace(getUrl() + StringUtils.SLASH, "");
-    }
-
-    /**
-     * 浠庢枃浠惰矾寰勪腑鎻愬彇鏂囦欢鍚�
-     *
-     * @param path 鏂囦欢璺緞
-     * @return 鎻愬彇鐨勬枃浠跺悕鎴栭粯璁ゆ枃浠跺悕
-     */
-    public String extractFileName(String path) {
-        return FileUtils.getTmpDir() + StringUtils.SLASH + Paths.get(path).getFileName().toString();
     }
 
     /**

--
Gitblit v1.9.3