From 34320634733b77a773af6836a2e3082092068ec9 Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期六, 25 四月 2026 15:09:50 +0800
Subject: [PATCH] 增加永久文件接口

---
 src/main/java/com/ruoyi/basic/utils/FileUtil.java |  166 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 135 insertions(+), 31 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/utils/FileUtil.java b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
index bf0b9b2..97397f3 100644
--- a/src/main/java/com/ruoyi/basic/utils/FileUtil.java
+++ b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
@@ -52,9 +52,6 @@
      * @param storageBlobDTOS 鏂囦欢淇℃伅
      */
     public void saveStorageAttachment(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, List<StorageBlobDTO> storageBlobDTOS) {
-        if (CollectionUtils.isEmpty(storageBlobDTOS)) {
-            throw new RuntimeException("鏂囦欢淇℃伅涓嶈兘涓虹┖");
-        }
         if (application == null) {
             throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
         }
@@ -66,6 +63,9 @@
         }
         // 鍒犻櫎鏃ч檮浠朵俊鎭�
         deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageBlobDTOS)) {
+            return;
+        }
         List<StorageAttachment> storageAttachments = new ArrayList<>();
         for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) {
             StorageAttachment storageAttachment = new StorageAttachment();
@@ -104,10 +104,10 @@
      *
      * @param application 鏂囦欢鐢ㄩ��
      * @param recordType  鍏宠仈璁板綍绫诲瀷
-     * @param recordId    鍏宠仈璁板綍id
+     * @param recordIds    鍏宠仈璁板綍id
      */
-    public void deleteStorageBlobsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
-        if (recordId == null || recordId <= 0) {
+    public void deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(ApplicationTypeEnum application, RecordTypeEnum recordType, List<Long> recordIds) {
+        if (recordIds == null || recordIds.isEmpty()) {
             throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
         }
         if (application == null) {
@@ -118,7 +118,7 @@
         }
         List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
                 .eq(StorageAttachment::getRecordType, recordType.getType())
-                .eq(StorageAttachment::getRecordId, recordId)
+                .in(StorageAttachment::getRecordId, recordIds)
                 .eq(StorageAttachment::getApplication, application.getType()));
         if (CollectionUtils.isNotEmpty(storageAttachments)) {
             List<Long> storageAttachmentIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId)
@@ -137,6 +137,13 @@
         storageAttachmentMapper.deleteByIds(storageAttachmentIds);
     }
 
+    /**
+     * 鍒犻櫎鏂囦欢鍏宠仈淇℃伅
+     *
+     * @param application 鏂囦欢鐢ㄩ��
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordId    鍏宠仈璁板綍id
+     */
     public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
         if (recordId == null || recordId <= 0) {
             throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
@@ -147,10 +154,34 @@
         if (recordType == null) {
             throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
         }
-        deleteStorageBlobsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(application, recordType, Arrays.asList(recordId));
         storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>()
                 .eq(StorageAttachment::getRecordType, recordType.getType())
                 .eq(StorageAttachment::getRecordId, recordId)
+                .eq(StorageAttachment::getApplication, application.getType()));
+    }
+
+    /**
+     * 鎵归噺鍒犻櫎鏂囦欢鍏宠仈淇℃伅 attachment
+     *
+     * @param application 鏂囦欢鐢ㄩ��
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordIds   鍏宠仈璁板綍id
+     */
+    public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordIds(ApplicationTypeEnum application, RecordTypeEnum recordType, List<Long> recordIds) {
+        if (recordIds == null || recordIds.isEmpty()) {
+            throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+        }
+        if (application == null) {
+            throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
+        }
+        if (recordType == null) {
+            throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
+        }
+        deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(application, recordType, recordIds);
+        storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>()
+                .eq(StorageAttachment::getRecordType, recordType.getType())
+                .in(StorageAttachment::getRecordId, recordIds)
                 .eq(StorageAttachment::getApplication, application.getType()));
     }
 
@@ -193,7 +224,7 @@
      *
      * @param storageAttachmentIds 鏂囦欢id
      */
