Browse Source

fix: 个人库删除文件夹及模型时删除oss对应文件释放空间

liaoyitao 2 months ago
parent
commit
7af895e9ed

+ 1 - 0
src/main/java/com/lqkj/link/module/zone/repository/ModelInfoRepository.java

@@ -40,4 +40,5 @@ public interface ModelInfoRepository extends JpaRepository<ModelInfo, Integer> {
40
     )
40
     )
41
     void deleteAllByTemplateIds(@Param("templateIds") List<Integer> templateIds);
41
     void deleteAllByTemplateIds(@Param("templateIds") List<Integer> templateIds);
42
 
42
 
43
+    List<ModelInfo> findByCategoryIdIn(List<Integer> categoryId);
43
 }
44
 }

+ 12 - 0
src/main/java/com/lqkj/link/module/zone/service/ResourceService.java

@@ -122,16 +122,28 @@ public class ResourceService {
122
     }
122
     }
123
 
123
 
124
     public void deleteModel(Integer modelId) {
124
     public void deleteModel(Integer modelId) {
125
+        ModelInfo modelInfo = infoRepository.findById(modelId).get();
125
         infoRepository.deleteById(modelId);
126
         infoRepository.deleteById(modelId);
127
+        aliOSSUtils.deleteModel(modelInfo.getOriginalPath());
126
     }
128
     }
127
 
129
 
128
     @Transactional
130
     @Transactional
129
     public void deleteCategory(List<Integer> categoryId) {
131
     public void deleteCategory(List<Integer> categoryId) {
132
+        deleteOssFiles(categoryId);
130
         categoryRepository.deleteAllByIdInBatch(categoryId);
133
         categoryRepository.deleteAllByIdInBatch(categoryId);
131
         // 更新用户资源刷新状态
134
         // 更新用户资源刷新状态
132
         userInfoRepository.updateRefreshStatus();
135
         userInfoRepository.updateRefreshStatus();
133
     }
136
     }
134
 
137
 
138
+    /**
139
+     * 删除oss文件
140
+     *
141
+     * @param categoryId
142
+     */
143
+    private void deleteOssFiles(List<Integer> categoryId) {
144
+        infoRepository.findByCategoryIdIn(categoryId).forEach(modelInfo -> aliOSSUtils.deleteModel(modelInfo.getOriginalPath()));
145
+    }
146
+
135
     public List<Map<String, Object>> resourceCategory(String userCode) {
147
     public List<Map<String, Object>> resourceCategory(String userCode) {
136
         return categoryRepository.queryWithUserCode(userCode);
148
         return categoryRepository.queryWithUserCode(userCode);
137
     }
149
     }

+ 5 - 0
src/main/java/com/lqkj/link/util/AliOSSUtils.java

@@ -123,5 +123,10 @@ public class AliOSSUtils {
123
         return size;
123
         return size;
124
     }
124
     }
125
 
125
 
126
+    public void deleteModel(String path) {
127
+        OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
128
+        String prefix = aliProperties.getOsspath().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getOsspath().split("//")[1] + "/";
129
+        ossClient.deleteObject(aliProperties.getBucketName(), path.replace(prefix, ""));
130
+    }
126
 
131
 
127
 }
132
 }