From 9f8ea84ddf839ace5e82512f33d63831435d058f Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期四, 07 五月 2026 11:53:46 +0800
Subject: [PATCH] 删除客户原有字段
---
src/main/java/com/ruoyi/basic/service/impl/StorageBlobServiceImpl.java | 198 +++++++++++++++++++++++++++++++++----------------
1 files changed, 133 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..9ad80ae 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,171 @@
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.math.BigDecimal;
+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, Boolean isPublic) {
+ 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, isPublic);
+ 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 = properties.getJwtSecret();
- 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);
+ }
+
+ @Override
+ public File getPublicFile(String fileName, String publicKey) {
+ if (!StringUtils.hasText(fileName)) {
+ throw new IllegalArgumentException("鏂囦欢鍚嶄笉鑳戒负绌�");
+ }
+ if (!StringUtils.hasText(publicKey)) {
+ throw new IllegalArgumentException("publicKey涓嶈兘涓虹┖");
+ }
+ StorageBlob storageBlob = storageBlobMapper.selectOne(new LambdaQueryWrapper<StorageBlob>()
+ .eq(StorageBlob::getUidFilename, fileName)
+ .eq(StorageBlob::getResourceKey, publicKey)
+ .last("limit 1"));
+ if (storageBlob == null) {
+ throw new IllegalArgumentException("鍏紑鏂囦欢涓嶅瓨鍦ㄦ垨publicKey涓嶅尮閰�");
+ }
+ String path = 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, Boolean isPublic) {
+ 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);
+ if (isPublic) {
+ storageBlob.setPreviewURL(fileUtil.buildSignedUrl(storageBlob, "/preview/", BigDecimal.valueOf(-1)));
+ storageBlob.setDownloadURL(fileUtil.buildSignedUrl(storageBlob, "/download/", BigDecimal.valueOf(-1)));
+ } else {
+ 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