-    public List<StorageBlobVO> getStorageBlobDTOsByStorageAttachmentIds(List<Long> storageAttachmentIds) {
+    public List<StorageBlobVO> getStorageBlobVOsByStorageAttachmentIds(List<Long> storageAttachmentIds) {
         List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds);
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
@@ -218,7 +249,7 @@
      * @param recordType  鍏宠仈璁板綍绫诲瀷
      * @param recordId    鍏宠仈璁板綍id
      */
-    public List<StorageBlobVO> getStorageBlobDTOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
+    public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
         List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
@@ -244,7 +275,7 @@
      * @param recordId    鍏宠仈璁板綍id
      * @param expired     杩囨湡鏃堕棿
      */
-    public List<StorageBlobVO> getStorageBlobDTOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
+    public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
         List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
@@ -268,7 +299,7 @@
      * @param storageAttachmentIds 鏂囦欢id
      * @param expired              杩囨湡鏃堕棿
      */
-    public List<StorageBlobVO> getStorageBlobDTOsByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) {
+    public List<StorageBlobVO> getStorageBlobVOsByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) {
         List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds);
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
@@ -300,7 +331,7 @@
         for (StorageAttachment storageAttachment : storageAttachments) {
             StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO();
             BeanUtils.copyProperties(storageAttachment, storageAttachmentVO);
-            List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()));
+            List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()));
             if (CollectionUtils.isEmpty(storageBlobVOS)) {
                 storageAttachmentVO.setStorageBlobVOS(new ArrayList<>());
             } else {
@@ -326,7 +357,7 @@
         for (StorageAttachment storageAttachment : storageAttachments) {
             StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO();
             BeanUtils.copyProperties(storageAttachment, storageAttachmentVO);
-            List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired);
+            List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired);
             if (CollectionUtils.isEmpty(storageBlobVOS)) {
                 storageAttachmentVO.setStorageBlobVOS(new ArrayList<>());
             } else {
@@ -353,7 +384,7 @@
         for (StorageAttachment storageAttachment : storageAttachments) {
             StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO();
             BeanUtils.copyProperties(storageAttachment, storageAttachmentVO);
-            List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()));
+            List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()));
             if (CollectionUtils.isEmpty(storageBlobVOS)) {
                 storageAttachmentVO.setStorageBlobVOS(new ArrayList<>());
             } else {
@@ -381,7 +412,7 @@
         for (StorageAttachment storageAttachment : storageAttachments) {
             StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO();
             BeanUtils.copyProperties(storageAttachment, storageAttachmentVO);
-            List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired);
+            List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired);
             if (CollectionUtils.isEmpty(storageBlobVOS)) {
                 storageAttachmentVO.setStorageBlobVOS(new ArrayList<>());
             } else {
@@ -399,7 +430,7 @@
      */
     public List<String> getFilePreviewURLByStorageAttachmentIds(List<Long> storageAttachmentIds) {
         List<String> res = new ArrayList<>();
-        List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(storageAttachmentIds);
+        List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds);
         for (StorageBlobVO storageBlobVO : storageBlobVOS) {
             res.add(buildSignedPreviewUrl(storageBlobVO));
         }
@@ -414,7 +445,7 @@
      */
     public List<String> getFilePreviewURLByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) {
         List<String> res = new ArrayList<>();
-        List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(storageAttachmentIds);
+        List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds);
         for (StorageBlobVO storageBlobVO : storageBlobVOS) {
             res.add(buildSignedUrl(storageBlobVO, "/preview/", expired));
         }
@@ -428,7 +459,7 @@
      */
     public List<String> getFileDownloadURLByStorageAttachmentIds(List<Long> storageAttachmentIds) {
         List<String> res = new ArrayList<>();
-        List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(storageAttachmentIds);
+        List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds);
         for (StorageBlobVO storageBlobVO : storageBlobVOS) {
             res.add(buildSignedDownloadUrl(storageBlobVO));
         }
