Commit 145c0c0997f2d9ba9072d4b14ef0a0e01cac82ce

Authored by 648540858
1 parent bae51700

优化录像定时删除

src/main/java/top/panll/assist/controller/RecordController.java
1 package top.panll.assist.controller; 1 package top.panll.assist.controller;
2 2
3 -import com.alibaba.fastjson.JSON;  
4 -import com.alibaba.fastjson.JSONObject;  
5 import io.swagger.v3.oas.annotations.Operation; 3 import io.swagger.v3.oas.annotations.Operation;
6 import io.swagger.v3.oas.annotations.Parameter; 4 import io.swagger.v3.oas.annotations.Parameter;
7 import io.swagger.v3.oas.annotations.tags.Tag; 5 import io.swagger.v3.oas.annotations.tags.Tag;
8 -import org.apache.commons.io.FileUtils;  
9 import org.slf4j.Logger; 6 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory; 7 import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
12 -import org.springframework.http.HttpStatus;  
13 -import org.springframework.http.ResponseEntity;  
14 import org.springframework.web.bind.annotation.*; 9 import org.springframework.web.bind.annotation.*;
15 -import top.panll.assist.controller.bean.ControllerException;  
16 -import top.panll.assist.controller.bean.ErrorCode;  
17 -import top.panll.assist.controller.bean.RecordFile;  
18 -import top.panll.assist.controller.bean.WVPResult; 10 +import top.panll.assist.controller.bean.*;
19 import top.panll.assist.dto.*; 11 import top.panll.assist.dto.*;
20 import top.panll.assist.service.VideoFileService; 12 import top.panll.assist.service.VideoFileService;
21 -import top.panll.assist.utils.PageInfo;  
22 import top.panll.assist.utils.RedisUtil; 13 import top.panll.assist.utils.RedisUtil;
23 14
24 -import javax.servlet.http.HttpServletRequest;  
25 -import java.io.File;  
26 -import java.text.DateFormat;  
27 -import java.text.ParseException;  
28 import java.text.SimpleDateFormat; 15 import java.text.SimpleDateFormat;
29 import java.util.*; 16 import java.util.*;
30 17
@@ -58,202 +45,6 @@ public class RecordController { @@ -58,202 +45,6 @@ public class RecordController {
58 return userSettings; 45 return userSettings;
59 } 46 }
60 47
61 -// /**  
62 -// * 获取app+stream列表  
63 -// * @return  
64 -// */  
65 -// @Operation(summary ="分页获取app+stream的列表")  
66 -// @Parameter(name = "page", description = "当前页", required = true)  
67 -// @Parameter(name = "count", description = "每页查询数量", required = true)  
68 -// @GetMapping(value = "/list")  
69 -// @ResponseBody  
70 -// public PageInfo<Map<String, String>> getList(@RequestParam int page,  
71 -// @RequestParam int count){  
72 -// List<Map<String, String>> appList = videoFileService.getList();  
73 -//  
74 -// PageInfo<Map<String, String>> stringPageInfo = new PageInfo<>(appList);  
75 -// stringPageInfo.startPage(page, count);  
76 -// return stringPageInfo;  
77 -// }  
78 -//  
79 -// /**  
80 -// * 分页获取app列表  
81 -// * @return  
82 -// */  
83 -// @Operation(summary ="分页获取app列表")  
84 -// @Parameter(name = "page", description = "当前页", required = true)  
85 -// @Parameter(name = "count", description = "每页查询数量", required = true)  
86 -// @GetMapping(value = "/app/list")  
87 -// @ResponseBody  
88 -// public PageInfo<String> getAppList(@RequestParam int page,  
89 -// @RequestParam int count){  
90 -// List<String> resultData = new ArrayList<>();  
91 -// List<File> appList = videoFileService.getAppList(true);  
92 -// if (appList.size() > 0) {  
93 -// for (File file : appList) {  
94 -// resultData.add(file.getName());  
95 -// }  
96 -// }  
97 -// Collections.sort(resultData);  
98 -//  
99 -// PageInfo<String> stringPageInfo = new PageInfo<>(resultData);  
100 -// stringPageInfo.startPage(page, count);  
101 -// return stringPageInfo;  
102 -// }  
103 -//  
104 -// /**  
105 -// * 分页stream列表  
106 -// * @return  
107 -// */  
108 -// @Operation(summary ="分页stream列表")  
109 -// @Parameter(name = "page", description = "当前页", required = true)  
110 -// @Parameter(name = "count", description = "每页查询数量", required = true)  
111 -// @Parameter(name = "app", description = "应用名", required = true)  
112 -// @GetMapping(value = "/stream/list")  
113 -// @ResponseBody  
114 -// public PageInfo<String> getStreamList(@RequestParam int page,  
115 -// @RequestParam int count,  
116 -// @RequestParam String app ){  
117 -// List<String> resultData = new ArrayList<>();  
118 -// if (app == null) {  
119 -// throw new ControllerException(ErrorCode.ERROR400.getCode(), "app不能为空");  
120 -// }  
121 -// List<File> streamList = videoFileService.getStreamList(app, true);  
122 -// if (streamList != null) {  
123 -// for (File file : streamList) {  
124 -// resultData.add(file.getName());  
125 -// }  
126 -// }  
127 -// PageInfo<String> stringPageInfo = new PageInfo<>(resultData);  
128 -// stringPageInfo.startPage(page, count);  
129 -// return stringPageInfo;  
130 -// }  
131 -//  
132 -// /**  
133 -// * 获取日期文件夹列表  
134 -// * @return  
135 -// */  
136 -// @Operation(summary ="获取日期文件夹列表")  
137 -// @Parameter(name = "year", description = "月", required = true)  
138 -// @Parameter(name = "month", description = "年", required = true)  
139 -// @Parameter(name = "app", description = "应用名", required = true)  
140 -// @Parameter(name = "stream", description = "流ID", required = true)  
141 -// @GetMapping(value = "/date/list")  
142 -// @ResponseBody  
143 -// public List<String> getDateList( @RequestParam(required = false) Integer year,  
144 -// @RequestParam(required = false) Integer month,  
145 -// @RequestParam String app,  
146 -// @RequestParam String stream ){  
147 -// List<String> resultData = new ArrayList<>();  
148 -// if (app == null) {  
149 -// throw new ControllerException(ErrorCode.ERROR400.getCode(), "app不能为空");  
150 -// };  
151 -// if (stream == null) {  
152 -// throw new ControllerException(ErrorCode.ERROR400.getCode(), "stream不能为空");  
153 -// }  
154 -// List<File> dateList = videoFileService.getDateList(app, stream, year, month, true);  
155 -// for (File file : dateList) {  
156 -// resultData.add(file.getName());  
157 -// }  
158 -// return resultData;  
159 -// }  
160 -//  
161 -// /**  
162 -// * 获取视频文件列表  
163 -// * @return  
164 -// */  
165 -// @Operation(summary ="获取视频文件列表")  
166 -// @Parameter(name = "page", description = "当前页", required = true)  
167 -// @Parameter(name = "count", description = "每页查询数量", required = true)  
168 -// @Parameter(name = "app", description = "应用名", required = true)  
169 -// @Parameter(name = "stream", description = "流ID", required = true)  
170 -// @Parameter(name = "startTime", description = "开始时间(yyyy-MM-dd HH:mm:ss)", required = true)  
171 -// @Parameter(name = "endTime", description = "结束时间(yyyy-MM-dd HH:mm:ss)", required = true)  
172 -// @GetMapping(value = "/file/list")  
173 -// @ResponseBody  
174 -// public PageInfo<String> getRecordList(@RequestParam int page,  
175 -// @RequestParam int count,  
176 -// @RequestParam String app,  
177 -// @RequestParam String stream,  
178 -// @RequestParam(required = false) String startTime,  
179 -// @RequestParam(required = false) String endTime  
180 -// ){  
181 -//  
182 -// // 开始时间与结束时间可不传或只传其一  
183 -// List<String> recordList = new ArrayList<>();  
184 -// try {  
185 -// Date startTimeDate = null;  
186 -// Date endTimeDate = null;  
187 -// if (startTime != null ) {  
188 -// startTimeDate = formatter.parse(startTime);  
189 -// }  
190 -// if (endTime != null ) {  
191 -// endTimeDate = formatter.parse(endTime);  
192 -// }  
193 -//  
194 -// List<File> filesInTime = videoFileService.getFilesInTime(app, stream, startTimeDate, endTimeDate);  
195 -// if (filesInTime != null && filesInTime.size() > 0) {  
196 -// for (File file : filesInTime) {  
197 -// recordList.add(file.getName());  
198 -// }  
199 -// }  
200 -// PageInfo<String> stringPageInfo = new PageInfo<>(recordList);  
201 -// stringPageInfo.startPage(page, count);  
202 -// return stringPageInfo;  
203 -// } catch (ParseException e) {  
204 -// logger.error("错误的开始时间[{}]或结束时间[{}]", startTime, endTime);  
205 -// throw new ControllerException(ErrorCode.ERROR400.getCode(), "错误的开始时间或结束时间, e=" + e.getMessage());  
206 -// }  
207 -// }  
208 -//  
209 -// /**  
210 -// * 获取视频文件列表  
211 -// * @return  
212 -// */  
213 -// @Operation(summary ="获取视频文件列表")  
214 -// @Parameter(name = "page", description = "当前页", required = true)  
215 -// @Parameter(name = "count", description = "每页查询数量", required = true)  
216 -// @Parameter(name = "app", description = "应用名", required = true)  
217 -// @Parameter(name = "stream", description = "流ID", required = true)  
218 -// @Parameter(name = "startTime", description = "开始时间(yyyy-MM-dd HH:mm:ss)", required = true)  
219 -// @Parameter(name = "endTime", description = "结束时间(yyyy-MM-dd HH:mm:ss)", required = true)  
220 -// @GetMapping(value = "/file/listWithDate")  
221 -// @ResponseBody  
222 -// public PageInfo<RecordFile> getRecordListWithDate(@RequestParam int page,  
223 -// @RequestParam int count,  
224 -// @RequestParam String app,  
225 -// @RequestParam String stream,  
226 -// @RequestParam(required = false) String startTime,  
227 -// @RequestParam(required = false) String endTime  
228 -// ){  
229 -//  
230 -// // 开始时间与结束时间可不传或只传其一  
231 -// List<RecordFile> recordList = new ArrayList<>();  
232 -// try {  
233 -// Date startTimeDate = null;  
234 -// Date endTimeDate = null;  
235 -// if (startTime != null ) {  
236 -// startTimeDate = formatter.parse(startTime);  
237 -// }  
238 -// if (endTime != null ) {  
239 -// endTimeDate = formatter.parse(endTime);  
240 -// }  
241 -//  
242 -// List<File> filesInTime = videoFileService.getFilesInTime(app, stream, startTimeDate, endTimeDate);  
243 -// if (filesInTime != null && filesInTime.size() > 0) {  
244 -// for (File file : filesInTime) {  
245 -// recordList.add(RecordFile.instance(app, stream, file.getName(), file.getParentFile().getName()));  
246 -// }  
247 -// }  
248 -// PageInfo<RecordFile> stringPageInfo = new PageInfo<>(recordList);  
249 -// stringPageInfo.startPage(page, count);  
250 -// return stringPageInfo;  
251 -// } catch (ParseException e) {  
252 -// logger.error("错误的开始时间[{}]或结束时间[{}]", startTime, endTime);  
253 -// throw new ControllerException(ErrorCode.ERROR400.getCode(), "错误的开始时间或结束时间, e=" + e.getMessage());  
254 -// }  
255 -// }  
256 -  
257 48
258 /** 49 /**
259 * 添加视频裁剪合并任务 50 * 添加视频裁剪合并任务
@@ -292,64 +83,6 @@ public class RecordController { @@ -292,64 +83,6 @@ public class RecordController {
292 } 83 }
293 84
294 /** 85 /**
295 - * 收藏录像(被收藏的录像不会被清理任务清理)  
296 - */  
297 - @Operation(summary ="收藏录像(被收藏的录像不会被清理任务清理)")  
298 - @Parameter(name = "type", description = "类型", required = true)  
299 - @Parameter(name = "app", description = "应用名", required = true)  
300 - @Parameter(name = "stream", description = "流ID", required = true)  
301 - @GetMapping(value = "/file/collection/add")  
302 - @ResponseBody  
303 - public void collection(  
304 - @RequestParam(required = true) String type,  
305 - @RequestParam(required = true) String app,  
306 - @RequestParam(required = true) String stream){  
307 -  
308 - boolean collectionResult = videoFileService.collection(app, stream, type);  
309 - if (!collectionResult) {  
310 - throw new ControllerException(ErrorCode.ERROR100);  
311 - }  
312 - }  
313 -  
314 - /**  
315 - * 移除收藏录像  
316 - */  
317 - @Operation(summary ="移除收藏录像")  
318 - @Parameter(name = "type", description = "类型", required = true)  
319 - @Parameter(name = "app", description = "应用名", required = true)  
320 - @Parameter(name = "stream", description = "流ID", required = true)  
321 - @GetMapping(value = "/file/collection/remove")  
322 - @ResponseBody  
323 - public void removeCollection(  
324 - @RequestParam(required = true) String type,  
325 - @RequestParam(required = true) String app,  
326 - @RequestParam(required = true) String stream){  
327 -  
328 - boolean collectionResult = videoFileService.removeCollection(app, stream, type);  
329 - if (!collectionResult) {  
330 - throw new ControllerException(ErrorCode.ERROR100);  
331 - }  
332 - }  
333 -  
334 - /**  
335 - * 收藏录像列表  
336 - */  
337 - @Operation(summary ="收藏录像列表")  
338 - @Parameter(name = "type", description = "类型", required = false)  
339 - @Parameter(name = "app", description = "应用名", required = false)  
340 - @Parameter(name = "stream", description = "流ID", required = false)  
341 - @GetMapping(value = "/file/collection/list")  
342 - @ResponseBody  
343 - public List<SignInfo> collectionList(  
344 - @RequestParam(required = false) String type,  
345 - @RequestParam(required = false) String app,  
346 - @RequestParam(required = false) String stream){  
347 -  
348 - List<SignInfo> signInfos = videoFileService.getCollectionList(app, stream, type);  
349 - return signInfos;  
350 - }  
351 -  
352 - /**  
353 * 中止视频裁剪合并任务列表 86 * 中止视频裁剪合并任务列表
354 */ 87 */
355 @Operation(summary ="中止视频裁剪合并任务列表(暂不支持)") 88 @Operation(summary ="中止视频裁剪合并任务列表(暂不支持)")
@@ -391,4 +124,15 @@ public class RecordController { @@ -391,4 +124,15 @@ public class RecordController {
391 public long fileDuration( @RequestParam String app, @RequestParam String stream) { 124 public long fileDuration( @RequestParam String app, @RequestParam String stream) {
392 return videoFileService.fileDuration(app, stream); 125 return videoFileService.fileDuration(app, stream);
393 } 126 }
  127 +
  128 + /**
  129 + * 删除文件
  130 + */
  131 + @Operation(summary ="删除文件")
  132 + @Parameter(name = "fileLIstInfo", description = "FileLIstInfo", required = true)
  133 + @ResponseBody
  134 + @PostMapping(value = "/file/delete", produces = "application/json;charset=UTF-8")
  135 + public int fileDuration( @RequestBody FileLIstInfo fileLIstInfo) {
  136 + return videoFileService.deleteFile(fileLIstInfo.getFilePathList());
  137 + }
394 } 138 }
src/main/java/top/panll/assist/controller/bean/FileLIstInfo.java 0 → 100644
  1 +package top.panll.assist.controller.bean;
  2 +
  3 +import java.util.List;
  4 +
  5 +public class FileLIstInfo {
  6 +
  7 + private List<String> filePathList;
  8 +
  9 + public List<String> getFilePathList() {
  10 + return filePathList;
  11 + }
  12 +
  13 + public void setFilePathList(List<String> filePathList) {
  14 + this.filePathList = filePathList;
  15 + }
  16 +}
