Commit e175ca1763b86cd26910b01ef3e5987dd47e902a
1 parent
d7d49dee
优化对旧录像文件的处理
Showing
4 changed files
with
60 additions
and
22 deletions
src/main/java/top/panll/assist/config/StartConfig.java
| @@ -46,7 +46,7 @@ public class StartConfig implements CommandLineRunner { | @@ -46,7 +46,7 @@ public class StartConfig implements CommandLineRunner { | ||
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | @Override | 48 | @Override |
| 49 | - public void run(String... args) { | 49 | + public void run(String... args) throws IOException { |
| 50 | if (!record.endsWith(File.separator)) { | 50 | if (!record.endsWith(File.separator)) { |
| 51 | record = record + File.separator; | 51 | record = record + File.separator; |
| 52 | } | 52 | } |
| @@ -68,6 +68,7 @@ public class StartConfig implements CommandLineRunner { | @@ -68,6 +68,7 @@ public class StartConfig implements CommandLineRunner { | ||
| 68 | } | 68 | } |
| 69 | List<CloudRecordItem> cloudRecordItemList = new ArrayList<>(); | 69 | List<CloudRecordItem> cloudRecordItemList = new ArrayList<>(); |
| 70 | Map<String, String> renameMap = new HashMap<>(); | 70 | Map<String, String> renameMap = new HashMap<>(); |
| 71 | + Map<String, List<CloudRecordItem>> dateVideoFileIndexList = new HashMap<>(); | ||
| 71 | List<String> streamFileList = new ArrayList<>(); | 72 | List<String> streamFileList = new ArrayList<>(); |
| 72 | // 搜集数据 | 73 | // 搜集数据 |
| 73 | for (File appFile : appFiles) { | 74 | for (File appFile : appFiles) { |
| @@ -92,24 +93,27 @@ public class StartConfig implements CommandLineRunner { | @@ -92,24 +93,27 @@ public class StartConfig implements CommandLineRunner { | ||
| 92 | } | 93 | } |
| 93 | stream = streamInfoArray[0]; | 94 | stream = streamInfoArray[0]; |
| 94 | String callId = streamInfoArray[1]; | 95 | String callId = streamInfoArray[1]; |
| 95 | - File[] dateFiles = streamFile.listFiles(); | 96 | + boolean collect = false; |
| 97 | + boolean reserve = false; | ||
| 98 | + File[] signFiles = streamFile.listFiles(File::isFile); | ||
| 99 | + if (signFiles.length > 0) { | ||
| 100 | + for (File signFile : signFiles) { | ||
| 101 | + if (signFile.getName().equals("a.sign")) { | ||
| 102 | + reserve = true; // 关联 | ||
| 103 | + }else if (signFile.getName().equals("b.sign")) { | ||
| 104 | + collect = true; // 归档 | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + File[] dateFiles = streamFile.listFiles(File::isDirectory); | ||
| 96 | if (dateFiles == null || dateFiles.length == 0) { | 110 | if (dateFiles == null || dateFiles.length == 0) { |
| 97 | continue; | 111 | continue; |
| 98 | } | 112 | } |
| 99 | streamFileList.add(streamFile.getAbsolutePath()); | 113 | streamFileList.add(streamFile.getAbsolutePath()); |
| 100 | - // TODC 确定关联和归档分别使用了什么类型名称 | ||
| 101 | - boolean collect = false; | ||
| 102 | - boolean reserve = false; | 114 | + |
| 103 | for (File dateFile : dateFiles) { | 115 | for (File dateFile : dateFiles) { |
| 104 | - if (dateFile.isFile()) { | ||
| 105 | - if (dateFile.getName().endsWith(".sign")) { | ||
| 106 | - if (dateFile.getName().startsWith("a")) { | ||
| 107 | - collect = true; | ||
| 108 | - }else if (dateFile.getName().startsWith("b")) { | ||
| 109 | - reserve = true; | ||
| 110 | - } | ||
| 111 | - } | ||
| 112 | - }else { | 116 | + if (dateFile.isDirectory()) { |
| 113 | // 检验是否是日期格式 | 117 | // 检验是否是日期格式 |
| 114 | if (!DateUtils.checkDateFormat(dateFile.getName())) { | 118 | if (!DateUtils.checkDateFormat(dateFile.getName())) { |
| 115 | continue; | 119 | continue; |
| @@ -119,6 +123,8 @@ public class StartConfig implements CommandLineRunner { | @@ -119,6 +123,8 @@ public class StartConfig implements CommandLineRunner { | ||
| 119 | if (videoFiles == null || videoFiles.length == 0) { | 123 | if (videoFiles == null || videoFiles.length == 0) { |
| 120 | continue; | 124 | continue; |
| 121 | } | 125 | } |
| 126 | + | ||
| 127 | + | ||
| 122 | for (int i = 0; i < videoFiles.length; i++) { | 128 | for (int i = 0; i < videoFiles.length; i++) { |
| 123 | File videoFile = videoFiles[i]; | 129 | File videoFile = videoFiles[i]; |
| 124 | if (!videoFile.getName().endsWith(".mp4") && !videoFile.getName().contains("-")) { | 130 | if (!videoFile.getName().endsWith(".mp4") && !videoFile.getName().contains("-")) { |
| @@ -136,10 +142,16 @@ public class StartConfig implements CommandLineRunner { | @@ -136,10 +142,16 @@ public class StartConfig implements CommandLineRunner { | ||
| 136 | } | 142 | } |
| 137 | String startTime = date + " " + videoInfoArray[0]; | 143 | String startTime = date + " " + videoInfoArray[0]; |
| 138 | String endTime = date + " " + videoInfoArray[1]; | 144 | String endTime = date + " " + videoInfoArray[1]; |
| 139 | - Long startTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime); | ||
| 140 | - Long endTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime); | 145 | + Long startTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime) * 1000; |
| 146 | + Long endTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) * 1000; | ||
| 141 | 147 | ||
| 142 | long timeLength = Long.parseLong(videoInfoArray[2].substring(0, videoInfoArray[2].length() - 4)); | 148 | long timeLength = Long.parseLong(videoInfoArray[2].substring(0, videoInfoArray[2].length() - 4)); |
| 149 | + | ||
| 150 | + String dataPath = appFile.getAbsolutePath() + File.separator + stream + File.separator + dateFile.getName(); | ||
| 151 | + if (dateVideoFileIndexList.get(dataPath) == null) { | ||
| 152 | + dateVideoFileIndexList.put(dataPath, new ArrayList<>()); | ||
| 153 | + } | ||
| 154 | + | ||
| 143 | CloudRecordItem cloudRecordItem = new CloudRecordItem(); | 155 | CloudRecordItem cloudRecordItem = new CloudRecordItem(); |
| 144 | cloudRecordItem.setApp(app); | 156 | cloudRecordItem.setApp(app); |
| 145 | cloudRecordItem.setStream(stream); | 157 | cloudRecordItem.setStream(stream); |
| @@ -149,14 +161,14 @@ public class StartConfig implements CommandLineRunner { | @@ -149,14 +161,14 @@ public class StartConfig implements CommandLineRunner { | ||
| 149 | cloudRecordItem.setCollect(collect); | 161 | cloudRecordItem.setCollect(collect); |
| 150 | cloudRecordItem.setReserve(reserve); | 162 | cloudRecordItem.setReserve(reserve); |
| 151 | cloudRecordItem.setMediaServerId(mediaServerId); | 163 | cloudRecordItem.setMediaServerId(mediaServerId); |
| 152 | - cloudRecordItem.setFileName(DateUtils.getTimeStr(startTimeStamp) + "-" + i + ".mp4"); | 164 | + cloudRecordItem.setFileName(DateUtils.getTimeStr(startTimeStamp) + "-" + dateVideoFileIndexList.get(dataPath).size() + ".mp4"); |
| 153 | cloudRecordItem.setFolder(streamFile.getAbsolutePath()); | 165 | cloudRecordItem.setFolder(streamFile.getAbsolutePath()); |
| 154 | cloudRecordItem.setFileSize(videoFile.length()); | 166 | cloudRecordItem.setFileSize(videoFile.length()); |
| 155 | cloudRecordItem.setTimeLen(timeLength); | 167 | cloudRecordItem.setTimeLen(timeLength); |
| 156 | cloudRecordItem.setFilePath(appFile.getAbsolutePath() + File.separator + stream + File.separator + dateFile.getName() + File.separator + cloudRecordItem.getFileName()); | 168 | cloudRecordItem.setFilePath(appFile.getAbsolutePath() + File.separator + stream + File.separator + dateFile.getName() + File.separator + cloudRecordItem.getFileName()); |
| 157 | cloudRecordItemList.add(cloudRecordItem); | 169 | cloudRecordItemList.add(cloudRecordItem); |
| 170 | + dateVideoFileIndexList.get(dataPath).add(cloudRecordItem); | ||
| 158 | renameMap.put(videoFile.getAbsolutePath(), cloudRecordItem.getFilePath()); | 171 | renameMap.put(videoFile.getAbsolutePath(), cloudRecordItem.getFilePath()); |
| 159 | - System.out.println(cloudRecordItem.getFilePath()); | ||
| 160 | } | 172 | } |
| 161 | } | 173 | } |
| 162 | } | 174 | } |
| @@ -195,6 +207,10 @@ public class StartConfig implements CommandLineRunner { | @@ -195,6 +207,10 @@ public class StartConfig implements CommandLineRunner { | ||
| 195 | logger.info("开始修改磁盘文件"); | 207 | logger.info("开始修改磁盘文件"); |
| 196 | for (String oldFileName : renameMap.keySet()) { | 208 | for (String oldFileName : renameMap.keySet()) { |
| 197 | File oldFile = new File(oldFileName); | 209 | File oldFile = new File(oldFileName); |
| 210 | + File newFile = new File(renameMap.get(oldFileName)); | ||
| 211 | + if (!newFile.getParentFile().exists()) { | ||
| 212 | + newFile.getParentFile().mkdirs(); | ||
| 213 | + } | ||
| 198 | boolean result = oldFile.renameTo(new File(renameMap.get(oldFileName))); | 214 | boolean result = oldFile.renameTo(new File(renameMap.get(oldFileName))); |
| 199 | if (result) { | 215 | if (result) { |
| 200 | logger.info("重命名成功: " + oldFileName + "===" + renameMap.get(oldFileName)); | 216 | logger.info("重命名成功: " + oldFileName + "===" + renameMap.get(oldFileName)); |
| @@ -203,7 +219,9 @@ public class StartConfig implements CommandLineRunner { | @@ -203,7 +219,9 @@ public class StartConfig implements CommandLineRunner { | ||
| 203 | logger.info("修改磁盘文件完成"); | 219 | logger.info("修改磁盘文件完成"); |
| 204 | logger.info("清理失效文件夹"); | 220 | logger.info("清理失效文件夹"); |
| 205 | for (String streamFileStr : streamFileList) { | 221 | for (String streamFileStr : streamFileList) { |
| 206 | - new File(streamFileStr).delete(); | 222 | + System.out.println(streamFileStr); |
| 223 | + File deleteFile = new File(streamFileStr); | ||
| 224 | + deleteFile(deleteFile); | ||
| 207 | } | 225 | } |
| 208 | logger.info("清理失效文件夹结束"); | 226 | logger.info("清理失效文件夹结束"); |
| 209 | } | 227 | } |
| @@ -212,4 +230,22 @@ public class StartConfig implements CommandLineRunner { | @@ -212,4 +230,22 @@ public class StartConfig implements CommandLineRunner { | ||
| 212 | } | 230 | } |
| 213 | 231 | ||
| 214 | 232 | ||
| 233 | + public void deleteFile(File file) { | ||
| 234 | + if (!file.exists()) { | ||
| 235 | + logger.warn("[删除文件] {} 不存在 ", file.getAbsolutePath()); | ||
| 236 | + }else { | ||
| 237 | + if (file.isFile()) { | ||
| 238 | + file.delete(); | ||
| 239 | + return; | ||
| 240 | + } | ||
| 241 | + File[] files = file.listFiles(); | ||
| 242 | + if (files.length > 0) { | ||
| 243 | + for (File childFile : files) { | ||
| 244 | + deleteFile(childFile); | ||
| 245 | + } | ||
| 246 | + } | ||
| 247 | + file.delete(); | ||
| 248 | + } | ||
| 249 | + } | ||
| 250 | + | ||
| 215 | } | 251 | } |
src/main/java/top/panll/assist/mapper/CloudRecordServiceMapper.java
| @@ -90,13 +90,15 @@ public interface CloudRecordServiceMapper { | @@ -90,13 +90,15 @@ public interface CloudRecordServiceMapper { | ||
| 90 | " folder," + | 90 | " folder," + |
| 91 | " file_path," + | 91 | " file_path," + |
| 92 | " file_size," + | 92 | " file_size," + |
| 93 | + " collect," + | ||
| 94 | + " reserve," + | ||
| 93 | " time_len " + | 95 | " time_len " + |
| 94 | ") " + | 96 | ") " + |
| 95 | "values " + | 97 | "values " + |
| 96 | "<foreach collection='cloudRecordItems' index='index' item='item' separator=','> " + | 98 | "<foreach collection='cloudRecordItems' index='index' item='item' separator=','> " + |
| 97 | "(#{item.app}, #{item.stream}, #{item.callId}, #{item.startTime}, " + | 99 | "(#{item.app}, #{item.stream}, #{item.callId}, #{item.startTime}, " + |
| 98 | "#{item.endTime}, #{item.mediaServerId}, #{item.fileName},#{item.folder}," + | 100 | "#{item.endTime}, #{item.mediaServerId}, #{item.fileName},#{item.folder}," + |
| 99 | - "#{item.filePath}, #{item.fileSize}, #{item.timeLen}) " + | 101 | + "#{item.filePath}, #{item.fileSize}, #{item.collect}, #{item.reserve}, #{item.timeLen}) " + |
| 100 | "</foreach> " + | 102 | "</foreach> " + |
| 101 | "</script>") | 103 | "</script>") |
| 102 | int batchAdd(@Param("cloudRecordItems") List<CloudRecordItem> cloudRecordItems); | 104 | int batchAdd(@Param("cloudRecordItems") List<CloudRecordItem> cloudRecordItems); |
src/main/java/top/panll/assist/utils/DateUtils.java
| @@ -49,7 +49,7 @@ public class DateUtils { | @@ -49,7 +49,7 @@ public class DateUtils { | ||
| 49 | return formatter.format(date); | 49 | return formatter.format(date); |
| 50 | } | 50 | } |
| 51 | public static String getTimeStr(Long dateTime) { | 51 | public static String getTimeStr(Long dateTime) { |
| 52 | - SimpleDateFormat formatter = new SimpleDateFormat(PATTERNForTime); | 52 | + SimpleDateFormat formatter = new SimpleDateFormat(PATTERNForTime, Locale.CHINESE); |
| 53 | return formatter.format(new Date(dateTime)); | 53 | return formatter.format(new Date(dateTime)); |
| 54 | } | 54 | } |
| 55 | 55 |
src/main/resources/application.yml
| @@ -17,7 +17,7 @@ user-settings: | @@ -17,7 +17,7 @@ user-settings: | ||
| 17 | id: 1111 | 17 | id: 1111 |
| 18 | media-server-id: 11212 | 18 | media-server-id: 11212 |
| 19 | # [可选 ] zlm配置的录像路径,不配置则使用当前目录下的record目录 即: ./record | 19 | # [可选 ] zlm配置的录像路径,不配置则使用当前目录下的record目录 即: ./record |
| 20 | - record: /home/lin/record1/ | 20 | + record: /home/lin/record/ |
| 21 | # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理, 不配置则不删除 | 21 | # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理, 不配置则不删除 |
| 22 | recordDay: 7 | 22 | recordDay: 7 |
| 23 | # [可选 ] 录像下载合成临时文件保存时长, 不配置默认取值recordDay(单位: 天)每天晚12点自动对过期文件执行清理 | 23 | # [可选 ] 录像下载合成临时文件保存时长, 不配置默认取值recordDay(单位: 天)每天晚12点自动对过期文件执行清理 |