@@ -443,11 +474,73 @@
      */
     public List<String> getFileDownloadURLByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) {
         List<String> res = new ArrayList<>();
-        List<StorageBlobVO> storageBlobVOS = getStorageBlobDTOsByStorageAttachmentIds(storageAttachmentIds);
+        List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds);
         for (StorageBlobVO storageBlobVO : storageBlobVOS) {
             res.add(buildSignedUrl(storageBlobVO, "/download/", expired));
         }
         return res;
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢棰勮鍦板潃
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     */
+    public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()));
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢棰勮鍦板潃瀛樺湪杩囨湡鏃堕棿
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     * @param expired     杩囨湡鏃堕棿锛堝垎閽燂級
+     */
+    public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired);
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢涓嬭浇鍦板潃
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     */
+    public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()));
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢涓嬭浇鍦板潃瀛樺湪杩囨湡鏃堕棿
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     * @param expired     杩囨湡鏃堕棿锛堝垎閽燂級
+     */
+    public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired);
     }
 
     public String buildSignedPreviewUrl(StorageBlobVO storageBlob) {
@@ -472,8 +565,28 @@
         if (storageBlob == null || !StringUtils.hasText(storageBlob.getUidFilename())) {
             throw new IllegalArgumentException("鏂囦欢淇℃伅涓嶅畬鏁�");
         }
+        String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/');
+        String prefix = properties.getUrlPrefix().startsWith("/") ? properties.getUrlPrefix() : "/" + properties.getUrlPrefix();
+        String normalizedActionPath = StringUtils.hasText(actionPath) ? actionPath : "/preview/";
+        if (!normalizedActionPath.startsWith("/")) {
+            normalizedActionPath = "/" + normalizedActionPath;
+        }
+        if (!normalizedActionPath.endsWith("/")) {
+            normalizedActionPath = normalizedActionPath + "/";
+        }
+        String baseUrl = domain + prefix + normalizedActionPath + storageBlob.getUidFilename();
+
+        // -1 琛ㄧず姘镐箙鏈夋晥锛屼笉鐢熸垚 token锛屾敼涓� publicKey 缁勫悎鏍¢獙
+        if (expired != null && BigDecimal.valueOf(-1L).compareTo(expired) == 0) {
+            if (!StringUtils.hasText(storageBlob.getResourceKey())) {
+                throw new IllegalArgumentException("鍏紑閾炬帴缂哄皯publicKey");
+            }
+            return baseUrl + "?publicKey=" + storageBlob.getResourceKey();
+        }
+
         long now = System.currentTimeMillis();
-        long expiredMillis = expired.multiply(new BigDecimal("60000")).longValue();
+        BigDecimal expiredValue = expired == null ? new BigDecimal("120") : expired;
+        long expiredMillis = expiredValue.multiply(new BigDecimal("60000")).longValue();
         if (expiredMillis <= 0L) {
             expiredMillis = 2L * 60L * 60L * 1000L;
         }
@@ -490,16 +603,7 @@
                 .signWith(key)            // 閲嶇偣锛氫紶鍏ヤ笂闈㈢敓鎴愮殑 key 瀵硅薄锛岃�屼笉鏄� String
                 .compact();
         cacheTokenUsage(token, expiredMillis);
-        String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/');
-        String prefix = properties.getUrlPrefix().startsWith("/") ? properties.getUrlPrefix() : "/" + properties.getUrlPrefix();
-        String normalizedActionPath = StringUtils.hasText(actionPath) ? actionPath : "/preview/";
-        if (!normalizedActionPath.startsWith("/")) {
-            normalizedActionPath = "/" + normalizedActionPath;
-        }
-        if (!normalizedActionPath.endsWith("/")) {
-            normalizedActionPath = normalizedActionPath + "/";
-        }
-        return domain + prefix + normalizedActionPath + storageBlob.getUidFilename() + "?token=" + token;
+        return baseUrl + "?token=" + token;
     }
 
     private void cacheTokenUsage(String token, long expiredMillis) {

--
Gitblit v1.9.3