From 2f20856ea3ca6f2f238ba66252164687f47fcd7e Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期二, 21 四月 2026 17:24:11 +0800
Subject: [PATCH] chore(deps): 更新 mybatis-plus 版本到 3.5.15

---
 src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java |  172 +++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 107 insertions(+), 65 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
index 6be4401..b576d23 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -1,103 +1,145 @@
 package com.ruoyi.basic.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.basic.dto.StorageBlobDTO;
-import com.ruoyi.basic.mapper.StorageAttachmentMapper;
+import com.ruoyi.basic.dto.StorageBlobVO;
 import com.ruoyi.basic.mapper.StorageBlobMapper;
-import com.ruoyi.basic.pojo.StorageAttachment;
 import com.ruoyi.basic.pojo.StorageBlob;
 import com.ruoyi.basic.service.StorageBlobService;
-import com.ruoyi.common.exception.file.InvalidExtensionException;
-import com.ruoyi.common.utils.DateUtils;
-import com.ruoyi.common.utils.MinioUtils;
-import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.uuid.IdUtils;
-import com.ruoyi.framework.web.domain.MinioResult;
+import com.ruoyi.basic.utils.FileUtil;
+import com.ruoyi.common.config.FileProperties;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.security.Keys;
 import lombok.RequiredArgsConstructor;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.crypto.SecretKey;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.stream.Collectors;
