Commit adbac9f9865c837a71d8215dd27ce127190e88d9

Authored by 648540858
1 parent b1a66406

添加对临时文件的清理

src/main/java/top/panll/assist/WvpProAssistApplication.java
@@ -2,8 +2,10 @@ package top.panll.assist; @@ -2,8 +2,10 @@ package top.panll.assist;
2 2
3 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
  5 +import org.springframework.scheduling.annotation.EnableScheduling;
5 6
6 @SpringBootApplication 7 @SpringBootApplication
  8 +@EnableScheduling
7 public class WvpProAssistApplication { 9 public class WvpProAssistApplication {
8 10
9 public static void main(String[] args) { 11 public static void main(String[] args) {
src/main/java/top/panll/assist/dto/UserSettings.java
@@ -9,9 +9,12 @@ public class UserSettings { @@ -9,9 +9,12 @@ public class UserSettings {
9 @Value("${userSettings.record}") 9 @Value("${userSettings.record}")
10 private String record; 10 private String record;
11 11
12 - @Value("${userSettings.recordDay}") 12 + @Value("${userSettings.recordDay:7}")
13 private int recordDay; 13 private int recordDay;
14 14
  15 + @Value("${userSettings.recordTempDay:-1}")
  16 + private int recordTempDay;
  17 +
15 @Value("${userSettings.ffmpeg}") 18 @Value("${userSettings.ffmpeg}")
16 private String ffmpeg; 19 private String ffmpeg;
17 20
@@ -49,4 +52,16 @@ public class UserSettings { @@ -49,4 +52,16 @@ public class UserSettings {
49 public void setRecordDay(int recordDay) { 52 public void setRecordDay(int recordDay) {
50 this.recordDay = recordDay; 53 this.recordDay = recordDay;
51 } 54 }
  55 +
  56 + public int getRecordTempDay() {
  57 + if (recordTempDay == -1) {
  58 + return recordDay;
  59 + }else {
  60 + return recordTempDay;
  61 + }
  62 + }
  63 +
  64 + public void setRecordTempDay(int recordTempDay) {
  65 + this.recordTempDay = recordTempDay;
  66 + }
52 } 67 }
src/main/java/top/panll/assist/service/FileManagerTimer.java
@@ -28,16 +28,16 @@ public class FileManagerTimer { @@ -28,16 +28,16 @@ public class FileManagerTimer {
28 @Autowired 28 @Autowired
29 private VideoFileService videoFileService; 29 private VideoFileService videoFileService;
30 30
31 -// @Scheduled(fixedDelay = 20000) //测试 20秒执行一次 31 +// @Scheduled(fixedDelay = 2000) //测试 20秒执行一次
32 @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行 32 @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行
33 public void execute(){ 33 public void execute(){
34 int recordDay = userSettings.getRecordDay(); 34 int recordDay = userSettings.getRecordDay();
35 - Date date=new Date();  
36 - Calendar calendar = Calendar.getInstance();  
37 - calendar.setTime(date);  
38 - calendar.add(Calendar.DAY_OF_MONTH, 0 - recordDay);  
39 - date = calendar.getTime();  
40 - logger.info("[录像巡查]移除 {} 之前的文件", formatter.format(date)); 35 + Date lastDate=new Date();
  36 + Calendar lastCalendar = Calendar.getInstance();
  37 + lastCalendar.setTime(lastDate);
  38 + lastCalendar.add(Calendar.DAY_OF_MONTH, 0 - recordDay);
  39 + lastDate = lastCalendar.getTime();
  40 + logger.info("[录像巡查]移除 {} 之前的文件", formatter.format(lastDate));
41 File recordFileDir = new File(userSettings.getRecord()); 41 File recordFileDir = new File(userSettings.getRecord());
42 if (recordFileDir.canWrite()) { 42 if (recordFileDir.canWrite()) {
43 List<File> appList = videoFileService.getAppList(false); 43 List<File> appList = videoFileService.getAppList(false);
@@ -49,12 +49,17 @@ public class FileManagerTimer { @@ -49,12 +49,17 @@ public class FileManagerTimer {
49 List<File> streamList = videoFileService.getStreamList(appFile, false); 49 List<File> streamList = videoFileService.getStreamList(appFile, false);
50 if (streamList != null && streamList.size() > 0) { 50 if (streamList != null && streamList.size() > 0) {
51 for (File streamFile : streamList) { 51 for (File streamFile : streamList) {
  52 + // 带有sig标记文件的为收藏文件,不被自动清理任务移除
  53 + File signFile = new File(streamFile.getAbsolutePath() + File.separator + "sign");
  54 + if (signFile.exists()) {
  55 + continue;
  56 + }
52 List<File> dateList = videoFileService.getDateList(streamFile, null, null, false); 57 List<File> dateList = videoFileService.getDateList(streamFile, null, null, false);
53 if (dateList != null && dateList.size() > 0) { 58 if (dateList != null && dateList.size() > 0) {
54 for (File dateFile : dateList) { 59 for (File dateFile : dateList) {
55 try { 60 try {
56 Date parse = formatter.parse(dateFile.getName()); 61 Date parse = formatter.parse(dateFile.getName());
57 - if (parse.before(date)) { 62 + if (parse.before(lastDate)) {
58 boolean result = FileUtils.deleteQuietly(dateFile); 63 boolean result = FileUtils.deleteQuietly(dateFile);
59 if (result) { 64 if (result) {
60 logger.info("[录像巡查]成功移除 {} ", dateFile.getAbsolutePath()); 65 logger.info("[录像巡查]成功移除 {} ", dateFile.getAbsolutePath());
@@ -85,7 +90,28 @@ public class FileManagerTimer { @@ -85,7 +90,28 @@ public class FileManagerTimer {
85 logger.info("[录像巡查]移除失败 {} ", appFile.getAbsolutePath()); 90 logger.info("[录像巡查]移除失败 {} ", appFile.getAbsolutePath());
86 } 91 }
87 } 92 }
88 - 93 + }
  94 + }
  95 + }
  96 + // 清理任务临时文件
  97 + int recordTempDay = userSettings.getRecordTempDay();
  98 + Date lastTempDate = new Date();
  99 + Calendar lastTempCalendar = Calendar.getInstance();
  100 + lastTempCalendar.setTime(lastTempDate);
  101 + lastTempCalendar.add(Calendar.DAY_OF_MONTH, 0 - recordTempDay);
  102 + lastTempDate = lastTempCalendar.getTime();
  103 + logger.info("[录像巡查]移除合并任务临时文件 {} 之前的文件", formatter.format(lastTempDate));
  104 + File recordTempFile = new File(recordFileDir.getParentFile().getAbsolutePath() + File.separator + "recordTemp");
  105 + if (recordTempFile.exists() && recordTempFile.isDirectory() && recordTempFile.canWrite()) {
  106 + File[] tempFiles = recordTempFile.listFiles();
  107 + for (File tempFile : tempFiles) {
  108 + if (tempFile.isDirectory() && new Date(tempFile.lastModified()).before(lastTempDate)) {
  109 + boolean result = FileUtils.deleteQuietly(tempFile);
  110 + if (result) {
  111 + logger.info("[录像巡查]成功移除合并任务临时文件 {} ", tempFile.getAbsolutePath());
  112 + }else {
  113 + logger.info("[录像巡查]合并任务临时文件移除失败 {} ", tempFile.getAbsolutePath());
  114 + }
89 } 115 }
90 } 116 }
91 } 117 }
src/main/java/top/panll/assist/service/VideoFileService.java
@@ -52,7 +52,10 @@ public class VideoFileService { @@ -52,7 +52,10 @@ public class VideoFileService {
52 public List<File> getAppList(Boolean sort) { 52 public List<File> getAppList(Boolean sort) {
53 File recordFile = new File(userSettings.getRecord()); 53 File recordFile = new File(userSettings.getRecord());
54 if (recordFile != null && recordFile.isDirectory()) { 54 if (recordFile != null && recordFile.isDirectory()) {
55 - File[] files = recordFile.listFiles(); 55 + File[] files = recordFile.listFiles((File dir, String name) -> {
  56 + File currentFile = new File(dir.getAbsolutePath() + File.separator + name);
  57 + return currentFile.isDirectory();
  58 + });
56 List<File> result = Arrays.asList(files); 59 List<File> result = Arrays.asList(files);
57 if (sort != null && sort) { 60 if (sort != null && sort) {
58 Collections.sort(result); 61 Collections.sort(result);
@@ -107,7 +110,10 @@ public class VideoFileService { @@ -107,7 +110,10 @@ public class VideoFileService {
107 110
108 public List<File> getStreamList(File appFile, Boolean sort) { 111 public List<File> getStreamList(File appFile, Boolean sort) {
109 if (appFile != null && appFile.isDirectory()) { 112 if (appFile != null && appFile.isDirectory()) {
110 - File[] files = appFile.listFiles(); 113 + File[] files = appFile.listFiles((File dir, String name) -> {
  114 + File currentFile = new File(dir.getAbsolutePath() + File.separator + name);
  115 + return currentFile.isDirectory();
  116 + });
111 List<File> result = Arrays.asList(files); 117 List<File> result = Arrays.asList(files);
112 if (sort != null && sort) { 118 if (sort != null && sort) {
113 Collections.sort(result); 119 Collections.sort(result);
@@ -390,6 +396,10 @@ public class VideoFileService { @@ -390,6 +396,10 @@ public class VideoFileService {
390 return null; 396 return null;
391 } 397 }
392 File[] dateFiles = streamFile.listFiles((File dir, String name)->{ 398 File[] dateFiles = streamFile.listFiles((File dir, String name)->{
  399 + File currentFile = new File(dir.getAbsolutePath() + File.separator + name);
  400 + if (currentFile.isDirectory()){
  401 + return false;
  402 + }
393 Date date = null; 403 Date date = null;
394 try { 404 try {
395 date = simpleDateFormat.parse(name); 405 date = simpleDateFormat.parse(name);
src/main/resources/application-dev.yml
@@ -32,6 +32,8 @@ userSettings: @@ -32,6 +32,8 @@ userSettings:
32 record: /media/lin/Server/ZLMediaKit/dev/ZLMediaKit/release/linux/Debug/www/record 32 record: /media/lin/Server/ZLMediaKit/dev/ZLMediaKit/release/linux/Debug/www/record
33 # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理 33 # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理
34 recordDay: 7 34 recordDay: 7
  35 + # [可选 ] 录像下载合成临时文件保存时长, 不配置默认取值recordDay(单位: 天)每天晚12点自动对过期文件执行清理
  36 + # recordTempDay: 7
35 # [必选 ] ffmpeg路径 37 # [必选 ] ffmpeg路径
36 ffmpeg: /usr/bin/ffmpeg 38 ffmpeg: /usr/bin/ffmpeg
37 # [必选 ] ffprobe路径, 一般安装ffmpeg就会自带, 一般跟ffmpeg在同一目录,用于查询文件的信息 39 # [必选 ] ffprobe路径, 一般安装ffmpeg就会自带, 一般跟ffmpeg在同一目录,用于查询文件的信息