Exemplo n.º 1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_upload_Tick(object sender, EventArgs e)
        {
            if (isupload)
            {
                if (!is_upload_copying)
                {
                    if (ftp_client_list != null && ftp_client_list.Count >= limit_upload_count)
                    {
                        BlockingCollection <int> list = new BlockingCollection <int>();
                        int index = 0;
                        foreach (var dic in diction_upload)
                        {
                            if (index >= limit_upload_count)
                            {
                                break;
                            }
                            index++;
                            list.Add(dic.Key);
                        }
                        is_upload_copying = true;
                        Task.Run(() =>
                        {
                            System.Threading.Tasks.Parallel.ForEach(list, s =>
                            {
                                Entity.Pe_video_list pe_Video = diction_upload[s];
                                try
                                {
                                    int p_index                  = list.ToList().IndexOf(s);
                                    string filefullname          = pe_Video.saveposition.Split('/').Last();
                                    Entity.media_info media_Info = new Entity.media_info(filefullname, "");
                                    string uploadPath            = ftp_server_mainpath + media_Info.remote_path;
                                    List <string> upload_list    = new List <string>();
                                    string filepath              = pe_Video.macposition;

                                    upload_list.Add(filepath);
                                    if ("MP4|JPG".Contains(pe_Video.filetype))
                                    {
                                        string thm_file = filepath.Substring(0, filepath.LastIndexOf('.')) + ".THM.JPG";
                                        upload_list.Add(thm_file);
                                    }
                                    if (System.IO.File.Exists(filepath))
                                    {
                                        this.Invoke(new delegate_updatetxt(update_upload_txt), $"正在开始上传文件[{filepath}]");
                                        FluentFTP.FtpStatus status = ftp_client_list[p_index].UploadFiles(upload_list, uploadPath);
                                        this.Invoke(new delegate_updatetxt(update_upload_txt), $"上传文件[{status.ToString()}]");
                                        if (status.ToString() == FluentFTP.FtpStatus.Success.ToString())
                                        {
                                            this.Invoke(new delegate_updatetxt(update_upload_txt), $"上传文件成功[{filepath}]");
                                            var model          = new Entity.Pe_video_list();
                                            model.playposition = media_Info.play_position;
                                            model.is_flg       = (int)HANGE.Media.Henum.HEnum.media_status.成功;
                                            bool update        = DBContext.DB.Update <Entity.Pe_video_list>(model, m => m.id == pe_Video.id) > 0;
                                            if (update)
                                            {
                                                //try
                                                //{
                                                //    bool result = diction_upload.TryRemove(s, out pe_Video);
                                                //    if (!result)
                                                //    {
                                                //        NLog.LogManager.GetCurrentClassLogger().Error(new Exception($"移除key[{s}]失败"));
                                                //    }
                                                //}
                                                //catch (Exception ex)
                                                //{
                                                //    NLog.LogManager.GetCurrentClassLogger().Error(ex);
                                                //}
                                            }
                                        }
                                    }
                                    else
                                    {
                                        this.Invoke(new delegate_updatetxt(update_upload_txt), $"文件[{filepath}]未找到请排查相关路径下文件");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    NLog.LogManager.GetCurrentClassLogger().Error(ex);
                                }
                                finally
                                {
                                    try
                                    {
                                        bool result = diction_upload.TryRemove(s, out pe_Video);
                                        if (!result)
                                        {
                                            NLog.LogManager.GetCurrentClassLogger().Error(new Exception($"移除key[{s}]失败"));
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        NLog.LogManager.GetCurrentClassLogger().Error(ex);
                                    }
                                }
                            });
                            is_upload_copying = false;
                        });
                    }
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void timer2_Tick(object sender, EventArgs e)
 {
     if (is_load)
     {
         if (!is_copying)
         {
             if (ftp_client_list != null && ftp_client_list.Count >= limit_count)
             {
                 BlockingCollection <string> list = new BlockingCollection <string>();
                 int index = 0;
                 foreach (var dic in diction)
                 {
                     if (index >= limit_count)
                     {
                         break;
                     }
                     index++;
                     list.Add(dic.Key);
                 }
                 is_copying = true;
                 Task.Run(() =>
                 {
                     System.Threading.Tasks.Parallel.ForEach(list, s =>
                     {
                         int p_index     = list.ToList().IndexOf(s);
                         string filepath = diction[s];
                         if (!string.IsNullOrEmpty(filepath))
                         {
                             if (!IsFileInUse(filepath))
                             {
                                 this.Invoke(new delegate_update(update_txt), $"正在开始上传文件[{s}]");
                                 FluentFTP.FtpStatus status = ftp_client_list[p_index].UploadFile(filepath, uploadPath);
                                 this.Invoke(new delegate_update(update_txt), $"上传文件[{status.ToString()}]");
                                 if (status.ToString() == FluentFTP.FtpStatus.Success.ToString())
                                 {
                                     this.Invoke(new delegate_update(update_txt), $"正在开始删除文件[{s}]");
                                     (new System.IO.FileInfo(filepath)).Delete();
                                     this.Invoke(new delegate_update(update_txt), $"删除文件成功[{s}]");
                                     try
                                     {
                                         string value = "";
                                         bool result  = diction.TryRemove(s, out value);
                                         if (!result)
                                         {
                                             NLog.LogManager.GetCurrentClassLogger().Error(new Exception($"移除key[{s}]失败"));
                                         }
                                     }
                                     catch (Exception ex)
                                     {
                                         NLog.LogManager.GetCurrentClassLogger().Error(ex);
                                     }
                                 }
                             }
                         }
                     });
                     is_copying = false;
                 });
             }
         }
     }
 }