Exemplo n.º 1
0
 public static void RemoveFileTask(string md5)
 {
     if (!String.IsNullOrEmpty(md5))
     {
         FileTaskDAL.GetInstance().Delete(md5);
     }
 }
Exemplo n.º 2
0
        public static FileTaskInfo GetFileTask(string md5)
        {
            if (String.IsNullOrEmpty(md5))
            {
                return(null);
            }

            return(FileTaskDAL.GetInstance().SelectFileTask(md5).Fill <FileTaskInfo>());
        }
Exemplo n.º 3
0
        public static void CreateTask(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                return;
            }

            //临时文件不提交
            if (filePath.IndexOf(".tmp.") >= 0)
            {
                return;
            }

            //文件已不存在
            var fi = new System.IO.FileInfo(filePath);

            if (!fi.Exists)
            {
                return;
            }

            //分辨率大于250*250才提交
            if (!ResolutionRatioTest(filePath))
            {
                return;
            }

            //提交任务
            var md5 = GetMD5HashFromFile(filePath);

            if (!String.IsNullOrEmpty(md5))
            {
                if (!FileTaskDAL.GetInstance().IsExistFileTask(md5))
                {
                    FileTaskDAL.GetInstance().Insert(new FileTaskInfo()
                    {
                        Md5            = md5,
                        FileFullPath   = filePath,
                        IsProcessed    = false,
                        Confidence     = 0,
                        Hot_score      = 0,
                        Porn_score     = 0,
                        Normal_score   = 0,
                        Result         = AppraiseResult.Unknow,
                        FileUploadTime = fi.CreationTime,
                        CorpId         = GetCorpIdByFilePath(filePath)
                    });
                }
            }
        }
Exemplo n.º 4
0
        public static void UpdateCorpId()
        {
            Task.Factory.StartNew(() => {
                while (true)
                {
                    try
                    {
                        var tasks = FileTaskDAL.GetInstance()
                                    .SelectFileTaskWhereCorpIdIsNull(50).ToList <FileTaskInfo>();

                        if (tasks.Count() == 0)
                        {
                            Console.WriteLine("UpdateCorpId Done");
                            return;
                        }
                        else
                        {
                            var buf = new StringBuilder();
                            foreach (var t in tasks)
                            {
                                var corpid = GetCorpIdByFilePath(t.FileFullPath);
                                if (corpid.HasValue)
                                {
                                    buf.AppendLine("update sm_fileinfo set CorpId=" + corpid.Value + " where Id=" + t.Id + ";");
                                }
                                else
                                {
                                    buf.AppendLine("update sm_fileinfo set CorpId=-1 where Id=" + t.Id + ";");
                                }
                            }

                            FileTaskDAL.GetInstance().DbInstance
                            .ExecuteNonQuery(System.Data.CommandType.Text, buf.ToString());

                            Console.WriteLine("UpdateCorpId Run");
                        }
                    }
                    catch (Exception ex)
                    {
                        FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                    }

                    System.Threading.Thread.Sleep(2000);
                }
            });
        }
Exemplo n.º 5
0
        public static void Process(IEnumerable <ResponseResultItem> resp)
        {
            if (resp == null || resp.Count() == 0)
            {
                return;
            }

            Task.Factory.StartNew(() => {
                List <string> exceptionMd5s  = new List <string>();
                List <long> exceptionTaskids = new List <long>();

                var buf = new StringBuilder();
                buf.AppendLine("处理结果" + resp.Count());

                //处理相应
                foreach (var item in resp)
                {
                    long?taskId    = null;
                    string taskMd5 = null;

                    try
                    {
                        taskId = TaskIdMaps.GetId(item.url);

                        if (!taskId.HasValue)
                        {
                            taskMd5 = getMd5FromResultUrl(item.url);
                        }

                        if (item.code == 0)//识别成功
                        {
                            if (taskId.HasValue)
                            {
                                FileTaskDAL.GetInstance().UpdateTaskProcessState3(taskId.Value, true, false, 0, "success",
                                                                                  (AppraiseResult)item.data.result, item.data.confidence, item.data.hot_score, item.data.normal_score, item.data.porn_score);

                                buf.AppendLine("task:" + taskId.Value + " code:" + item.code);
                            }
                            else
                            {
                                FileTaskDAL.GetInstance().UpdateTaskProcessState3(taskMd5, true, false, 0, "success",
                                                                                  (AppraiseResult)item.data.result, item.data.confidence, item.data.hot_score, item.data.normal_score, item.data.porn_score);

                                buf.AppendLine("task:" + taskMd5 + " code:" + item.code);
                            }
                        }
                        else//识别错误
                        {
                            if (taskId.HasValue)
                            {
                                //错误有定义
                                if (_errorCode.ContainsKey(item.code))
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskId.Value, false, false, item.code, _errorCode[item.code]);
                                }
                                else//错误未定义
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskId.Value, false, false, -1, "不能识别的错误");
                                }

                                buf.AppendLine("task:" + taskId.Value + " code:" + item.code);
                            }
                            else
                            {
                                //错误有定义
                                if (_errorCode.ContainsKey(item.code))
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskMd5, false, false, item.code, _errorCode[item.code]);
                                }
                                else//错误未定义
                                {
                                    FileTaskDAL.GetInstance().UpdateTaskProcessState2(taskMd5, false, false, -1, "不能识别的错误");
                                }

                                buf.AppendLine("task:" + taskMd5 + " code:" + item.code);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //将产生异常的ID记录下来

                        if (taskId.HasValue)
                        {
                            exceptionTaskids.Add(taskId.Value);
                            buf.AppendLine("task exception:" + taskId.Value + " code:" + item.code);
                        }
                        if (!String.IsNullOrEmpty(taskMd5))
                        {
                            exceptionMd5s.Add(taskMd5);
                            buf.AppendLine("task exception:" + taskMd5 + " code:" + item.code);
                        }

                        FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                    }
                    finally
                    {
                        try
                        {
                            //使用完成后从缓存中移除ID映射
                            TaskIdMaps.RemoveId(item.url);
                        }
                        catch (Exception ex)
                        {
                            FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                        }
                    }
                }

                if (LogState.Enabled)
                {
                    FaceHand.Common.Core.WxLogProvider.Write(buf.ToString(), "TaskResult_Process");
                }

                //更新异常任务的状态
                try
                {
                    if (exceptionMd5s != null && exceptionMd5s.Count() > 0)
                    {
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(exceptionMd5s);
                    }
                    if (exceptionTaskids != null && exceptionTaskids.Count() > 0)
                    {
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(exceptionTaskids);
                    }
                }
                catch (Exception ex)
                {
                    FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                }
            });
        }
