Ejemplo n.º 1
0
        public static KafKaLogic GetInstance()
        {
            // 如果类的实例不存在则创建,否则直接返回
            if (KafKaInstance == null)
            {
                KafKaInstance = new KafKaLogic();
            }

            return(KafKaInstance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 订阅总署文件上传消息
        /// </summary>
        /// <param name="kafkaData"></param>
        public void PullUploadMsg(KafKaContract kafkaData)
        {
            string KafkaMsgTopic = ConfigHelper.ReadConfigByName("KafkaMsgTopic");

            LogHelper.logInfo("插入文件:" + kafkaData.Msg);
            var result = false;

            try
            {
                switch (kafkaData.MsgCode)
                {
                case KafkaMsgCodeEnum.AddList:
                    var insertListData = SerializeHelper.deserializeToObject <List <UploadContract> >(kafkaData.Msg);
                    result = InsertESList(insertListData);
                    break;

                case KafkaMsgCodeEnum.Add:
                    var insertData = SerializeHelper.deserializeToObject <UploadContract>(kafkaData.Msg);
                    result = InsertES(insertData);
                    break;

                case KafkaMsgCodeEnum.Update:
                    var updateData = SerializeHelper.deserializeToObject <UploadContract>(kafkaData.Msg);
                    result = UpdateES(updateData);
                    break;

                default:
                    //msgCode错误
                    break;
                }
            }
            catch (Exception e)
            {
                result = false;
            }

            if (result) //入库成功
            {
                if (kafkaData.MsgCode == KafkaMsgCodeEnum.AddList)
                {
                    kafkaData.MsgCode = KafkaMsgCodeEnum.InsertListSuccess;
                }
                else
                {
                    kafkaData.MsgCode = KafkaMsgCodeEnum.InsertSuccess;
                }
            }
            else //入库失败
            {
                kafkaData.MsgCode = KafkaMsgCodeEnum.InsertError;
            }

            KafKaLogic.GetInstance().Push(kafkaData, KafkaMsgTopic);
        }
Ejemplo n.º 3
0
 public void BigVideo()
 {
     try
     {
         AliyunOSSHepler oss = new AliyunOSSHepler();
         //获取文件所在路径
         string        workDir = AppDomain.CurrentDomain.BaseDirectory + ConfigHelper.ReadConfigByName("UploadFilePath");
         DirectoryInfo folder  = new DirectoryInfo(workDir);
         //回写数据库的上云路径
         string osspath = "";
         //本地全部上云
         FileInfo[] list = folder.GetFiles("*");
         LogHelper.logError("获取大文件路径:" + workDir);
         LogHelper.logError("获取大文件数量:" + list.Length.ToString());
         for (int i = 0; i < list.Length; i++)
         {
             if (!CommonDictionary.GetInstance().KafkaIsOnline)
             {
                 continue;
             }
             bool returnType = oss.UploadFiles(workDir, list[i].Name, ref osspath);
             LogHelper.logError("上云路径:" + osspath);
             if (returnType)
             {
                 string         fileid   = list[i].Name.Split(".")[0];
                 UploadContract contract = new UploadContract();
                 contract.FileId      = Guid.Parse(fileid);
                 contract.IsUpload    = true;
                 contract.Url         = osspath;
                 contract.NewFileName = list[i].Name;
                 KafKaContract kafka = new KafKaContract();
                 kafka.MsgId   = fileid;
                 kafka.MsgCode = KafkaMsgCodeEnum.Update;
                 kafka.Msg     = SerializeHelper.serializeToString(contract);
                 KafKaLogic.GetInstance().Push(kafka, KafkaTopic);
             }
         }
     }
     catch (Exception e)
     {
         LogHelper.logError("上云异常:" + e.Message);
     }
 }
        public void FailureVideo()
        {
            try
            {
                //获取文件所在路径
                string workDir = AppDomain.CurrentDomain.BaseDirectory + ConfigHelper.ReadConfigByName("FailurePath");
                if (System.IO.Directory.Exists(workDir))
                {
                    var list = this._dbContext.FileDB.FindAll <FileDbContract>();
                    LogHelper.logError("读取失败json");
                    foreach (var entity in list)
                    {
                        if (!CommonDictionary.GetInstance().KafkaIsOnline)
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(entity.Id))
                        {
                            LogHelper.logInfo("读取json文件:" + entity.Id);
                            DirectoryInfo folder = new DirectoryInfo(workDir);
                            //回写数据库的上云路径
                            string osspath = "";
                            //本地全部上云
                            FileInfo[] fileList = folder.GetFiles(entity.Id + ".*");
                            for (int i = 0; i < fileList.Length; i++)
                            {
                                UploadContract contract  = SerializeHelper.deserializeToObject <UploadContract>(entity.data);
                                bool           imgupload = true;
                                string         imgurl    = "";

                                if (contract.IsUpload == true)
                                {
                                    KafKaContract kafka = new KafKaContract();
                                    kafka.MsgId   = entity.Id;
                                    kafka.MsgCode = KafkaMsgCodeEnum.Add;
                                    kafka.Msg     = SerializeHelper.serializeToString(contract);
                                    KafKaLogic.GetInstance().Push(kafka, KafkaTopic);
                                    continue;
                                }

                                if (CommonDictionary.GetInstance().VideoType.Count(d => d.ToLower() == contract.FileType.ToLower()) > 0)
                                {
                                    imgurl    = AliyunOSSHepler.GetInstance().GetPicFromVideo(workDir + fileList[i].Name, imgPath + Path.DirectorySeparatorChar + contract.FileId + ".jpg", "1");
                                    imgupload = AliyunOSSHepler.GetInstance().UploadFiles(imgPath, contract.FileId + ".jpg", ref imgurl);
                                }

                                if (imgupload)
                                {
                                    bool returnType = AliyunOSSHepler.GetInstance().UploadFiles(workDir, fileList[i].Name, ref osspath);

                                    if (returnType)
                                    {
                                        LogHelper.logInfo("上云封面地址:" + imgurl + "_______上云视频路径:" + osspath);
                                        try
                                        {
                                            contract.IsUpload     = true;
                                            contract.Url          = osspath;
                                            contract.ThumbnailUrl = imgurl;
                                            entity.data           = SerializeHelper.serializeToString(contract);
                                            this._dbContext.FileDB.Save <FileDbContract>(contract.FileId.ToString(), entity);
                                            KafKaContract kafka = new KafKaContract();
                                            kafka.MsgId   = entity.Id;
                                            kafka.MsgCode = KafkaMsgCodeEnum.Add;
                                            kafka.Msg     = SerializeHelper.serializeToString(contract);
                                            KafKaLogic.GetInstance().Push(kafka, KafkaTopic);
                                        }
                                        catch (Exception e)
                                        {
                                            LogHelper.logError("解析json数据推送kafka异常:文件名-" + fileList[i].Name + "__" + e.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    LogHelper.logError("失败任务无数据,本次不执行");
                }
            }
            catch (Exception e)
            {
                LogHelper.logError("失败文件推送异常:" + e.Message + "__" + e.ToString());
            }
        }