From b4a9e12e00b78e1aef8acda070434de9ffd0d66a Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期五, 24 四月 2026 15:29:19 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_pro' into dev_New_pro
---
src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java | 235 +++++++++++++++++++++++-----------------------------------
1 files changed, 94 insertions(+), 141 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 d85a522..4fb85e2 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java
@@ -1,190 +1,143 @@
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.base.BaseException;
-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.apache.commons.io.FilenameUtils;
-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 java.time.LocalDateTime;
+import javax.crypto.SecretKey;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
-import java.util.Arrays;
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.setResourceKey(IdUtils.simpleUUID());
- dto.setBucketName(bucketName);
- 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 List<StorageBlobDTO> updateStorageBlobs(List<MultipartFile> files, String bucketName, Long type) {
-
- // 鑻ユ病浼犲叆bucketName锛屽垯浣跨敤榛樿bucketName
- if (StringUtils.isEmpty(bucketName)) {
- bucketName = minioUtils.getDefaultBucket();
+ public File getFileByToken(String fileName, String token) {
+ if (!StringUtils.hasText(token)) {
+ throw new IllegalArgumentException("token涓嶈兘涓虹┖");
}
- List<StorageBlobDTO> storageBlobDTOs = new ArrayList<>();
- for (MultipartFile file : files) {
- try {
- validateFileExtension(file);
- MinioResult res = minioUtils.upload(bucketName, file, false);
+ String secretStr = properties.getJwtSecret();
- StorageBlobDTO dto = buildStorageBlobDTO(file, res, bucketName, type);
-
- storageBlobMapper.insert(dto);
- storageBlobDTOs.add(dto);
-
- } catch (InvalidExtensionException e) {
- throw new RuntimeException("涓嶆敮鎸佺殑鏂囦欢绫诲瀷锛�" + file.getOriginalFilename(), e);
- } catch (Exception e) {
- throw new RuntimeException("涓婁紶鏂囦欢澶辫触锛�" + file.getOriginalFilename(), e);
- }
+ 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涓庢枃浠朵笉鍖归厤");
}
- return storageBlobDTOs;
+ 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 StorageBlobDTO buildStorageBlobDTO(MultipartFile file, MinioResult res, String bucketName, Long type) {
- StorageBlobDTO dto = new StorageBlobDTO();
- dto.setContentType(file.getContentType());
- dto.setBucketFilename(res.getBucketFileName());
- dto.setOriginalFilename(res.getOriginalName());
- dto.setByteSize(file.getSize());
- dto.setResourceKey(IdUtils.simpleUUID());
- dto.setBucketName(bucketName);
- dto.setUrl(minioUtils.getPreviewUrl(res.getBucketFileName(), bucketName, false));
- dto.setDownloadUrl(minioUtils.getDownloadUrl(res.getBucketFileName(), bucketName));
-
- if (type != null) {
- dto.setType(type);
- }
-
- return dto;
+ private StorageBlob findStorageBlob(String fileName) {
+ return storageBlobMapper.selectOne(new LambdaQueryWrapper<StorageBlob>()
+ .eq(StorageBlob::getUidFilename, fileName)
+ .last("limit 1"));
}
- private void validateFileExtension(MultipartFile file) throws InvalidExtensionException {
- String filename = file.getOriginalFilename();
- String extension = FilenameUtils.getExtension(filename).toLowerCase();
- List<String> allowedExtensions = Arrays.asList(
- // 鍥剧墖
- "jpg", "jpeg", "png", "gif", "bmp", "webp", "tiff", "ico", "svg",
-
- // 鏂囨。
- "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "rtf", "md", "csv", "odt",
-
- // 瑙嗛
- "mp4", "mov", "avi", "wmv", "flv", "mkv", "webm", "mpeg", "3gp", "MOV",
-
- // 闊抽
- "mp3", "wav", "ogg", "aac", "flac", "m4a", "wma", "amr",
-
- // 鍘嬬缉鍖�
- "zip", "rar", "7z", "tar", "gz", "bz2", "xz",
-
- // 缂栫▼浠g爜鏂囦欢
- "java", "py", "js", "ts", "html", "css", "cpp", "c", "cs", "json", "xml", "sql", "yaml", "yml", "sh", "bat",
-
- // 瀹夎绋嬪簭 & 浜岃繘鍒�
- "exe", "apk", "dmg", "msi", "bin", "iso",
-
- // 璁捐绫�
- "psd", "ai", "xd", "sketch", "fig"
- );
-
- if (!allowedExtensions.contains(extension)) {
- throw new BaseException("鏂囦欢绫诲瀷涓嶈鍏佽锛�" + extension);
+ 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 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());
- if(CollectionUtils.isEmpty(ids)){
- return 0;
+ public String getDownloadFileName(String fileName) {
+ StorageBlob storageBlob = findStorageBlob(fileName);
+ if (storageBlob == null || !StringUtils.hasText(storageBlob.getOriginalFilename())) {
+ return fileName;
}
- 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());
- }
- }
-
- if (!ids.isEmpty()) {
- return storageBlobMapper.delete(new QueryWrapper<StorageBlob>().lambda().in(StorageBlob::getId, ids));
- }
-
- return 0;
+ return storageBlob.getOriginalFilename();
}
}
--
Gitblit v1.9.3