Exemplo n.º 6
0
        public static Task Process(IEnumerable <FileTaskInfo> tasks)
        {
            return(Task.Factory.StartNew(() => {
                if (tasks == null || tasks.Count() == 0)
                {
                    return;
                }

                try
                {
                    var urls = new List <string>();
                    var taskIds = new List <long>();
                    foreach (var task in tasks)
                    {
                        if (String.IsNullOrEmpty(task.FileFullPath) || task.IsProcessed)
                        {
                            continue;
                        }

                        if (!System.IO.File.Exists(task.FileFullPath))
                        {
                            //文件已不存在,删除任务
                            FileTask.RemoveFileTask(task.Id);
                            continue;
                        }

                        var url = replaceFileUrl(task.FileFullPath);
                        TaskIdMaps.Set(url, task.Id);//将ID映射记录下来

                        urls.Add(url);
                        taskIds.Add(task.Id);
                    }

                    //再次确认哪些URL要处理
                    if (urls.Count == 0)
                    {
                        return;
                    }

                    var reqdata = new RequestBody()
                    {
                        appid = CLOUD_appid, bucket = CLOUD_bucket, url_list = urls
                    };
                    var str_reqdata = Newtonsoft.Json.JsonConvert.SerializeObject(reqdata);
                    var byt_reqdata = Encoding.UTF8.GetBytes(str_reqdata);
                    var sign_reqdata = GetSign();

                    var client = (HttpWebRequest)WebRequest.Create(CLOUD_url);
                    client.Headers.Add("Authorization", sign_reqdata);
                    client.ContentType = "application/json";
                    client.ContentLength = byt_reqdata.Length;
                    client.Timeout = 20000;//20s
                    client.Method = "POST";
                    //写数据到请求
                    using (var reqStream = client.GetRequestStream())
                    {
                        reqStream.Write(byt_reqdata, 0, byt_reqdata.Length);
                    }

                    //发送请求前记录请求日志
                    if (LogState.Enabled)
                    {
                        string log = String.Format("{0} {1} {2}\n{3}", DateTime.Now, tasks.Count(), sign_reqdata, str_reqdata);
                        FaceHand.Common.Core.WxLogProvider.Write(log, "SexyFilter_Process");
                    }

                    //发送请求
                    try
                    {
                        HttpWebResponse resp = client.GetResponse() as HttpWebResponse;
                        using (var respStream = resp.GetResponseStream())
                        {
                            var sr = new System.IO.StreamReader(respStream, Encoding.UTF8);
                            var resp_data = Newtonsoft.Json.JsonConvert.DeserializeObject <ResponseBody>(sr.ReadToEnd());

                            //处理结果
                            TaskResult.Process(resp_data.result_list);
                        }
                    }
                    catch (WebException ex)
                    {
                        HttpWebResponse resp = ex.Response as HttpWebResponse;

                        var buf = new StringBuilder();
                        buf.AppendLine("Service Interface Call Exception " + ((int)resp.StatusCode).ToString() + " " + resp.StatusDescription);
                        buf.AppendLine("Url:" + CLOUD_url + " Sign:" + sign_reqdata);


                        //在次检测文件的有效性,有问题的直接删除任务
                        foreach (var task in tasks)
                        {
                            if (!System.IO.File.Exists(task.FileFullPath))
                            {
                                FileTask.RemoveFileTask(task.Id);
                            }

                            buf.AppendLine(String.Format("{0} {1}", task.Id, task.FileFullPath));
                        }

                        //再次更新为可处理的状态
                        FileTaskDAL.GetInstance().UpdateTaskProcessState4(taskIds);

                        if (LogState.Enabled)
                        {
                            FaceHand.Common.Core.WxLogProvider.Write(buf.ToString(), "SexyFilter_Process");
                        }
                    }
                }
                catch (Exception ex)
                {
                    FaceHand.Common.Util.SystemLoger.Current.Write(ex);
                }
            }));
        }
Exemplo n.º 7
0
 public static void UpdateTaskProcessState(IEnumerable <long> taskIds)
 {
     FileTaskDAL.GetInstance().UpdateTaskProcessState(taskIds);
 }
Exemplo n.º 8
0
 public static IEnumerable <FileTaskInfo> GetFileTaskWaitPorcessing(int num)
 {
     return(FileTaskDAL.GetInstance().SelectFileTaskWaitPorcessing(num).ToList <FileTaskInfo>());
 }
Exemplo n.º 9
0
 public static FileTaskInfo GetFileTask(long id)
 {
     return(FileTaskDAL.GetInstance().SelectFileTask(id).Fill <FileTaskInfo>());
 }
Exemplo n.º 10
0
 public static void RemoveFileTask(long id)
 {
     FileTaskDAL.GetInstance().Delete(id);
 }