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

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