星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DAL_Upload.cs 8.6KB

4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = null;
  119. foreach (Model_WKData Vdata in APP.WKDataList)
  120. {
  121. if (Vdata.VideoList == null)
  122. continue;
  123. foreach (Model_Video videoinfo in Vdata.VideoList)
  124. {
  125. if(videoinfo.FileGuid== VideoGuid)
  126. {
  127. VideoInfo = videoinfo;
  128. break;
  129. }
  130. }
  131. if (VideoInfo != null)
  132. break;
  133. }
  134. if (VideoInfo==null)
  135. {
  136. ErrMessage = "未找到课程!";
  137. return false;
  138. }
  139. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";
  140. if (VideoInfo.IsUpload)
  141. {
  142. ErrMessage = "视频已上传";
  143. return false;
  144. }
  145. else
  146. {
  147. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  148. {
  149. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  150. }
  151. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  152. {
  153. //视频长度
  154. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  155. //每片的长度
  156. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  157. if (VideoInfo.SliceLen == 0)
  158. {
  159. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  160. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  161. }
  162. //已上传长度
  163. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  164. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  165. //分块
  166. for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  167. {
  168. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  169. //取指定长度的流
  170. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, 102400);
  171. //参数
  172. NameValueCollection formFields = new NameValueCollection();
  173. formFields.Add("identifier", VideoInfo.FileGuid);
  174. formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  175. formFields.Add("filename", fileName);
  176. formFields.Add("totalchunk", VideoInfo.Block.ToString());
  177. formFields.Add("md5", VideoInfo.FileMD5);
  178. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  179. //0成功,1失败
  180. if (jo["code"].ToString() != "0")
  181. {
  182. ErrMessage = jo["msg"].ToString();
  183. return false;
  184. }
  185. }
  186. //合并文件
  187. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  188. if (isres)
  189. {
  190. VideoInfo.IsUpload = true;
  191. return true;
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. else
  199. {
  200. ErrMessage = "上传失败:" + ErrMessage;
  201. return false;
  202. }
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  208. }
  209. return false;
  210. }
  211. }
  212. }