src/main/java/top/panll/assist/service/FileManagerTimer.java
@@ -42,74 +42,7 @@ public class FileManagerTimer { @@ -42,74 +42,7 @@ public class FileManagerTimer {
42 if (userSettings.getRecord() == null) { 42 if (userSettings.getRecord() == null) {
43 return; 43 return;
44 } 44 }
45 - int recordDay = userSettings.getRecordDay();  
46 - Date lastDate=new Date();  
47 - Calendar lastCalendar = Calendar.getInstance();  
48 - if (recordDay > 0) {  
49 - lastCalendar.setTime(lastDate);  
50 - lastCalendar.add(Calendar.DAY_OF_MONTH, 0 - recordDay);  
51 - lastDate = lastCalendar.getTime();  
52 - }  
53 45
54 - logger.info("[录像巡查]移除 {} 之前的文件", formatter.format(lastDate));  
55 - File recordFileDir = new File(userSettings.getRecord());  
56 - if (recordFileDir.canWrite()) {  
57 - List<File> appList = videoFileService.getAppList(false);  
58 - if (appList != null && appList.size() > 0) {  
59 - for (File appFile : appList) {  
60 - if ("download.html".equals(appFile.getName())) {  
61 - continue;  
62 - }  
63 - List<File> streamList = videoFileService.getStreamList(appFile, false);  
64 - if (streamList != null && streamList.size() > 0) {  
65 - for (File streamFile : streamList) {  
66 - // 带有sig标记文件的为收藏文件,不被自动清理任务移除  
67 - File[] signFiles = streamFile.listFiles((File dir, String name) -> {  
68 - File currentFile = new File(dir.getAbsolutePath() + File.separator + name);  
69 - return currentFile.isFile() && name.endsWith(".sign");  
70 - });  
71 - if (signFiles != null && signFiles.length > 0) {  
72 - continue;  
73 - }  
74 - List<File> dateList = videoFileService.getDateList(streamFile, null, null, false);  
75 - if (dateList != null && dateList.size() > 0) {  
76 - for (File dateFile : dateList) {  
77 - try {  
78 - Date parse = formatter.parse(dateFile.getName());  
79 - if (parse.before(lastDate)) {  
80 - boolean result = FileUtils.deleteQuietly(dateFile);  
81 - if (result) {  
82 - logger.info("[录像巡查]成功移除 {} ", dateFile.getAbsolutePath());  
83 - }else {  
84 - logger.info("[录像巡查]移除失败 {} ", dateFile.getAbsolutePath());  
85 - }  
86 - }  
87 - } catch (ParseException e) {  
88 - e.printStackTrace();  
89 - }  
90 - }  
91 - }  
92 - if (streamFile.listFiles() == null || streamFile.listFiles().length == 0) {  
93 - boolean result = FileUtils.deleteQuietly(streamFile);  
94 - if (result) {  
95 - logger.info("[录像巡查]成功移除 {} ", streamFile.getAbsolutePath());  
96 - }else {  
97 - logger.info("[录像巡查]移除失败 {} ", streamFile.getAbsolutePath());  
98 - }  
99 - }  
100 - }  
101 - }  
102 - if (appFile.listFiles() == null || appFile.listFiles().length == 0) {  
103 - boolean result = FileUtils.deleteQuietly(appFile);  
104 - if (result) {  
105 - logger.info("[录像巡查]成功移除 {} ", appFile.getAbsolutePath());  
106 - }else {  
107 - logger.info("[录像巡查]移除失败 {} ", appFile.getAbsolutePath());  
108 - }  
109 - }  
110 - }  
111 - }  
112 - }  
113 // 清理任务临时文件 46 // 清理任务临时文件
114 int recordTempDay = userSettings.getRecordTempDay(); 47 int recordTempDay = userSettings.getRecordTempDay();
115 Date lastTempDate = new Date(); 48 Date lastTempDate = new Date();
src/main/java/top/panll/assist/service/VideoFileService.java
@@ -461,4 +461,24 @@ public class VideoFileService { @@ -461,4 +461,24 @@ public class VideoFileService {
461 } 461 }
462 return durationResult; 462 return durationResult;
463 } 463 }
  464 +
  465 + public int deleteFile(List<String> filePathList) {
  466 + assert filePathList != null;
  467 + assert filePathList.isEmpty();
  468 + int deleteResult = 0;
  469 + for (String filePath : filePathList) {
  470 + File file = new File(filePath);
  471 + if (file.exists()) {
  472 + if (file.delete()) {
  473 + deleteResult ++;
  474 + }
  475 + }else {
  476 + logger.warn("[删除文件] 文件不存在,{}", filePath);
  477 + }
  478 + }
  479 + if (deleteResult == 0) {
  480 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "未删除任何文件");
  481 + }
  482 + return deleteResult;
  483 + }
464 } 484 }