public static bool Add(string filePath) { if (!File.Exists(filePath)) { return(false); } FileInfo f = new FileInfo(filePath); FileQueue q = new FileQueue(); q.ID = Guid.NewGuid().ToString(); q.DateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); q.FileName = f.Name; q.FilePath = f.FullName; q.FileLen = f.Length; q.OverPercent = 0; q.Status = "未开启"; q.ReSetSendQueue(); q.SendListLock = new object(); lock (fileQueueListLock) { if (mList == null) { mList = new List <FileQueue>(); } mList.Add(q); } return(true); }
static void Main(string[] args) { Log($"程序启动,版本:{Module.Version}"); ConfigHelper.ReadConfig(); FileQueue.ReadLocalList(); if (args == null || args.Length == 0) { int count = FileQueue.Count(); if (count == 0) { Log("没有待传文件"); return; } else { Log("继续传本地缓存文件队列..."); Run(); } } else { string str0 = args[0]; if (str0 == "-e") { if (args.Length < 3) { Log("请输入 inputfile 和 outputfile"); return; } else { string input = args[1]; string output = args[2]; double rate = 256000; if (args.Length >= 4) { rate = double.Parse(args[3]); } EncodeFile(input, output, rate); return; } } else { string path = args[0]; Log($"地址为:{path}"); Run(path); } } Console.ReadLine(); }
private static async Task UploadWork(FileQueue file) { file.Status = "上传中"; if (file.SendListLock == null) { file.SendListLock = new object(); } bool isComplete = true; Program.Log($"文件{file.FileName}上传中..."); for (int i = 0; i < file.SendList.Count; i++) { SendQueue itm = file.SendList[i]; if (itm.IsSendOver) { continue; } itm.Status = "上传中"; file.SendList[i] = itm; FileQueue.Save(); Program.Log($"-->Part {i + 1} 正在上传"); bool flag = await UploadBlock(itm); itm.IsSendOver = flag; if (itm.IsSendOver) { Program.Log($" -->[ok]Part {i + 1} 上传成功,总计 {file.SendList.Count}"); itm.Status = "已上传"; } else { isComplete = false; Program.Log($" -->[fail]Part {i+1} 上传失败,等待下次重传"); } file.SendList[i] = itm; file.CurrentIndex = i + 1; FileQueue.Save(); } if (isComplete) { Program.Log($"文件{file.FileName}上传成功!"); FileQueue.RemoveFirst(); FileQueue.Save(); } else { Program.Log($"文件{file.FileName}部分块上传失败,等待重传"); } }
private static async void LoopWork() { while (true) { Program.Log($">>检查待传输队列,文件数量{FileQueue.Count()}<<"); FileQueue file = FileQueue.GetOne(); if (file != null) { await UploadWork(file); } else { Environment.Exit(0); } } }
static void Run(string path = "") { if (!string.IsNullOrEmpty(path)) { if (Directory.Exists(path)) { Log("检测到文件夹,读取文件中..."); DirectoryInfo dinfo = new DirectoryInfo(path); foreach (var f in dinfo.GetFiles()) { FileQueue.Add(f.FullName); } FileQueue.Save(); } else { if (!File.Exists(path)) { Log("文件不存在"); } else { Log("新增文件..."); FileQueue.Add(path); FileQueue.Save(); } } } try { LoopUploader.StartWork(); } catch (Exception e) { Log(e.ToString()); } }