星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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