Commit 75fccfaf1774b0902eec6e7dd8b2dac8105fa04b
1 parent
4ef1277c
使用zlm原生接口删除录像文件
Showing
4 changed files
with
43 additions
and
27 deletions
src/main/java/com/genersoft/iot/vmp/conf/CloudRecordTimer.java
| ... | ... | @@ -3,8 +3,10 @@ package com.genersoft.iot.vmp.conf; |
| 3 | 3 | |
| 4 | 4 | import com.alibaba.fastjson2.JSONObject; |
| 5 | 5 | import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils; |
| 6 | +import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; | |
| 6 | 7 | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| 7 | 8 | import com.genersoft.iot.vmp.service.IMediaServerService; |
| 9 | +import com.genersoft.iot.vmp.service.bean.CloudRecordItem; | |
| 8 | 10 | import com.genersoft.iot.vmp.storager.dao.CloudRecordServiceMapper; |
| 9 | 11 | import com.genersoft.iot.vmp.vmanager.cloudRecord.CloudRecordController; |
| 10 | 12 | import org.slf4j.Logger; |
| ... | ... | @@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
| 13 | 15 | import org.springframework.scheduling.annotation.Scheduled; |
| 14 | 16 | import org.springframework.stereotype.Component; |
| 15 | 17 | |
| 18 | +import java.io.File; | |
| 16 | 19 | import java.util.ArrayList; |
| 17 | 20 | import java.util.Calendar; |
| 18 | 21 | import java.util.Date; |
| ... | ... | @@ -33,12 +36,13 @@ public class CloudRecordTimer { |
| 33 | 36 | private CloudRecordServiceMapper cloudRecordServiceMapper; |
| 34 | 37 | |
| 35 | 38 | @Autowired |
| 36 | - private AssistRESTfulUtils assistRESTfulUtils; | |
| 39 | + private ZLMRESTfulUtils zlmresTfulUtils; | |
| 37 | 40 | |
| 38 | 41 | /** |
| 39 | 42 | * 定时查询待删除的录像文件 |
| 40 | 43 | */ |
| 41 | 44 | @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行 |
| 45 | +// @Scheduled(fixedRate = 5000) | |
| 42 | 46 | public void execute(){ |
| 43 | 47 | logger.info("[录像文件定时清理] 开始清理过期录像文件"); |
| 44 | 48 | // 获取配置了assist的流媒体节点 |
| ... | ... | @@ -55,18 +59,29 @@ public class CloudRecordTimer { |
| 55 | 59 | // 获取保存的最后截至日期,因为每个节点都有一个日期,也就是支持每个节点设置不同的保存日期, |
| 56 | 60 | lastCalendar.add(Calendar.DAY_OF_MONTH, -mediaServerItem.getRecordDate()); |
| 57 | 61 | Long lastDate = lastCalendar.getTimeInMillis(); |
| 62 | + | |
| 58 | 63 | // 获取到截至日期之前的录像文件列表,文件列表满足未被收藏和保持的。这两个字段目前共能一致, |
| 59 | 64 | // 为我自己业务系统相关的代码,大家使用的时候直接使用收藏(collect)这一个类型即可 |
| 60 | - List<String> filePathList = cloudRecordServiceMapper.queryRecordFilePathListForDelete(lastDate, mediaServerItem.getId()); | |
| 61 | - if (filePathList.isEmpty()) { | |
| 65 | + List<CloudRecordItem> cloudRecordItemList = cloudRecordServiceMapper.queryRecordListForDelete(lastDate, mediaServerItem.getId()); | |
| 66 | + if (cloudRecordItemList.isEmpty()) { | |
| 62 | 67 | continue; |
| 63 | 68 | } |
| 64 | - // 先调用assist删除磁盘文件,删除成功后再删除数据库记录 | |
| 65 | - JSONObject jsonObject = assistRESTfulUtils.deleteFiles(mediaServerItem, filePathList); | |
| 66 | - if (jsonObject != null && jsonObject.getInteger("code") == 0 && jsonObject.getInteger("data") > 0) { | |
| 67 | - result += jsonObject.getInteger("data"); | |
| 68 | - cloudRecordServiceMapper.deleteByFileList(filePathList, mediaServerItem.getId()); | |
| 69 | + List<Integer> cloudRecordItemIdList = new ArrayList<>(); | |
| 70 | + for (CloudRecordItem cloudRecordItem : cloudRecordItemList) { | |
| 71 | + String date = new File(cloudRecordItem.getFilePath()).getParentFile().getName(); | |
| 72 | + JSONObject jsonObject = zlmresTfulUtils.deleteRecordDirectory(mediaServerItem, cloudRecordItem.getApp(), | |
| 73 | + cloudRecordItem.getStream(), date, cloudRecordItem.getFileName()); | |
| 74 | + if (jsonObject.getInteger("code") == 0) { | |
| 75 | + cloudRecordItemIdList.add(cloudRecordItem.getId()); | |
| 76 | + }else { | |
| 77 | + logger.warn("[录像文件定时清理] 删除磁盘文件错误: {}", jsonObject); | |
| 78 | + } | |
| 79 | + } | |
| 80 | + if (cloudRecordItemIdList.isEmpty()) { | |
| 81 | + continue; | |
| 69 | 82 | } |
| 83 | + cloudRecordServiceMapper.deleteList(cloudRecordItemIdList, mediaServerItem.getId()); | |
| 84 | + result += cloudRecordItemIdList.size(); | |
| 70 | 85 | } |
| 71 | 86 | } |
| 72 | 87 | logger.info("[录像文件定时清理] 共清理{}个过期录像文件", result); | ... | ... |
src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java
| ... | ... | @@ -268,14 +268,4 @@ public class AssistRESTfulUtils { |
| 268 | 268 | |
| 269 | 269 | return sendGet(mediaServerItem, "api/record/file/download/task/list", param, null); |
| 270 | 270 | } |
| 271 | - | |
| 272 | - public JSONObject addCollect(MediaServerItem mediaServerItem, JSONObject jsonObject) { | |
| 273 | - return sendPost(mediaServerItem, "api/record/file/collection/add", jsonObject, null, 30); | |
| 274 | - } | |
| 275 | - | |
| 276 | - public JSONObject deleteFiles(MediaServerItem mediaServerItem, List<String> filePathList) { | |
| 277 | - JSONObject jsonObject = new JSONObject(); | |
| 278 | - jsonObject.put("filePathList", filePathList); | |
| 279 | - return sendPost(mediaServerItem, "api/record/file/delete", jsonObject, null, 15*60); | |
| 280 | - } | |
| 281 | 271 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
| ... | ... | @@ -25,8 +25,6 @@ public class ZLMRESTfulUtils { |
| 25 | 25 | |
| 26 | 26 | private OkHttpClient client; |
| 27 | 27 | |
| 28 | - | |
| 29 | - | |
| 30 | 28 | public interface RequestCallback{ |
| 31 | 29 | void run(JSONObject response); |
| 32 | 30 | } |
| ... | ... | @@ -398,4 +396,14 @@ public class ZLMRESTfulUtils { |
| 398 | 396 | param.put("stream_id", streamId); |
| 399 | 397 | return sendPost(mediaServerItem, "updateRtpServerSSRC",param, null); |
| 400 | 398 | } |
| 399 | + | |
| 400 | + public JSONObject deleteRecordDirectory(MediaServerItem mediaServerItem, String app, String stream, String date, String fileName) { | |
| 401 | + Map<String, Object> param = new HashMap<>(1); | |
| 402 | + param.put("vhost", "__defaultVhost__"); | |
| 403 | + param.put("app", app); | |
| 404 | + param.put("stream", stream); | |
| 405 | + param.put("period", date); | |
| 406 | + param.put("name", fileName); | |
| 407 | + return sendPost(mediaServerItem, "deleteRecordDirectory",param, null); | |
| 408 | + } | |
| 401 | 409 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/dao/CloudRecordServiceMapper.java
| ... | ... | @@ -82,25 +82,28 @@ public interface CloudRecordServiceMapper { |
| 82 | 82 | int updateCollectList(@Param("collect") boolean collect, List<CloudRecordItem> cloudRecordItemList); |
| 83 | 83 | |
| 84 | 84 | @Delete(" <script>" + |
| 85 | - "delete from wvp_cloud_record where media_server_id=#{mediaServerId} file_path in " + | |
| 85 | + "delete from wvp_cloud_record where media_server_id=#{mediaServerId} and file_path in " + | |
| 86 | 86 | " <foreach collection='filePathList' item='item' open='(' separator=',' close=')' > #{item}</foreach>" + |
| 87 | 87 | " </script>") |
| 88 | 88 | void deleteByFileList(List<String> filePathList, @Param("mediaServerId") String mediaServerId); |
| 89 | 89 | |
| 90 | 90 | |
| 91 | 91 | @Select(" <script>" + |
| 92 | - "select file_path" + | |
| 92 | + "select *" + | |
| 93 | 93 | " from wvp_cloud_record " + |
| 94 | - " where collect = false " + | |
| 95 | - " <if test= 'endTimeStamp != null '> and start_time <= #{endTimeStamp}</if>" + | |
| 96 | - " <if test= 'callId != null '> and call_id = #{callId}</if>" + | |
| 97 | - " <if test= 'mediaServerId != null ' > and media_server_id = #{mediaServerId} </if>" + | |
| 94 | + " where end_time <= #{endTimeStamp} and media_server_id = #{mediaServerId} " + | |
| 98 | 95 | " </script>") |
| 99 | - List<String> queryRecordFilePathListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId); | |
| 96 | + List<CloudRecordItem> queryRecordListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId); | |
| 100 | 97 | |
| 101 | 98 | @Update(" <script>" + |
| 102 | 99 | "update wvp_cloud_record set collect = #{collect} where id = #{recordId} " + |
| 103 | 100 | " </script>") |
| 104 | 101 | int changeCollectById(@Param("collect") boolean collect, @Param("recordId") Integer recordId); |
| 105 | 102 | |
| 103 | + @Delete(" <script>" + | |
| 104 | + "delete from wvp_cloud_record where media_server_id=#{mediaServerId} and id in " + | |
| 105 | + " <foreach collection='cloudRecordItemIdList' item='item' open='(' separator=',' close=')' > #{item}</foreach>" + | |
| 106 | + " </script>") | |
| 107 | + int deleteList(List<Integer> cloudRecordItemIdList, @Param("mediaServerId") String mediaServerId); | |
| 108 | + | |
| 106 | 109 | } | ... | ... |