星火微课系统客户端
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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 /*"http://192.168.2.18:8908"*/ + @"/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 /*"http://192.168.2.18:8908"*/ + @"/chunkdb/mergechunk", @"application/x-www-form-urlencoded", @"savefolder=" + Savefolder + "&identifier=" + FileCode, "");
  87. Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString()); ;
  88. APP.ResourceAddTwo = new Model_ResourceAddTwo();
  89. //0成功,1失败
  90. if (resultObj.code == 0 && resultObj.obj != null)
  91. {
  92. APP.ResourceAddTwo = resultObj.obj;
  93. return true;
  94. }
  95. else
  96. {
  97. //Message = "上传失败!";
  98. Message = jo["msg"].ToString();
  99. return false;
  100. }
  101. }
  102. catch (Exception e)
  103. {
  104. Message = e.Message;
  105. ex = e;
  106. Thread.Sleep(1000);
  107. }
  108. }
  109. string ErrMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + Message;
  110. LogHelper.WriteErrLog(ErrMessage, ex);
  111. return false;
  112. }
  113. /// <summary>
  114. /// 上传视频
  115. /// </summary>
  116. /// <returns></returns>
  117. public bool UploadVideo(string VideoGuid, out string ErrMessage)
  118. {
  119. ErrMessage = "";
  120. try
  121. {
  122. Model_Video VideoInfo = null;
  123. foreach (Model_WKData Vdata in APP.WKDataList)
  124. {
  125. if (Vdata.VideoList == null)
  126. continue;
  127. foreach (Model_Video videoinfo in Vdata.VideoList)
  128. {
  129. if(videoinfo.FileGuid== VideoGuid)
  130. {
  131. VideoInfo = videoinfo;
  132. break;
  133. }
  134. }
  135. if (VideoInfo != null)
  136. break;
  137. }
  138. if (VideoInfo==null)
  139. {
  140. ErrMessage = "未找到课程!";
  141. return false;
  142. }
  143. string UploadUrl = FileRequestAddress /*"http://192.168.2.18:8908" */+ "/chunkdb/upchunk";//zxyceshi
  144. if (VideoInfo.IsUpload)
  145. {
  146. ErrMessage = "视频已上传";
  147. return false;
  148. }
  149. else
  150. {
  151. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  152. {
  153. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  154. }
  155. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  156. {
  157. //视频长度
  158. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  159. //每片的长度
  160. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  161. if (VideoInfo.SliceLen == 0)
  162. {
  163. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  164. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  165. }
  166. //已上传长度
  167. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  168. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  169. ////分块
  170. //for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  171. //{
  172. // len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  173. // //取指定长度的流
  174. // byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  175. // //参数
  176. // NameValueCollection formFields = new NameValueCollection();
  177. // formFields.Add("identifier", VideoInfo.FileGuid);
  178. // formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  179. // formFields.Add("filename", fileName);
  180. // formFields.Add("totalchunk", VideoInfo.Block.ToString());
  181. // formFields.Add("md5", VideoInfo.FileMD5);
  182. // JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  183. // //0成功,1失败
  184. // if (jo["code"].ToString() != "0")
  185. // {
  186. // ErrMessage = jo["msg"].ToString();
  187. // return false;
  188. // }
  189. //}
  190. //分块
  191. do
  192. {
  193. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  194. //取指定长度的流
  195. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  196. //参数
  197. NameValueCollection formFields = new NameValueCollection();
  198. formFields.Add("identifier", VideoInfo.FileGuid);
  199. formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  200. formFields.Add("filename", fileName);
  201. formFields.Add("totalchunk", VideoInfo.Block.ToString());
  202. formFields.Add("md5", VideoInfo.FileMD5);
  203. VideoInfo.Uploaded++;
  204. //formFields.Add();///教材
  205. //if()//章节若没选不传
  206. //{
  207. // formFields.Add();
  208. //}
  209. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  210. //0成功,1失败
  211. if (jo["code"].ToString() != "0")
  212. {
  213. ErrMessage = jo["msg"].ToString();
  214. return false;
  215. }
  216. } while (len + VideoInfo.SliceLen < filelen);
  217. //合并文件
  218. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  219. if (isres)
  220. {
  221. VideoInfo.IsUpload = true;
  222. return true;
  223. }
  224. else
  225. {
  226. return false;
  227. }
  228. }
  229. else
  230. {
  231. ErrMessage = "上传失败:" + ErrMessage;
  232. return false;
  233. }
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  239. }
  240. return false;
  241. }
  242. }
  243. }