From 5033c375a3ee6a63bcb600d5b9f4b8d549d089e2 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期四, 23 四月 2026 19:09:09 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_pro' into dev_New_pro
---
src/main/java/com/ruoyi/basic/utils/FileUtil.java | 175 +++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 105 insertions(+), 70 deletions(-)
diff --git a/src/main/java/com/ruoyi/basic/utils/FileUtil.java b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
index 606228c..19ac77c 100644
--- a/src/main/java/com/ruoyi/basic/utils/FileUtil.java
+++ b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
@@ -2,23 +2,28 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.ruoyi.basic.constant.ApplicationType;
-import com.ruoyi.basic.constant.RecordType;
import com.ruoyi.basic.dto.StorageAttachmentDTO;
import com.ruoyi.basic.dto.StorageBlobVO;
+import com.ruoyi.basic.enums.ApplicationTypeEnum;
+import com.ruoyi.basic.enums.RecordTypeEnum;
import com.ruoyi.basic.mapper.StorageAttachmentMapper;
import com.ruoyi.basic.mapper.StorageBlobMapper;
import com.ruoyi.basic.pojo.StorageAttachment;
+import com.ruoyi.basic.pojo.StorageBlob;
import com.ruoyi.common.config.FileProperties;
import io.jsonwebtoken.Jwts;
-import io.jsonwebtoken.SignatureAlgorithm;
+import io.jsonwebtoken.security.Keys;
import lombok.RequiredArgsConstructor;
+import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
+import javax.crypto.SecretKey;
+import java.io.File;
import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -30,8 +35,8 @@
public class FileUtil {
private final FileProperties properties;
private final StorageAttachmentMapper storageAttachmentMapper;
- private final StorageBlobMapper storageBlobMapper;
private final StringRedisTemplate stringRedisTemplate;
+ private final StorageBlobMapper storageBlobMapper;
private static final String TOKEN_USAGE_KEY_PREFIX = "file:token:usage:";
private static final DateTimeFormatter YEAR_PATH_FORMATTER = DateTimeFormatter.ofPattern("yyyy");
@@ -45,14 +50,14 @@
* @param recordId 鍏宠仈璁板綍id
* @param storageBlobVOS 鏂囦欢淇℃伅
*/
- public void saveStorageAttachment(ApplicationType application, RecordType recordType, Long recordId, List<StorageBlobVO> storageBlobVOS) {
+ public void saveStorageAttachment(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, List<StorageBlobVO> storageBlobVOS) {
if (CollectionUtils.isEmpty(storageBlobVOS)) {
throw new RuntimeException("鏂囦欢淇℃伅涓嶈兘涓虹┖");
}
- if (!application.isValid()) {
+ if (application == null) {
throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
}
- if (!recordType.isValid()) {
+ if (recordType == null) {
throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
}
if (recordId == null || recordId <= 0) {
@@ -69,8 +74,7 @@
storageAttachment.setStorageBlobId(storageBlobVO.getId());
storageAttachment.setDeleted(0L);
}
- // todo fileChange
-// storageAttachmentMapper.insert(storageAttachments);
+ storageAttachmentMapper.insert(storageAttachments);
}
/**
@@ -79,8 +83,7 @@
* @param storageBlobIds 鏂囦欢id
*/
public void deleteStorageBlobs(List<Long> storageBlobIds) {
- // todo fileChange
-// storageBlobMapper.deleteByIds(storageBlobIds);
+ storageBlobMapper.deleteByIds(storageBlobIds);
}
/**
@@ -89,10 +92,9 @@
* @param storageAttachmentIds 鏂囦欢id
*/
public void deleteStorageBlobsByStorageAttachmentIds(List<Long> storageAttachmentIds) {
- // todo fileChange
-// List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectByIds(storageAttachmentIds);
-// List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
-// deleteStorageBlobs(storageBlobIds);
+ List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectByIds(storageAttachmentIds);
+ List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
+ deleteStorageBlobs(storageBlobIds);
}
/**
@@ -102,15 +104,15 @@
* @param recordType 鍏宠仈璁板綍绫诲瀷
* @param recordId 鍏宠仈璁板綍id
*/
- public void deleteStorageBlobsByApplicationAndRecordTypeAndRecordId(ApplicationType application, RecordType recordType, Long recordId) {
- if (!application.isValid()) {
- throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
- }
- if (!recordType.isValid()) {
- throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
- }
+ public void deleteStorageBlobsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
if (recordId == null || recordId <= 0) {
throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+ }
+ if (application == null) {
+ throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
+ }
+ if (recordType == null) {
+ throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
}
List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
.eq(StorageAttachment::getRecordType, recordType.getType())
@@ -130,19 +132,18 @@
*/
public void deleteStorageAttachmentsByStorageAttachmentIds(List<Long> storageAttachmentIds) {
deleteStorageBlobsByStorageAttachmentIds(storageAttachmentIds);
- // todo fileChange
-// storageAttachmentMapper.deleteByIds(storageAttachmentIds);
+ storageAttachmentMapper.deleteByIds(storageAttachmentIds);
}
- public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationType application, RecordType recordType, Long recordId) {
- if (!application.isValid()) {
- throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
- }
- if (!recordType.isValid()) {
- throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
- }
+ public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
if (recordId == null || recordId <= 0) {
throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+ }
+ if (application == null) {
+ throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
+ }
+ if (recordType == null) {
+ throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
}
deleteStorageBlobsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>()
@@ -160,9 +161,7 @@
if (CollectionUtils.isEmpty(storageAttachmentIds)) {
throw new RuntimeException("鏂囦欢id涓嶈兘涓虹┖");
}
- // todo fileChange
-// return storageAttachmentMapper.selectByIds(storageAttachmentIds);
- return new ArrayList<>();
+ return storageAttachmentMapper.selectByIds(storageAttachmentIds);
}
/**
@@ -172,15 +171,14 @@
* @param recordType 鍏宠仈璁板綍绫诲瀷
* @param recordId 鍏宠仈璁板綍id
*/
- public List<StorageAttachment> getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationType application, RecordType recordType, Long recordId) {
- if (!application.isValid()) {
- throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
- }
- if (!recordType.isValid()) {
- throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
- }
+ public List<StorageAttachment> getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
if (recordId == null || recordId <= 0) {
throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+ } if (application == null) {
+ throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
+ }
+ if (recordType == null) {
+ throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
}
return storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
.eq(StorageAttachment::getRecordType, recordType.getType())
@@ -199,17 +197,16 @@
return null;
}
List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
- // todo fileChange
-// List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
-// List<StorageBlobDTO> storageBlobDTOS = new ArrayList<>();
-// for (StorageBlob storageBlob : storageBlobs) {
-// StorageBlobDTO storageBlobDTO = new StorageBlobDTO();
-// BeanUtils.copyProperties(storageBlob, storageBlobDTO);
-// storageBlobDTO.setPreviewURL(buildSignedPreviewUrl(storageBlobDTO));
-// storageBlobDTO.setDownloadURL(buildSignedDownloadUrl(storageBlobDTO));
-// storageBlobDTOS.add(storageBlobDTO);
-// }
- return new ArrayList<>();
+ List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
+ List<StorageBlobVO> storageBlobDTOS = new ArrayList<>();
+ for (StorageBlob storageBlob : storageBlobs) {
+ StorageBlobVO storageBlobVO = new StorageBlobVO();
+ BeanUtils.copyProperties(storageBlob, storageBlobVO);
+ storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO));
+ storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO));
+ storageBlobDTOS.add(storageBlobVO);
+ }
+ return storageBlobDTOS;
}
/**
@@ -224,18 +221,16 @@
return null;
}
List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
- // todo fileChange
-// List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
-// List<StorageBlobDTO> storageBlobDTOS = new ArrayList<>();
-// for (StorageBlob storageBlob : storageBlobs) {
-// StorageBlobDTO storageBlobDTO = new StorageBlobDTO();
-// BeanUtils.copyProperties(storageBlob, storageBlobDTO);
-// storageBlobDTO.setPreviewURL(buildSignedUrl(storageBlobDTO, "/preview/", expired));
-// storageBlobDTO.setDownloadURL(buildSignedUrl(storageBlobDTO, "/download/", expired));
-// storageBlobDTOS.add(storageBlobDTO);
-// }
-// return storageBlobDTOS;
- return new ArrayList<>();
+ List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
+ List<StorageBlobVO> storageBlobDTOS = new ArrayList<>();
+ for (StorageBlob storageBlob : storageBlobs) {
+ StorageBlobVO storageBlobVO = new StorageBlobVO();
+ BeanUtils.copyProperties(storageBlob, storageBlobVO);
+ storageBlobVO.setPreviewURL(buildSignedUrl(storageBlobVO, "/preview/", expired));
+ storageBlobVO.setDownloadURL(buildSignedUrl(storageBlobVO, "/download/", expired));
+ storageBlobDTOS.add(storageBlobVO);
+ }
+ return storageBlobDTOS;
}
/**
@@ -296,7 +291,7 @@
* @param recordType 璁板綍绫诲瀷
* @param recordId 璁板綍id
*/
- public List<StorageAttachmentDTO> getStorageAttachmentDTOsByApplicationAndRecordTypeAndRecordId(ApplicationType application, RecordType recordType, Long recordId) {
+ public List<StorageAttachmentDTO> getStorageAttachmentDTOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
if (CollectionUtils.isEmpty(storageAttachments)) {
return new ArrayList<>();
@@ -324,7 +319,7 @@
* @param recordId 璁板綍id
* @param expired 杩囨湡鏃堕棿
*/
- public List<StorageAttachmentDTO> getStorageAttachmentDTOsByApplicationAndRecordTypeAndRecordId(ApplicationType application, RecordType recordType, Long recordId, BigDecimal expired) {
+ public List<StorageAttachmentDTO> getStorageAttachmentDTOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
if (CollectionUtils.isEmpty(storageAttachments)) {
return new ArrayList<>();
@@ -431,13 +426,15 @@
}
Date issuedAt = new Date(now);
Date expiration = new Date(now + expiredMillis);
+ SecretKey key = Keys.hmacShaKeyFor(properties.getJwtSecret().getBytes(StandardCharsets.UTF_8));
+
String token = Jwts.builder()
- .setSubject(storageBlob.getUidFilename())
- .setIssuedAt(issuedAt)
- .setExpiration(expiration)
+ .subject(storageBlob.getUidFilename())
+ .issuedAt(issuedAt) // 鏂扮増寤鸿鐩存帴璋冪敤 .issuedAt()
+ .expiration(expiration) // 鏂扮増寤鸿鐩存帴璋冪敤 .expiration()
.claim("path", storageBlob.getPath())
.claim("resourceKey", storageBlob.getResourceKey())
- .signWith(SignatureAlgorithm.HS256, properties.getJwtSecret())
+ .signWith(key) // 閲嶇偣锛氫紶鍏ヤ笂闈㈢敓鎴愮殑 key 瀵硅薄锛岃�屼笉鏄� String
.compact();
cacheTokenUsage(token, expiredMillis);
String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/');
@@ -491,4 +488,42 @@
return properties.getUseLimit() == null || properties.getUseLimit() <= 0 ? 10 : properties.getUseLimit();
}
+ /**
+ * 鍘嬬缉鏂囦欢
+ *
+ * @param file 鏂囦欢
+ * @return 鍘嬬缉鍚庣殑鏂囦欢
+ */
+ public File compressFile(File file) {
+ if (properties.getCompress() && isImage(file.getName()) && (file.length() > properties.getNeedCompressSize().toBytes())) {
+ try {
+ // 鍒涘缓涓�涓复鏃舵枃浠跺瓨鏀惧帇缂╁悗鐨勫浘鐗囷紝閬垮厤鐮村潖鍘熷浘
+ File compressedFile = new File(file.getParent(), "thumb_" + file.getName());
+
+ // 1. 濡傛灉宸茬粡瀛樺湪鍘嬬缉杩囩殑鏂囦欢锛岀洿鎺ヨ繑鍥烇紝涓嶅啀娑堣�� CPU 鍘嬬缉
+ if (compressedFile.exists()) {
+ return compressedFile;
+ }
+
+ // 浣跨敤 Thumbnailator 杩涜鍘嬬缉
+ Thumbnails.of(file)
+ .scale(1.0f) // 淇濇寔鍘熷昂瀵�
+ .outputQuality(properties.getCompressQuality()) // 鏍稿績锛氳缃敾璐� (0.0~1.0)
+ .toFile(compressedFile);
+
+ return compressedFile;
+ } catch (Exception e) {
+ // 濡傛灉鍘嬬缉澶辫触锛岄檷绾у鐞嗭細杩斿洖鍘熷浘
+ return file;
+ }
+ }
+ return file;
+ }
+ // 绠�鍗曠殑鍚庣紑鍒ゆ柇
+ private boolean isImage(String fileName) {
+ String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
+ return "jpg".equals(ext) || "jpeg".equals(ext) || "png".equals(ext);
+ }
+
+
}
--
Gitblit v1.9.3