星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DAL_Upload.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Common;
  2. using Common.system;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using XHWK.Model;
  13. namespace XHWK.WKTool.DAL
  14. {
  15. /// <summary>
  16. /// 上传相关方法
  17. /// 创建人:赵耀
  18. /// 创建时间:2020年9月4日
  19. /// </summary>
  20. public class DAL_Upload
  21. {
  22. string APIRequestAddress = FileToolsCommon.GetConfigValue("APIRequestAddress");
  23. string FileRequestAddress = FileToolsCommon.GetConfigValue("FileRequestAddress");
  24. string schoolfileRequestAddress = FileToolsCommon.GetConfigValue("schoolfileRequestAddress");
  25. /// <summary>
  26. /// 文件是否允许上传
  27. /// </summary>
  28. /// <param name="MD5">MD5</param>
  29. /// <param name="Message">错误消息</param>
  30. /// <returns></returns>
  31. public bool IsAllowUploaded(string MD5, out string Message)
  32. {
  33. Exception ex = null;
  34. Message = "";//请求重试5次 共5秒
  35. for (int num = 0; num < 5; num++)
  36. {
  37. try
  38. {
  39. JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/isexist", @"application/x-www-form-urlencoded", @"md5=" + MD5, "");
  40. //0成功,1失败
  41. if (jo["code"].ToString() == "0")
  42. {
  43. if (string.IsNullOrWhiteSpace(jo["obj"].ToString()))
  44. {
  45. //不存在 允许上传
  46. return true;
  47. }
  48. else
  49. {
  50. //已存在 不允许上传
  51. return false;
  52. }
  53. }
  54. else
  55. {
  56. Message = jo["msg"].ToString();
  57. return false;
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. Message = e.Message;
  63. ex = e;
  64. Thread.Sleep(1000);
  65. }
  66. }
  67. string ErrMessage = "【文件是否存在】(IsUploaded):请求失败。" + Message;
  68. LogHelper.WriteErrLog(ErrMessage, ex);
  69. return false;
  70. }
  71. /// <summary>
  72. /// 上报文件合并指令
  73. /// </summary>
  74. /// <param name="Savefolder">保存文件位置 学校id/resource</param>
  75. /// <param name="FileCode">文件唯一编号 Guid</param>
  76. /// <param name="Message">错误信息</param>
  77. /// <returns></returns>
  78. public bool ReportFileMerge(string Savefolder, string FileCode, out string Message)
  79. {
  80. Exception ex = null;
  81. Message = "";//请求重试5次 共5秒
  82. for (int num = 0; num < 5; num++)
  83. {
  84. try
  85. {
  86. JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/mergechunk", @"application/x-www-form-urlencoded", @"savefolder=" + Savefolder + "&identifier=" + FileCode, "");
  87. //0成功,1失败
  88. if (jo["code"].ToString() == "0")
  89. {
  90. return true;
  91. }
  92. else
  93. {
  94. Message = jo["msg"].ToString();
  95. return false;
  96. }
  97. }
  98. catch (Exception e)
  99. {
  100. Message = e.Message;
  101. ex = e;
  102. Thread.Sleep(1000);
  103. }
  104. }
  105. string ErrMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + Message;
  106. LogHelper.WriteErrLog(ErrMessage, ex);
  107. return false;
  108. }
  109. /// <summary>
  110. /// 上传视频
  111. /// </summary>
  112. /// <returns></returns>
  113. public bool UploadVideo(string VideoGuid, out string ErrMessage)
  114. {
  115. ErrMessage = "";
  116. try
  117. {
  118. Model_Video VideoInfo = APP.VideoList.Find(x => x.FileGuid == VideoGuid);
  119. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";
  120. if (VideoInfo.IsUpload)
  121. {
  122. ErrMessage = "视频已上传";
  123. return false;
  124. }
  125. else
  126. {
  127. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  128. {
  129. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  130. }
  131. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  132. {
  133. //视频长度
  134. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  135. //每片的长度
  136. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  137. if (VideoInfo.SliceLen == 0)
  138. {
  139. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  140. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  141. }
  142. //已上传长度
  143. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  144. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  145. //分块
  146. for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  147. {
  148. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  149. //取指定长度的流
  150. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, 102400);
  151. //参数
  152. NameValueCollection formFields = new NameValueCollection();
  153. formFields.Add("identifier", VideoInfo.FileGuid);
  154. formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  155. formFields.Add("filename", fileName);
  156. formFields.Add("totalchunk", VideoInfo.Block.ToString());
  157. formFields.Add("md5", VideoInfo.FileMD5);
  158. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  159. //0成功,1失败
  160. if (jo["code"].ToString() != "0")
  161. {
  162. ErrMessage = jo["msg"].ToString();
  163. return false;
  164. }
  165. }
  166. //合并文件
  167. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  168. if (isres)
  169. {
  170. VideoInfo.IsUpload = true;
  171. return true;
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. }
  178. else
  179. {
  180. ErrMessage = "上传失败:" + ErrMessage;
  181. return false;
  182. }
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  188. }
  189. return false;
  190. }
  191. }
  192. }