+import java.util.UUID;
 
 /**
  * <p>
  * 閫氱敤鏂囦欢涓婁紶鐨勯檮浠朵俊鎭� 鏈嶅姟瀹炵幇绫�
  * </p>
  *
- * @author ruoyi
- * @since 2025-05-29
  */
 @Service
 @RequiredArgsConstructor
 public class StorageBlobServiceImpl extends ServiceImpl<StorageBlobMapper, StorageBlob> implements StorageBlobService {
-    @Autowired
-    private StorageAttachmentMapper storageAttachmentMapper;
-
-    @Autowired
-    private StorageBlobMapper storageBlobMapper;
-
-    @Autowired
-    private MinioUtils minioUtils;
+    private final FileProperties properties;
+    private final StorageBlobMapper storageBlobMapper;
+    private final FileUtil fileUtil;
 
     @Override
-    public List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName) {
-
-        // 鑻ユ病浼犲叆bucketName锛屽垯浣跨敤榛樿bucketName
-        if (StringUtils.isEmpty(bucketName)) {
-            bucketName  = minioUtils.getDefaultBucket();
+    public List<StorageBlobVO> upload(List<MultipartFile> files) {
+        if (CollectionUtils.isEmpty(files)) {
+            throw new IllegalArgumentException("鏂囦欢涓嶈兘涓虹┖");
         }
 
-        List<StorageBlobDTO> storageBlobDTOs = new ArrayList<>();
+        List<StorageBlobVO> storageBlobVOS = new ArrayList<>();
+
         for (MultipartFile file : files) {
-            try {
-                MinioResult res = minioUtils.upload(bucketName, file, false);
-                StorageBlobDTO dto = new StorageBlobDTO();
-                dto.setContentType(file.getContentType());
-                dto.setBucketFilename(res.getBucketFileName());
-                dto.setOriginalFilename(res.getOriginalName());
-                dto.setByteSize(file.getSize());
-                dto.setKey(IdUtils.simpleUUID());
-                dto.setBucketName(bucketName);
-                dto.setCreateTime(DateUtils.getNowDate());
-                dto.setUrl(minioUtils.getPreviewUrl(res.getBucketFileName(), bucketName, false));
-                // 鎻掑叆鏁版嵁搴�
-                storageBlobMapper.insert(dto);
-
-                storageBlobDTOs.add(dto);
-            } catch (InvalidExtensionException e) {
-                throw new RuntimeException("minio鏂囦欢涓婁紶寮傚父锛�" +  e);
+            if (file == null || file.isEmpty()) {
+                throw new IllegalArgumentException("鏂囦欢涓嶈兘涓虹┖");
             }
+
+            String originalFileName = StringUtils.hasText(file.getOriginalFilename())
+                    ? StringUtils.cleanPath(file.getOriginalFilename())
+                    : UUID.randomUUID().toString();
+            String fileName = UUID.randomUUID() + "_" + originalFileName;
+            String relativePath = fileUtil.buildRelativePath();
+            File targetDirectory = new File(properties.getPath(), relativePath);
+            if (!targetDirectory.exists() && !targetDirectory.mkdirs()) {
+                throw new RuntimeException("鍒涘缓涓婁紶鐩綍澶辫触");
+            }
+            File dest = new File(targetDirectory, fileName);
+
+            StorageBlobVO storageBlob;
+            try {
+                file.transferTo(dest);
+                storageBlob = getStorageBlob(file, originalFileName, fileName, relativePath);
+                if (storageBlob == null || storageBlob.getId() == null) {
+                    throw new RuntimeException("鏂囦欢鍏冩暟鎹繚瀛樺け璐�");
+                }
+            } catch (RuntimeException e) {
+                if (dest.exists()) {
+                    dest.delete();
+                }
+                throw e;
+            } catch (IOException e) {
+                throw new RuntimeException("鏂囦欢淇濆瓨澶辫触", e);
+            }
+
+            storageBlobVOS.add(storageBlob);
         }
-
-
-        return storageBlobDTOs;
+        return storageBlobVOS;
     }
 
     @Override
-    public int deleteStorageBlobs(StorageAttachment attachment) {
-        List<StorageAttachment> attachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
-                .eq(StorageAttachment::getRecordId, attachment.getRecordId())
-                .eq(StorageAttachment::getRecordType, attachment.getRecordType())
-                .eq(StorageAttachment::getName, attachment.getName()));
-        List<Long> ids = attachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
-        List<StorageBlob> storageBlobs = storageBlobMapper.selectList(new LambdaQueryWrapper<StorageBlob>()
-                .in(StorageBlob::getId, ids));
-        if (!storageBlobs.isEmpty()) {
-            for (StorageBlob storageBlob : storageBlobs) {
-                // 绉婚櫎妗跺唴鏂囦欢
-                minioUtils.removeObjectsResult(storageBlob.getBucketName(), storageBlob.getBucketName());
-            }
+    public File getFileByToken(String fileName, String token) {
+        if (!StringUtils.hasText(token)) {
+            throw new IllegalArgumentException("token涓嶈兘涓虹┖");
         }
 
-        if (!ids.isEmpty()) {
-            return storageBlobMapper.delete(new QueryWrapper<StorageBlob>().lambda().in(StorageBlob::getId, ids));
-        }
+        String secretStr = StringUtils.hasText(properties.getJwtSecret())
+                ? properties.getJwtSecret()
+                : "local-file-jwt-secret";
 
-        return 0;
+        SecretKey key = Keys.hmacShaKeyFor(secretStr.getBytes(StandardCharsets.UTF_8));
+        Claims claims = Jwts.parser()
+                .verifyWith(key)          // 浠f浛鏃х増鐨� setSigningKey
+                .build()                  // 蹇呴』鍏堟瀯寤鸿В鏋愬櫒
+                .parseSignedClaims(token) // 浠f浛鏃х増鐨� parseClaimsJws
+                .getPayload();            // 浠f浛鏃х増鐨� getBody()
+        if (!fileName.equals(claims.getSubject())) {
+            throw new IllegalArgumentException("token涓庢枃浠朵笉鍖归厤");
+        }
+        fileUtil.validateTokenUsage(token);
+        StorageBlob storageBlob = findStorageBlob(fileName);
+        String path = storageBlob == null ? claims.get("path", String.class) : storageBlob.getPath();
+        if (!StringUtils.hasText(path)) {
+            return new File(properties.getPath(), fileName);
+        }
+        return new File(new File(properties.getPath(), path), fileName);
+    }
+
+    private StorageBlob findStorageBlob(String fileName) {
+        return storageBlobMapper.selectOne(new LambdaQueryWrapper<StorageBlob>()
+                .eq(StorageBlob::getUidFilename, fileName)
+                .last("limit 1"));
+    }
+
+    private StorageBlobVO getStorageBlob(MultipartFile file, String originalFileName, String fileName, String relativePath) {
+        StorageBlobVO storageBlob = new StorageBlobVO();
+        storageBlob.setResourceKey(UUID.randomUUID().toString().replace("-", ""));
+        storageBlob.setContentType(file.getContentType());
+        storageBlob.setOriginalFilename(originalFileName);
+        storageBlob.setUidFilename(fileName);
+        storageBlob.setByteSize(file.getSize());
+        storageBlob.setPath(relativePath);
+        storageBlob.setPreviewURL(fileUtil.buildSignedPreviewUrl(storageBlob));
+        storageBlob.setDownloadURL(fileUtil.buildSignedDownloadUrl(storageBlob));
+        int affectedRows = storageBlobMapper.insert(storageBlob);
+        if (affectedRows <= 0) {
+            throw new RuntimeException("鏂囦欢鍏冩暟鎹繚瀛樺け璐�");
+        }
+        return storageBlob;
+    }
+
+    @Override
+    public String getDownloadFileName(String fileName) {
+        StorageBlob storageBlob = findStorageBlob(fileName);
+        if (storageBlob == null || !StringUtils.hasText(storageBlob.getOriginalFilename())) {
+            return fileName;
+        }
+        return storageBlob.getOriginalFilename();
     }
 }

--
Gitblit v1.9.3