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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. return true;
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  157. }
  158. return false;
  159. }
  160. /// <summary>
  161. /// 上传视频
  162. /// </summary>
  163. /// <returns></returns>
  164. public bool UploadVideoTwo(string VideoGuid, out string ErrMessage)
  165. {
  166. ErrMessage = "";
  167. try
  168. {
  169. Model_Video VideoInfo = null;
  170. foreach (Model_WKData Vdata in APP.WKDataList)
  171. {
  172. if (Vdata.VideoList == null)
  173. continue;
  174. foreach (Model_Video videoinfo in Vdata.VideoList)
  175. {
  176. if (videoinfo.FileGuid == VideoGuid)
  177. {
  178. VideoInfo = videoinfo;
  179. break;
  180. }
  181. }
  182. if (VideoInfo != null)
  183. break;
  184. }
  185. if (VideoInfo == null)
  186. {
  187. ErrMessage = "未找到课程!";
  188. return false;
  189. }
  190. string UploadUrl = FileRequestAddress /*"http://192.168.2.18:8908" */+ "/chunkdb/upchunk";//zxyceshi
  191. if (VideoInfo.IsUpload)
  192. {
  193. ErrMessage = "视频已上传";
  194. return false;
  195. }
  196. else
  197. {
  198. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  199. {
  200. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  201. }
  202. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  203. {
  204. //视频长度
  205. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  206. //每片的长度
  207. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  208. if (VideoInfo.SliceLen == 0)
  209. {
  210. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  211. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  212. }
  213. //已上传长度
  214. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  215. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  216. ////分块
  217. //for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  218. //{
  219. // len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  220. // //取指定长度的流
  221. // byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  222. // //参数
  223. // NameValueCollection formFields = new NameValueCollection();
  224. // formFields.Add("identifier", VideoInfo.FileGuid);
  225. // formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  226. // formFields.Add("filename", fileName);
  227. // formFields.Add("totalchunk", VideoInfo.Block.ToString());
  228. // formFields.Add("md5", VideoInfo.FileMD5);
  229. // JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  230. // //0成功,1失败
  231. // if (jo["code"].ToString() != "0")
  232. // {
  233. // ErrMessage = jo["msg"].ToString();
  234. // return false;
  235. // }
  236. //}
  237. //分块
  238. do
  239. {
  240. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  241. //取指定长度的流
  242. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  243. //参数
  244. NameValueCollection formFields = new NameValueCollection();
  245. formFields.Add("identifier", VideoInfo.FileGuid);
  246. formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  247. formFields.Add("filename", fileName);
  248. formFields.Add("totalchunk", VideoInfo.Block.ToString());
  249. formFields.Add("md5", VideoInfo.FileMD5);
  250. VideoInfo.Uploaded++;
  251. //formFields.Add();///教材
  252. //if()//章节若没选不传
  253. //{
  254. // formFields.Add();
  255. //}
  256. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  257. //0成功,1失败
  258. if (jo["code"].ToString() != "0")
  259. {
  260. ErrMessage = jo["msg"].ToString();
  261. return false;
  262. }
  263. } while (len + VideoInfo.SliceLen < filelen);
  264. //合并文件
  265. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  266. if (isres)
  267. {
  268. VideoInfo.IsUpload = true;
  269. return true;
  270. }
  271. else
  272. {
  273. return false;
  274. }
  275. }
  276. else
  277. {
  278. ErrMessage = "上传失败:" + ErrMessage;
  279. return false;
  280. }
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  286. }
  287. return false;
  288. }
  289. }
  290. }