星火微课系统客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DAL_Upload.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using Common;
  2. using Common.system;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Specialized;
  6. using System.Threading;
  7. using XHWK.Model;
  8. namespace XHWK.WKTool.DAL
  9. {
  10. /// <summary>
  11. /// 上传相关方法
  12. /// 创建人:赵耀
  13. /// 创建时间:2020年9月4日
  14. /// </summary>
  15. public class DAL_Upload
  16. {
  17. //private string APIRequestAddress = APP.apiUrl;
  18. private string FileRequestAddress = APP.uploadUrl;
  19. //private string schoolfileRequestAddress = APP.showImageUrl;
  20. /// <summary>
  21. /// 文件是否允许上传
  22. /// </summary>
  23. /// <param name="MD5">MD5</param>
  24. /// <param name="Message">错误消息</param>
  25. /// <returns></returns>
  26. public bool IsAllowUploaded(string MD5, out string Message)
  27. {
  28. Exception ex = null;
  29. Message = "";//请求重试5次 共5秒
  30. for (int num = 0; num < 5; num++)
  31. {
  32. try
  33. {
  34. JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/isexist", @"application/x-www-form-urlencoded", @"md5=" + MD5, "");
  35. //0成功,1失败
  36. if (jo["code"].ToString() == "0")
  37. {
  38. if (string.IsNullOrWhiteSpace(jo["obj"].ToString()))
  39. {
  40. //不存在 允许上传
  41. return true;
  42. }
  43. else
  44. {
  45. //已存在 不允许上传
  46. return false;
  47. }
  48. }
  49. else
  50. {
  51. Message = jo["msg"].ToString();
  52. return false;
  53. }
  54. }
  55. catch (Exception e)
  56. {
  57. Message = e.Message;
  58. ex = e;
  59. Thread.Sleep(1000);
  60. }
  61. }
  62. string ErrMessage = "【文件是否存在】(IsUploaded):请求失败。" + Message;
  63. LogHelper.WriteErrLog(ErrMessage, ex);
  64. return false;
  65. }
  66. /// <summary>
  67. /// 上报文件合并指令
  68. /// </summary>
  69. /// <param name="Savefolder">保存文件位置 学校id/resource</param>
  70. /// <param name="FileCode">文件唯一编号 Guid</param>
  71. /// <param name="Message">错误信息</param>
  72. /// <returns></returns>
  73. public bool ReportFileMerge(string Savefolder, string FileCode, out string Message)
  74. {
  75. Exception ex = null;
  76. Message = "";//请求重试5次 共5秒
  77. for (int num = 0; num < 5; num++)
  78. {
  79. try
  80. {
  81. JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/mergechunk", @"application/x-www-form-urlencoded", @"savefolder=" + Savefolder + "&identifier=" + FileCode, "");
  82. Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString()); ;
  83. APP.ResourceAddTwo = new Model_ResourceAddTwo();
  84. //0成功,1失败
  85. if (resultObj.code == 0 && resultObj.obj != null)
  86. {
  87. APP.ResourceAddTwo = resultObj.obj;
  88. foreach (Model_WKData Vdata in APP.WKDataList)
  89. {
  90. if (Vdata.VideoList == null)
  91. {
  92. continue;
  93. }
  94. foreach (Model_Video videoinfo in Vdata.VideoList)
  95. {
  96. if (videoinfo.FileGuid == FileCode)
  97. {
  98. videoinfo.IsUpload = true;
  99. break;
  100. }
  101. }
  102. }
  103. return true;
  104. }
  105. else
  106. {
  107. //Message = "上传失败!";
  108. Message = jo["msg"].ToString();
  109. return false;
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. Message = e.Message;
  115. ex = e;
  116. Thread.Sleep(1000);
  117. }
  118. }
  119. string ErrMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + Message;
  120. LogHelper.WriteErrLog(ErrMessage, ex);
  121. return false;
  122. }
  123. /// <summary>
  124. /// 上传视频
  125. /// </summary>
  126. /// <returns></returns>
  127. public bool UploadVideo(string VideoGuid, out string ErrMessage)
  128. {
  129. ErrMessage = "";
  130. try
  131. {
  132. Model_Video VideoInfo = null;
  133. foreach (Model_WKData Vdata in APP.WKDataList)
  134. {
  135. if (Vdata.VideoList == null)
  136. {
  137. continue;
  138. }
  139. foreach (Model_Video videoinfo in Vdata.VideoList)
  140. {
  141. if (videoinfo.FileGuid == VideoGuid)
  142. {
  143. VideoInfo = videoinfo;
  144. break;
  145. }
  146. }
  147. if (VideoInfo != null)
  148. {
  149. break;
  150. }
  151. }
  152. if (VideoInfo == null)
  153. {
  154. ErrMessage = "未找到课程!";
  155. return false;
  156. }
  157. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";//zxyceshi
  158. if (VideoInfo.IsUpload)
  159. {
  160. ErrMessage = "视频已上传";
  161. return false;
  162. }
  163. else
  164. {
  165. return true;
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  171. }
  172. return false;
  173. }
  174. /// <summary>
  175. /// 上传视频
  176. /// </summary>
  177. /// <returns></returns>
  178. public bool UploadVideoTwo(string VideoGuid, out string ErrMessage)
  179. {
  180. ErrMessage = "";
  181. try
  182. {
  183. Model_Video VideoInfo = null;
  184. foreach (Model_WKData Vdata in APP.WKDataList)
  185. {
  186. if (Vdata.VideoList == null)
  187. {
  188. continue;
  189. }
  190. foreach (Model_Video videoinfo in Vdata.VideoList)
  191. {
  192. if (videoinfo.FileGuid == VideoGuid)
  193. {
  194. VideoInfo = videoinfo;
  195. break;
  196. }
  197. }
  198. if (VideoInfo != null)
  199. {
  200. break;
  201. }
  202. }
  203. if (VideoInfo == null)
  204. {
  205. ErrMessage = "未找到课程!";
  206. return false;
  207. }
  208. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";//zxyceshi
  209. if (VideoInfo.IsUpload)
  210. {
  211. ErrMessage = "视频已上传";
  212. return false;
  213. }
  214. else
  215. {
  216. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  217. {
  218. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  219. }
  220. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  221. {
  222. //视频长度
  223. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  224. //每片的长度
  225. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  226. if (VideoInfo.SliceLen == 0)
  227. {
  228. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  229. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  230. }
  231. //已上传长度
  232. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  233. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  234. ////分块
  235. //for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  236. //{
  237. // len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  238. // //取指定长度的流
  239. // byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  240. // //参数
  241. // NameValueCollection formFields = new NameValueCollection();
  242. // formFields.Add("identifier", VideoInfo.FileGuid);
  243. // formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  244. // formFields.Add("filename", fileName);
  245. // formFields.Add("totalchunk", VideoInfo.Block.ToString());
  246. // formFields.Add("md5", VideoInfo.FileMD5);
  247. // JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  248. // //0成功,1失败
  249. // if (jo["code"].ToString() != "0")
  250. // {
  251. // ErrMessage = jo["msg"].ToString();
  252. // return false;
  253. // }
  254. //}
  255. //分块
  256. do
  257. {
  258. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  259. //取指定长度的流
  260. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  261. //参数
  262. NameValueCollection formFields = new NameValueCollection
  263. {
  264. { "identifier", VideoInfo.FileGuid },
  265. { "chunkNumber", (VideoInfo.Uploaded + 1).ToString() },
  266. { "filename", fileName },
  267. { "totalchunk", VideoInfo.Block.ToString() },
  268. { "md5", VideoInfo.FileMD5 }
  269. };
  270. VideoInfo.Uploaded++;
  271. //formFields.Add();///教材
  272. //if()//章节若没选不传
  273. //{
  274. // formFields.Add();
  275. //}
  276. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  277. //0成功,1失败
  278. if (jo["code"].ToString() != "0")
  279. {
  280. ErrMessage = jo["msg"].ToString();
  281. return false;
  282. }
  283. } while (len + VideoInfo.SliceLen < filelen);
  284. //合并文件
  285. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  286. if (isres)
  287. {
  288. VideoInfo.IsUpload = true;
  289. return true;
  290. }
  291. else
  292. {
  293. return false;
  294. }
  295. }
  296. else
  297. {
  298. ErrMessage = "上传失败:" + ErrMessage;
  299. return false;
  300. }
  301. }
  302. }
  303. catch (Exception ex)
  304. {
  305. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  306. }
  307. return false;
  308. }
  309. }
  310. }