From ca77a052f3af47697cd3cf0a4f71468feb8455af Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期三, 31 十二月 2025 13:42:48 +0800
Subject: [PATCH] yys  巡检bug

---
 ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java |   85 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java b/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
index cf2094e..2f33993 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -17,13 +17,21 @@
 import com.ruoyi.common.utils.uuid.IdUtils;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.poi.util.TempFile;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.UUID;
 import java.util.stream.Collectors;
 
 /**
@@ -46,6 +54,36 @@
     @Autowired
     private MinioUtils minioUtils;
 
+    @Value("${file.file-url}")
+    private String fileUrl;
+
+    @Override
+    public List<StorageBlobDTO> uploads(List<MultipartFile> files, String bucketName, Long type) {
+        // 鑻ユ病浼犲叆bucketName锛屽垯浣跨敤榛樿bucketName
+        if (StringUtils.isEmpty(bucketName)) {
+            bucketName = minioUtils.getDefaultBucket();
+        }
+        List<StorageBlobDTO> storageBlobDTOs = new ArrayList<>();
+        for (MultipartFile file : files) {
+            try {
+                validateFileExtension(file);
+//                MinioResult res = minioUtils.upload(bucketName, file, false);
+                // 涓嶄娇鐢╩inio锛屼娇鐢ㄦ湰鍦版枃浠朵笂浼�
+                String res = uploadFile(file, type);
+                StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type);
+                dto.setFileUrl(res);
+                storageBlobMapper.insert(dto);
+                storageBlobDTOs.add(dto);
+
+            } catch (InvalidExtensionException e) {
+                throw new RuntimeException("涓嶆敮鎸佺殑鏂囦欢绫诲瀷锛�" + file.getOriginalFilename(), e);
+            } catch (Exception e) {
+                throw new RuntimeException("涓婁紶鏂囦欢澶辫触锛�" + file.getOriginalFilename(), e);
+            }
+        }
+        return storageBlobDTOs;
+    }
+
     @Override
     public List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName,Long type) {
 
@@ -57,9 +95,8 @@
         for (MultipartFile file : files) {
             try {
                 validateFileExtension(file);
-
                 MinioResult res = minioUtils.upload(bucketName, file, false);
-
+//                String res = uploadFile(file, type);
                 StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type);
 
                 storageBlobMapper.insert(dto);
@@ -72,6 +109,30 @@
             }
         }
         return storageBlobDTOs;
+    }
+
+    @Value("${file.upload-dir}")
+    private String tempDir;
+
+    public String uploadFile(MultipartFile file, Long type) throws IOException {
+        // 1. 鐢熸垚涓存椂鏂囦欢ID鍜岃矾寰�
+        String tempId = UUID.randomUUID().toString();
+        String originalFilename = file.getOriginalFilename();
+        if(originalFilename == null) throw new IOException("鏂囦欢鍚嶄笉鑳戒负绌�");
+
+        Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename()); //windows涓婁紶璺緞
+        Path prodFilePath = Paths.get(fileUrl, tempId + "_" + file.getOriginalFilename()); //nginx浠g悊璺緞
+
+        // 2. 纭繚鐩綍瀛樺湪
+        Path parentDir = tempFilePath.getParent();
+        if (parentDir != null) {
+            Files.createDirectories(parentDir); // 閫掑綊鍒涘缓鐩綍
+        }
+
+        // 3. 淇濆瓨鏂囦欢鍒颁复鏃剁洰褰�
+        file.transferTo(tempFilePath.toFile());
+
+        return prodFilePath.toString();
     }
 
     private void validateFileExtension(MultipartFile file) throws InvalidExtensionException {
@@ -126,6 +187,24 @@
         return dto;
     }
 
+    private StorageBlobDTO buildStorageBlobDTO(MultipartFile file, String res, String bucketName, Long type) {
+        StorageBlobDTO dto = new StorageBlobDTO();
+        dto.setContentType(file.getContentType());
+        dto.setBucketFilename("sys");
+        dto.setOriginalFilename(file.getOriginalFilename());
+        dto.setByteSize(file.getSize());
+        dto.setKey(IdUtils.simpleUUID());
+        dto.setBucketName(bucketName);
+        dto.setUrl(res);
+        dto.setDownloadUrl(res);
+
+        if (type != null) {
+            dto.setType(type);
+        }
+
+        return dto;
+    }
+
     @Override
     public int deleteStorageBlobs(StorageAttachment attachment) {
         List<StorageAttachment> attachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
@@ -158,4 +237,6 @@
         }
         return 0;
     }
+
+
 }

--
Gitblit v1.9.3