فهرست منبع

feat:
文件管理新增过期字段

18339543638 2 سال پیش
والد
کامیت
82d64392f5

+ 13 - 1
tr-modules-api/tr-module-system-api/src/main/java/cn/tr/module/api/sys/storage/SysStorageApi.java

@@ -2,6 +2,7 @@ package cn.tr.module.api.sys.storage;
 
 import java.util.Collection;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @ClassName : SysStorageApi
@@ -17,7 +18,18 @@ public interface SysStorageApi {
      * @param filename 上传文件名称
      * @return  文件id
      */
-    String upload(byte[] content,String filename) throws Exception;
+    default String upload(byte[] content,String filename) throws Exception{
+        return upload(content, filename, null);
+    };
+
+    /**
+     * 上传文件
+     * @param content 文件内容
+     * @param filename 上传文件名称
+     * @param millis 过期时间(毫秒)
+     * @return  文件id
+     */
+    String upload(byte[] content, String filename,Long millis) throws Exception;
 
     /**
      * 删除文件

+ 1 - 2
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/controller/StorageFileController.java

@@ -1,6 +1,5 @@
 package cn.tr.module.sys.storage.controller;
 
-import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.hutool.core.io.IoUtil;
 import cn.tr.core.pojo.CommonResult;
 import cn.tr.module.sys.storage.dto.SysStorageRecordDTO;
@@ -32,7 +31,7 @@ public class StorageFileController {
     public CommonResult<SysStorageRecordDTO> uploadFile(@RequestParam MultipartFile file,
                                                         @RequestParam(required = false) String configId,
                                                         @RequestParam(required = false) String cateId) throws Exception {
-        return CommonResult.success(fileService.upload(configId,cateId,file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
+        return CommonResult.success(fileService.upload(configId,cateId,file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream()),null));
     }
 
     @ApiOperationSupport(author = "lf",order = 2)

+ 4 - 0
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/dto/SysStorageRecordDTO.java

@@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 
 import javax.validation.constraints.*;
+import java.util.Date;
 
 /**
  * 存储记录传输对象
@@ -50,4 +51,7 @@ public class SysStorageRecordDTO extends BaseDTO  {
 
     @ApiModelProperty(value = "存储配置名称", position = 10,readOnly = true)
     private String configName;
+
+    @ApiModelProperty(value = "过期时间", position = 9)
+    private Date expireTime;
 }

+ 42 - 0
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/job/StorageJob.java

@@ -0,0 +1,42 @@
+package cn.tr.module.sys.storage.job;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.tr.core.utils.JsonUtils;
+import cn.tr.module.api.sys.storage.SysStorageApi;
+import cn.tr.module.sys.storage.po.SysStorageRecordPO;
+import cn.tr.module.sys.storage.repository.SysStorageRecordRepository;
+import cn.tr.module.sys.storage.service.IStorageFileService;
+import cn.tr.module.sys.storage.service.ISysStorageRecordService;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @ClassName : StorageJob
+ * @Description :
+ * @Author : LF
+ * @Date: 2023年09月26日
+ */
+@Component
+@AllArgsConstructor
+@Slf4j
+public class StorageJob {
+    private final SysStorageRecordRepository sysStorageRecordRepository;
+
+    private final IStorageFileService storageFileService;
+    public void delExpireFile(){
+        Date now = new Date();
+        List<SysStorageRecordPO> expireStorageRecord = sysStorageRecordRepository.selectList(new LambdaQueryWrapper<SysStorageRecordPO>()
+                .le(SysStorageRecordPO::getExpireTime, now));
+        if(CollectionUtil.isNotEmpty(expireStorageRecord)){
+            List<String> expireIds = expireStorageRecord.stream().map(SysStorageRecordPO::getId).collect(Collectors.toList());
+            storageFileService.remove(expireIds);
+            log.info("删除过期文件成功,{}", JsonUtils.toJsonString(expireIds));
+        }
+    }
+}

+ 5 - 0
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/po/SysStorageRecordPO.java

@@ -8,6 +8,8 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.ToString;
 
+import java.util.Date;
+
 /**
  * 存储记录实体
  *
@@ -53,4 +55,7 @@ public class SysStorageRecordPO extends TenantPO {
     @ApiModelProperty(value = "所属目录id", position = 9)
     private String cateId;
 
+    @ApiModelProperty(value = "过期时间",position = 10)
+    private Date expireTime;
+
 }

+ 3 - 2
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/provider/SysStorageApiProvider.java

@@ -20,9 +20,10 @@ import java.util.Map;
 @AllArgsConstructor
 public class SysStorageApiProvider implements SysStorageApi {
     private final IStorageFileService storageFileService;
+
     @Override
-    public String upload(byte[] content, String filename) throws Exception {
-        return  storageFileService.upload(null,null,filename,content).getId();
+    public String upload(byte[] content, String filename, Long millis) throws Exception {
+        return  storageFileService.upload(null,null,filename,content,millis).getId();
     }
 
     @Override

+ 2 - 1
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/service/IStorageFileService.java

@@ -28,9 +28,10 @@ public interface IStorageFileService {
      * @param cateId 文件所属目录id
      * @param filename 文件名称
      * @param content 文件内容
+     * @param millis 过期时间
      * @return
      */
-    SysStorageRecordDTO upload(String configId, String cateId, String filename, byte[] content) throws Exception;
+    SysStorageRecordDTO upload(String configId, String cateId, String filename, byte[] content,Long millis) throws Exception;
 
     /**
      * 删除文件

+ 7 - 1
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/storage/service/impl/StorageFileServiceImpl.java

@@ -32,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.PostConstruct;
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -78,7 +79,7 @@ public class StorageFileServiceImpl implements IStorageFileService {
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public SysStorageRecordDTO upload(String configId,String cateId,String filename, byte[] content) throws Exception {
+    public SysStorageRecordDTO upload(String configId,String cateId,String filename, byte[] content, Long millis) throws Exception {
         if(StrUtil.isEmpty(configId)){
             configId=defaultConfig.getId();
         }
@@ -87,6 +88,10 @@ public class StorageFileServiceImpl implements IStorageFileService {
         String suffix = FileUtil.extName(filename);
         fileClient.upload(content,bizName);
         String downUrl = fileClient.downUrl(bizName);
+        Date expireTime=null;
+        if(millis!=null){
+            expireTime=new Date(System.currentTimeMillis() + millis);
+        }
         SysStorageRecordDTO record = SysStorageRecordDTO.builder()
                 .realName(filename)
                 .bizName(bizName)
@@ -95,6 +100,7 @@ public class StorageFileServiceImpl implements IStorageFileService {
                 .cateId(cateId)
                 .configId(configId)
                 .size(content.length/1024)
+                .expireTime(expireTime)
                 .build();
         String recordId = storageRecordService.insertSysStorageRecordReturnId(record);
         record.setId(recordId);

+ 0 - 6
tr-test/src/main/resources/application.yml

@@ -31,12 +31,6 @@ spring:
     locations:
       - classpath:db/migration
     sql-migration-suffixes: .sql
-    validate-on-migrate: true
-    baseline-on-migrate: true
-    baseline-version: 0
-    placeholder-replacement: false
-
-
 
 tr:
   storage:

+ 1 - 0
tr-test/src/main/resources/db/migration/V20230926__AddStorage.sql

@@ -0,0 +1 @@
+alter table sys_storage_record add expire_time datetime after cate_id