Exemplo n.º 1
0
        private void deleteFileOneByOne(decimal videoSize, StreamDvrPlan sdp)
        {
            long deleteSize = 0;
            List <OrderByStruct> orderBy = new List <OrderByStruct>();

            orderBy.Add(new OrderByStruct()
            {
                FieldName  = "starttime",
                OrderByDir = OrderByDir.ASC,
            });
            ReqGetDvrVideo rgdv = new ReqGetDvrVideo()
            {
                App            = sdp.App,
                DeviceId       = sdp.DeviceId,
                EndTime        = null,
                IncludeDeleted = false,
                OrderBy        = orderBy,
                PageIndex      = 1,
                PageSize       = 10,
                StartTime      = null,
                Stream         = sdp.Stream,
                VhostDomain    = sdp.VhostDomain,
            };

            while (videoSize - deleteSize > sdp.LimitSpace)
            {
                DvrVideoResponseList videoList = DvrPlanApis.GetDvrVideoList(rgdv, out ResponseStruct rs);
                if (videoList != null && videoList.DvrVideoList != null && videoList.DvrVideoList.Count > 0)
                {
                    lock (SrsManageCommon.Common.LockDbObjForDvrVideo)
                    {
                        OrmService.Db.Transaction(() =>
                        {
                            foreach (var ret in videoList.DvrVideoList)
                            {
                                if (ret != null)
                                {
                                    if (File.Exists(ret.VideoPath))
                                    {
                                        File.Delete(ret.VideoPath);
                                        deleteSize += (long)ret.FileSize !;
                                        LogWriter.WriteLog("删除录制文件", ret.VideoPath !);

                                        Thread.Sleep(20);
                                    }

                                    OrmService.Db.Update <DvrVideo>().Set(x => x.UpdateTime, DateTime.Now)
                                    .Set(x => x.Deleted, true)
                                    .Where(x => x.Id == ret !.Id).ExecuteAffrows();
                                }
                                if ((videoSize - deleteSize) < sdp.LimitSpace)
                                {
                                    break;
                                }
                            }
                        });
                    }
                }
            }
        }
        /// <summary>
        /// 文件一个一个删除
        /// </summary>
        /// <param name="videoSize"></param>
        /// <param name="sdp"></param>
        private void deleteFileOneByOne(decimal videoSize, StreamDvrPlan sdp)
        {
            var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(sdp.MediaServerId));

            if (mediaServer == null || !mediaServer.IsRunning)
            {
                return;
            }

            long deleteSize = 0;
            List <OrderByStruct> orderBy = new List <OrderByStruct>();

            orderBy.Add(new OrderByStruct()
            {
                FieldName  = "starttime",
                OrderByDir = OrderByDir.ASC,
            });
            ReqGetDvrVideo rgdv = new ReqGetDvrVideo()
            {
                CameraId       = sdp.CameraId,
                MediaServerId  = sdp.MediaServerId,
                EndTime        = null,
                IncludeDeleted = false,
                OrderBy        = orderBy,
                PageIndex      = 1,
                PageSize       = 10,
                StartTime      = null,
            };

            while (videoSize - deleteSize > sdp.LimitSpace)
            {
                DvrVideoResponseList videoList = MediaServerApis.GetDvrVideoList(rgdv, out ResponseStruct rs);
                if (videoList != null && videoList.DvrVideoList != null && videoList.DvrVideoList.Count > 0)
                {
                    OrmService.Db.Transaction(() =>
                    {
                        foreach (var ret in videoList.DvrVideoList)
                        {
                            if (ret != null)
                            {
                                if (mediaServer.CheckFileExists(ret.VideoPath, out _))
                                {
                                    mediaServer.DeleteFile(ret.VideoPath, out _);
                                    deleteSize += (long)ret.FileSize !;
                                    OrmService.Db.Update <RecordFile>().Set(x => x.UpdateTime, DateTime.Now)
                                    .Set(x => x.Deleted, true)
                                    .Where(x => x.Id == ret !.Id).ExecuteAffrows();
                                    LogWriter.WriteLog("删除录制文件", ret.VideoPath !);
                                    Thread.Sleep(20);
                                }
                            }

                            if ((videoSize - deleteSize) < sdp.LimitSpace)
                            {
                                break;
                            }
                        }
                    });
                }
            }
        }