/// <summary> /// 初始化上传信息 /// </summary> /// <param name="downIofo">下载信息</param> public UploadInfo(DownIofoData downIofo) { try { streamerName = downIofo.主播名称; streamTitle = downIofo.标题; srcFile = downIofo.文件保存路径; fileName = System.IO.Path.GetFileName(srcFile); fileSize = (double)new FileInfo(srcFile).Length; remotePath = $"{downIofo.主播名称}_{downIofo.房间_频道号}/{MMPU.Unix转换为DateTime(downIofo.开始时间.ToString()).ToString("yyyyMMdd")}_{downIofo.标题}/"; type = null; retries = 0; } catch { InfoLog.InfoPrintf($"创建{srcFile}上传任务失败,无法获取文件信息", InfoLog.InfoClass.系统错误信息); throw new CreateUploadTaskFailure("fail to ctreate upload task");//处理获取文件名、文件大小的错误 } }
/// <summary> /// 上传文件 /// </summary> /// <param name="do">委托,传入当前上传目标的特定上传函数</param> private void Upload(doUpload @do) { uploadInfo.retries = 1;//第一次上传 UploadStatus uploadStatus = new UploadStatus(); uploadInfo.status.Add(uploadInfo.type, uploadStatus);//初始化并在该文件上传信息中添加新的上传状态 InfoLog.InfoPrintf($"\r\n==============建立{uploadInfo.type}上传任务================\r\n" + $"主播名:{uploadInfo.streamerName}" + $"\r\n标题:{uploadInfo.streamTitle}" + $"\r\n本地文件:{uploadInfo.srcFile}" + $"\r\n上传路径:{uploadInfo.remotePath}" + $"\r\n网盘类型:{uploadInfo.type}" + $"\r\n开始时间:{MMPU.Unix转换为DateTime(uploadInfo.status[uploadInfo.type].startTime.ToString())}" + $"\r\n===============建立{uploadInfo.type}上传任务===============\r\n", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].comments = "建立上传任务"; uploadInfo.status[uploadInfo.type].statusCode = 1; //第一次上传 while (true) //失败后重试,达到最大次数后退出 { try { InfoLog.InfoPrintf($"{uploadInfo.type}:开始第{uploadInfo.retries}次上传", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].comments = $"开始第{uploadInfo.retries}次上传"; uploadInfo.status[uploadInfo.type].statusCode = uploadInfo.retries; //第n次上传 @do(uploadInfo); //执行指定目标的上传函数,失败则异常被捕获 InfoLog.InfoPrintf($"{uploadInfo.type}:上传完毕", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].endTime = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); //更新结束时间 InfoLog.InfoPrintf($"\r\n=============={uploadInfo.type}上传成功================\r\n" + $"主播名:{uploadInfo.streamerName}" + $"\r\n标题:{uploadInfo.streamTitle}" + $"\r\n本地文件:{uploadInfo.srcFile}" + $"\r\n上传路径:{uploadInfo.remotePath}" + $"\r\n网盘类型:{uploadInfo.type}" + $"\r\n开始时间:{MMPU.Unix转换为DateTime(uploadInfo.status[uploadInfo.type].startTime.ToString())}" + $"\r\n结束时间:{MMPU.Unix转换为DateTime(uploadInfo.status[uploadInfo.type].endTime.ToString())}" + $"\r\n==============={uploadInfo.type}上传成功===============\r\n", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].comments = $"上传成功"; uploadInfo.status[uploadInfo.type].statusCode = 0; //上传成功 break; //成功则退出 } catch (UploadFailure) //此次上传失败 { if (uploadInfo.retries == Uploader.RETRY_MAX_TIMES) //最后一次上传 { InfoLog.InfoPrintf($"{uploadInfo.type}:第{uploadInfo.retries}/{Uploader.RETRY_MAX_TIMES}次{uploadInfo.type}上传失败", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].endTime = Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); InfoLog.InfoPrintf($"\r\n=============={uploadInfo.type}上传失败================\r\n" + $"主播名:{uploadInfo.streamerName}" + $"\r\n标题:{uploadInfo.streamTitle}" + $"\r\n本地文件:{uploadInfo.srcFile}" + $"\r\n上传路径:{uploadInfo.remotePath}" + $"\r\n网盘类型:{uploadInfo.type}" + $"\r\n开始时间:{MMPU.Unix转换为DateTime(uploadInfo.status[uploadInfo.type].startTime.ToString())}" + $"\r\n结束时间:{MMPU.Unix转换为DateTime(uploadInfo.status[uploadInfo.type].endTime.ToString())}" + $"\r\n==============={uploadInfo.type}上传失败===============\r\n", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].comments = $"上传失败"; uploadInfo.status[uploadInfo.type].statusCode = -1; //上传失败 break; //达到最大重试次数,失败,退出 } else//未达到最大重试次数,等待一定时间后重试 { InfoLog.InfoPrintf($"{uploadInfo.type}:第{uploadInfo.retries}/{Uploader.RETRY_MAX_TIMES}次上传失败,{Uploader.RETRY_WAITING_TIME}s后重试", InfoLog.InfoClass.必要提示); uploadInfo.status[uploadInfo.type].comments = $"第{uploadInfo.retries}次上传失败,重试等待中"; uploadInfo.retries++; //重试次数+1 Thread.Sleep(Uploader.RETRY_WAITING_TIME * 1000); //等待RETRY_WAITING_TIME秒 uploadInfo.status[uploadInfo.type].statusCode = uploadInfo.retries; //更新第n次重试 } } } }