/// <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; }); } } } }
private void timer_bak_Tick(object sender, EventArgs e) { if (isload) { if (!is_copying) { if (server_Config != null) { int parall_num = server_Config.upload_parallel; string bak_path = server_Config.backup_path; BlockingCollection <string> list = new BlockingCollection <string>(); int index = 0; foreach (var dic in diction) { if (index > parall_num) { break; } index++; list.Add(dic.Key); } is_copying = true; Task.Run(() => { System.Threading.Tasks.Parallel.ForEach(list, s => { media_collect_class collect_Class = diction[s]; string filepath = collect_Class.full_path; Entity.Pe_device device = collect_Class.device; if (!string.IsNullOrEmpty(filepath)) { if (!FileUtils.IsFileInUse(filepath)) { try { Entity.media_info media_Info = new Entity.media_info(s, server_Config.backup_path); LogManager.Info(new Entity.media_option_log() { client_ip = server_Config.client_ip, device_name = media_Info.host_body, media_filename = s, media_type = media_Info.file_type, option_info = $"开始复制文件:[{s}]", option_type = (int)HANGE.Media.Henum.HEnum.media_status.开始备份, user_code = media_Info.host_code }); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath); long file_size = fileInfo.Length; #region 新建上传实体类 Entity.Pe_video_list video_list_model = new Entity.Pe_video_list() { filename = media_Info.host_code + "@" + media_Info.file_name, bfilename = device.Hostname + "@" + media_Info.file_name, createdate = DateTime.ParseExact(media_Info.file_name, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss"), filelen = file_size.ToString(), playfilelen = file_size.ToString(), filetype = media_Info.file_type, hostname = device.Hostname, hostbody = device.Hostbody, hostcode = device.Hostcode, danwei = device.Danwei, onlyread = media_Info.is_imp ? 1 : 0, caserank = media_Info.is_imp ? "重要" : "", casetopic = "", note = "", serverurl = server_Config.server_name, servername = server_Config.client_ip, thumb = media_Info.thumb, saveposition = media_Info.save_position, playposition = "", macposition = media_Info.local_positon, creater = server_Config.client_ip, playtime = 1000, resolution = "1920*1080", filetotnum = 1, filetottime = 1000, is_flg = (int)HANGE.Media.Henum.HEnum.media_status.备份成功 }; var isexsit = HANGE.Media.Mysql.DBContext.DB.From <Entity.Pe_video_list>().Where(p => p.filename == video_list_model.filename).First(); if (isexsit != null) { video_list_model.id = isexsit.id; HANGE.Media.Mysql.DBContext.DB.Update <Entity.Pe_video_list>(video_list_model); } else { HANGE.Media.Mysql.DBContext.DB.Insert <Entity.Pe_video_list>(video_list_model); } #endregion FileUtils.CopyOrRemoveFileFullPath(media_Info.local_positon, fileInfo); LogManager.Info(new Entity.media_option_log() { client_ip = server_Config.client_ip, device_name = media_Info.host_body, media_filename = s, media_type = media_Info.file_type, option_info = $"备份文件:[{s}]成功", option_type = (int)HANGE.Media.Henum.HEnum.media_status.备份成功, user_code = media_Info.host_code }); } catch (Exception ex) { LogManager.Error(ex); } } } try { bool result = diction.TryRemove(s, out collect_Class); if (!result) { LogManager.Error(new Exception($"移除key[{s}]失败")); } } catch (Exception ex) { LogManager.Error(ex); } }); is_copying = false; }); } } } }