Commit d3b2f7e0a2a604143e1dbb8e32c09c8b7fe84cca

Authored by 王明元
1 parent b8c3b5a7

2022年7月27日10:31:32 删除过期主题封面图

fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/app/OtherController.java
... ... @@ -5,6 +5,7 @@ import cn.fw.common.web.auth.LoginAuthBean;
5 5 import cn.fw.common.web.auth.annotation.CurrentUser;
6 6 import cn.fw.dalaran.domain.param.LiveCheckParams;
7 7 import cn.fw.dalaran.domain.vo.LiveCheckVo;
  8 +import cn.fw.dalaran.service.Common;
8 9 import cn.fw.dalaran.service.biz.OtherBizService;
9 10 import cn.fw.data.base.domain.common.Message;
10 11 import cn.fw.security.auth.client.annotation.Authorization;
... ... @@ -39,6 +40,7 @@ import static cn.fw.common.web.util.ResultBuilder.success;
39 40 public class OtherController {
40 41  
41 42 private final OtherBizService otherBizService;
  43 + private final Common common;
42 44  
43 45 /**
44 46 * 获取直播审计页面
... ... @@ -76,13 +78,13 @@ public class OtherController {
76 78 }
77 79  
78 80 /**
79   - * 指定目录创建文件夹
  81 + * 操作指定目录
80 82 *
81 83 * @param dir 目录
82 84 * @param type 操作类型
83 85 * @return
84 86 */
85   - @GetMapping("/mkdir")
  87 + @GetMapping("/processDir")
86 88 public Message<Boolean> mkdir(@NotBlank(message = "请必须指定路径") String dir, @NotNull(message = "请必须指定操作类型") Integer type) {
87 89 File file = new File(dir);
88 90 Boolean result = null;
... ... @@ -93,7 +95,9 @@ public class OtherController {
93 95 result = file.mkdirs();
94 96 } else {
95 97 handleType = "删除";
96   - if (file.exists())
  98 + if (file.isDirectory())
  99 + result = common.deleteAllFile(dir, file);
  100 + else if(file.isFile())
97 101 result = file.delete();
98 102 }
99 103 log.info(String.format("%s文件夹, 路径为: %s, 结果为: %s", handleType, dir, Objects.isNull(result) ? "未执行" : (result ? "成功" : "失败")));
... ...
fw-dalaran-server/src/main/java/cn/fw/dalaran/server/task/OtherTask.java 0 → 100644
  1 +package cn.fw.dalaran.server.task;
  2 +
  3 +import cn.fw.dalaran.domain.db.ActivityTheme;
  4 +import cn.fw.dalaran.service.Common;
  5 +import cn.fw.dalaran.service.data.ActivityThemeService;
  6 +import lombok.RequiredArgsConstructor;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  9 +import org.springframework.scheduling.annotation.Scheduled;
  10 +import org.springframework.stereotype.Component;
  11 +import org.springframework.util.CollectionUtils;
  12 +
  13 +import java.io.File;
  14 +import java.util.List;
  15 +import java.util.stream.Collectors;
  16 +
  17 +/**
  18 + * @author wmy3969
  19 + * @version 1.0
  20 + * @date 2022/7/27 09:04
  21 + * @Description 系统其他定时任务
  22 + */
  23 +@Slf4j
  24 +@Component
  25 +@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
  26 +@RequiredArgsConstructor
  27 +public class OtherTask {
  28 +
  29 + private final ActivityThemeService activityThemeService;// 活动主题业务
  30 + private final Common common;
  31 +
  32 + /**
  33 + * 每天都找已经停止抓数据7天及其更久的主题, 删除缓存封面图
  34 + */
  35 + @Scheduled(fixedRate = 15 * 60 * 1000, initialDelay = 5 * 1000)
  36 + public void deleteCoverFiles() {
  37 + final List<ActivityTheme> themeList = activityThemeService.lambdaQuery()
  38 + .list()
  39 + .stream()
  40 + .filter(item -> activityThemeService.themeHaveStopCatchData(item.getEndTime(), 7))
  41 + .collect(Collectors.toList());// 找到需要审计的主题
  42 + if (CollectionUtils.isEmpty(themeList))
  43 + return;
  44 + String tempDir = common.getActivityThemeCoverDir();
  45 + StringBuilder sb = new StringBuilder();
  46 + themeList.forEach(item -> {
  47 + sb.append(tempDir);
  48 + sb.append("activityTheme");
  49 + sb.append(File.separator);
  50 + sb.append(item.getTheme());
  51 + String dirPath = sb.toString();
  52 + File file = new File(dirPath);
  53 + if (file.isDirectory())
  54 + common.deleteAllFile(dirPath, file);
  55 + sb.setLength(0);
  56 + });
  57 + }
  58 +
  59 +}
... ...
fw-dalaran-service/src/main/java/cn/fw/dalaran/service/Common.java
... ... @@ -4,6 +4,9 @@ import lombok.extern.slf4j.Slf4j;
4 4 import org.springframework.beans.factory.annotation.Value;
5 5 import org.springframework.stereotype.Component;
6 6  
  7 +import java.io.File;
  8 +import java.util.Objects;
  9 +
7 10 /**
8 11 * @author wmy3969
9 12 * @version 1.0
... ... @@ -28,4 +31,59 @@ public class Common {
28 31 else
29 32 return "/web/logs/testserver.dalaran.feewee.cn/";
30 33 }
  34 +
  35 + /**
  36 + * 删除文件
  37 + *
  38 + * @param filePath 文件路径
  39 + * @return 操作结果
  40 + */
  41 + public boolean deleteFile(String filePath) {
  42 + File file = new File(filePath);
  43 + if (file.exists() && file.isFile()) {// 如果文件路径只有单个文件
  44 + boolean result = file.delete();
  45 + if (result)
  46 + log.info(String.format("删除文件[%s]成功", filePath));
  47 + else
  48 + log.info(String.format("删除文件[%s]失败", filePath));
  49 + return result;
  50 + } else {
  51 + log.error(String.format("文件[%s]不存在", filePath));
  52 + return false;
  53 + }
  54 + }
  55 +
  56 + /**
  57 + * 删除文件夹
  58 + *
  59 + * @param dirPath 文件夹路径
  60 + * @param appointedFile 指定开始递归的文件(可不传)
  61 + * @return 操作结果
  62 + */
  63 + public boolean deleteAllFile(String dirPath, File... appointedFile) {
  64 + File dirFile = Objects.equals(0, appointedFile.length) ? new File(dirPath) : appointedFile[0];
  65 + if (!dirFile.isDirectory()) {// 如果dir对应的文件不存在, 或者不是一个目录, 则退出
  66 + log.error(String.format("删除文件夹失败:[%s]不是文件夹或不存在", dirPath));
  67 + return false;
  68 + }
  69 + // 递归删除文件夹中的所有文件包括子文件夹
  70 + boolean flag = true;
  71 + File[] files = dirFile.listFiles();
  72 + Objects.requireNonNull(files);
  73 + for (File file : files) {
  74 + if (file.isFile())
  75 + flag = this.deleteFile(file.getAbsolutePath());// 删除子文件
  76 + else if (file.isDirectory())
  77 + flag = this.deleteAllFile(file.getAbsolutePath());// 递归
  78 + if (!flag) {
  79 + log.error("删除文件夹失败");
  80 + return false;
  81 + }
  82 + }
  83 + boolean result = dirFile.delete();// 删除当前文件夹
  84 + if (result)
  85 + log.info(String.format("删除文件夹[%s]成功", dirPath));
  86 + return result;
  87 + }
  88 +
31 89 }
... ...
fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ActivityThemeService.java
... ... @@ -102,4 +102,13 @@ public interface ActivityThemeService extends IService&lt;ActivityTheme&gt; {
102 102 * @param configGroupId 配置组id
103 103 */
104 104 String getValidConfigJson(Long configGroupId);
  105 +
  106 + /**
  107 + * 验证主题是否结束数据抓取
  108 + *
  109 + * @param themeEndTime 主题结束时间
  110 + * @param delayDays 延期天数
  111 + * @return 主题是否结束数据抓取
  112 + */
  113 + boolean themeHaveStopCatchData(Date themeEndTime, int delayDays);
105 114 }
... ...
fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ActivityThemeServiceImpl.java
... ... @@ -173,7 +173,7 @@ public class ActivityThemeServiceImpl extends ServiceImpl&lt;ActivityThemeDao, Acti
173 173 List<String> oldTopic = Arrays.asList(theme.getTopic().split(",").clone());
174 174 ArrayList<String> result = new ArrayList<>(newTopic);
175 175 result.retainAll(oldTopic);
176   - if (this.themeHaveStopCatchData(theme.getEndTime()))
  176 + if (this.themeHaveStopCatchData(theme.getEndTime(), 0))
177 177 throw new BusinessException("活动已经结束数据抓取, 不能再增加话题");
178 178 if (result.size() < oldTopic.size())
179 179 throw new BusinessException("不允许删除原有话题");
... ... @@ -198,7 +198,7 @@ public class ActivityThemeServiceImpl extends ServiceImpl&lt;ActivityThemeDao, Acti
198 198 .peek(item -> item.setType(FileTypeEnum.THEME_COVER.getValue()))
199 199 .collect(Collectors.toList());
200 200 ActivityTheme theme = this.getById(themeId);
201   - if (this.themeHaveStopCatchData(theme.getEndTime()))
  201 + if (this.themeHaveStopCatchData(theme.getEndTime(), 0))
202 202 throw new BusinessException("活动已经结束数据抓取, 不能再增加封面");
203 203 this.saveFiles(themeId, fileList);
204 204 this.downloadAndConvertCover(theme, fileList);
... ... @@ -403,14 +403,16 @@ public class ActivityThemeServiceImpl extends ServiceImpl&lt;ActivityThemeDao, Acti
403 403 }
404 404  
405 405 /**
406   - * 判断主题是否结束数据抓取
  406 + * 验证主题是否结束数据抓取
407 407 *
408 408 * @param themeEndTime 主题结束时间
409   - * @retur 主题是否结束数据抓取
  409 + * @param delayDays 延期天数
  410 + * @return 主题是否结束数据抓取
410 411 */
411   - private boolean themeHaveStopCatchData(Date themeEndTime) {
  412 + @Override
  413 + public boolean themeHaveStopCatchData(Date themeEndTime, int delayDays) {
412 414 long timeSub = System.currentTimeMillis() - themeEndTime.getTime();
413   - return timeSub > 24 * 3600 * 1000 + 1000;
  415 + return timeSub > (1 + delayDays) * 24 * 3600 * 1000L + 1000;
414 416 }
415 417  
416 418 /**
... ... @@ -419,7 +421,7 @@ public class ActivityThemeServiceImpl extends ServiceImpl&lt;ActivityThemeDao, Acti
419 421 * @param item 待处理的活动主题
420 422 */
421 423 private void peekActivityThemeVo(ActivityThemeVo item) {
422   - item.setCanAlter(!this.themeHaveStopCatchData(item.getEndTime()));
  424 + item.setCanAlter(!this.themeHaveStopCatchData(item.getEndTime(), 0));
423 425 item.setAllFileDesc(Optional.ofNullable(
424 426 themeFileService.lambdaQuery()
425 427 .eq(ThemeFile::getThemeId, item.getId())
... ...