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

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