使用zlm原生接口删除录像文件

pull/1242/head
648540858 2023-11-01 10:45:36 +08:00
parent 4ef1277c16
commit 75fccfaf17
4 changed files with 43 additions and 27 deletions

View File

@ -3,8 +3,10 @@ package com.genersoft.iot.vmp.conf;
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.service.IMediaServerService;
import com.genersoft.iot.vmp.service.bean.CloudRecordItem;
import com.genersoft.iot.vmp.storager.dao.CloudRecordServiceMapper;
import com.genersoft.iot.vmp.vmanager.cloudRecord.CloudRecordController;
import org.slf4j.Logger;
@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -33,12 +36,13 @@ public class CloudRecordTimer {
private CloudRecordServiceMapper cloudRecordServiceMapper;
@Autowired
private AssistRESTfulUtils assistRESTfulUtils;
private ZLMRESTfulUtils zlmresTfulUtils;
/**
*
*/
@Scheduled(cron = "0 0 0 * * ?") //每天的0点执行
// @Scheduled(fixedRate = 5000)
public void execute(){
logger.info("[录像文件定时清理] 开始清理过期录像文件");
// 获取配置了assist的流媒体节点
@ -55,18 +59,29 @@ public class CloudRecordTimer {
// 获取保存的最后截至日期,因为每个节点都有一个日期,也就是支持每个节点设置不同的保存日期,
lastCalendar.add(Calendar.DAY_OF_MONTH, -mediaServerItem.getRecordDate());
Long lastDate = lastCalendar.getTimeInMillis();
// 获取到截至日期之前的录像文件列表,文件列表满足未被收藏和保持的。这两个字段目前共能一致,
// 为我自己业务系统相关的代码大家使用的时候直接使用收藏collect这一个类型即可
List<String> filePathList = cloudRecordServiceMapper.queryRecordFilePathListForDelete(lastDate, mediaServerItem.getId());
if (filePathList.isEmpty()) {
List<CloudRecordItem> cloudRecordItemList = cloudRecordServiceMapper.queryRecordListForDelete(lastDate, mediaServerItem.getId());
if (cloudRecordItemList.isEmpty()) {
continue;
}
// 先调用assist删除磁盘文件删除成功后再删除数据库记录
JSONObject jsonObject = assistRESTfulUtils.deleteFiles(mediaServerItem, filePathList);
if (jsonObject != null && jsonObject.getInteger("code") == 0 && jsonObject.getInteger("data") > 0) {
result += jsonObject.getInteger("data");
cloudRecordServiceMapper.deleteByFileList(filePathList, mediaServerItem.getId());
List<Integer> cloudRecordItemIdList = new ArrayList<>();
for (CloudRecordItem cloudRecordItem : cloudRecordItemList) {
String date = new File(cloudRecordItem.getFilePath()).getParentFile().getName();
JSONObject jsonObject = zlmresTfulUtils.deleteRecordDirectory(mediaServerItem, cloudRecordItem.getApp(),
cloudRecordItem.getStream(), date, cloudRecordItem.getFileName());
if (jsonObject.getInteger("code") == 0) {
cloudRecordItemIdList.add(cloudRecordItem.getId());
}else {
logger.warn("[录像文件定时清理] 删除磁盘文件错误: {}", jsonObject);
}
}
if (cloudRecordItemIdList.isEmpty()) {
continue;
}
cloudRecordServiceMapper.deleteList(cloudRecordItemIdList, mediaServerItem.getId());
result += cloudRecordItemIdList.size();
}
}
logger.info("[录像文件定时清理] 共清理{}个过期录像文件", result);

View File

@ -268,14 +268,4 @@ public class AssistRESTfulUtils {
return sendGet(mediaServerItem, "api/record/file/download/task/list", param, null);
}
public JSONObject addCollect(MediaServerItem mediaServerItem, JSONObject jsonObject) {
return sendPost(mediaServerItem, "api/record/file/collection/add", jsonObject, null, 30);
}
public JSONObject deleteFiles(MediaServerItem mediaServerItem, List<String> filePathList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("filePathList", filePathList);
return sendPost(mediaServerItem, "api/record/file/delete", jsonObject, null, 15*60);
}
}

View File

@ -25,8 +25,6 @@ public class ZLMRESTfulUtils {
private OkHttpClient client;
public interface RequestCallback{
void run(JSONObject response);
}
@ -398,4 +396,14 @@ public class ZLMRESTfulUtils {
param.put("stream_id", streamId);
return sendPost(mediaServerItem, "updateRtpServerSSRC",param, null);
}
public JSONObject deleteRecordDirectory(MediaServerItem mediaServerItem, String app, String stream, String date, String fileName) {
Map<String, Object> param = new HashMap<>(1);
param.put("vhost", "__defaultVhost__");
param.put("app", app);
param.put("stream", stream);
param.put("period", date);
param.put("name", fileName);
return sendPost(mediaServerItem, "deleteRecordDirectory",param, null);
}
}

View File

@ -82,25 +82,28 @@ public interface CloudRecordServiceMapper {
int updateCollectList(@Param("collect") boolean collect, List<CloudRecordItem> cloudRecordItemList);
@Delete(" <script>" +
"delete from wvp_cloud_record where media_server_id=#{mediaServerId} file_path in " +
"delete from wvp_cloud_record where media_server_id=#{mediaServerId} and file_path in " +
" <foreach collection='filePathList' item='item' open='(' separator=',' close=')' > #{item}</foreach>" +
" </script>")
void deleteByFileList(List<String> filePathList, @Param("mediaServerId") String mediaServerId);
@Select(" <script>" +
"select file_path" +
"select *" +
" from wvp_cloud_record " +
" where collect = false " +
" <if test= 'endTimeStamp != null '> and start_time &lt;= #{endTimeStamp}</if>" +
" <if test= 'callId != null '> and call_id = #{callId}</if>" +
" <if test= 'mediaServerId != null ' > and media_server_id = #{mediaServerId} </if>" +
" where end_time &lt;= #{endTimeStamp} and media_server_id = #{mediaServerId} " +
" </script>")
List<String> queryRecordFilePathListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId);
List<CloudRecordItem> queryRecordListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId);
@Update(" <script>" +
"update wvp_cloud_record set collect = #{collect} where id = #{recordId} " +
" </script>")
int changeCollectById(@Param("collect") boolean collect, @Param("recordId") Integer recordId);
@Delete(" <script>" +
"delete from wvp_cloud_record where media_server_id=#{mediaServerId} and id in " +
" <foreach collection='cloudRecordItemIdList' item='item' open='(' separator=',' close=')' > #{item}</foreach>" +
" </script>")
int deleteList(List<Integer> cloudRecordItemIdList, @Param("mediaServerId") String mediaServerId);
}