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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if (jo == null)
  36. {
  37. Message = "无法访问文件服务器,请检查网络或文件服务器地址。";
  38. return false;
  39. }
  40. //修改为0为之前上传成功,1为之前未上传成功
  41. if (jo["code"].ToString() == "0")
  42. {
  43. Message = "";
  44. Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString());
  45. APP.ResourceAddTwo = resultObj.obj;
  46. //已存在 不允许上传
  47. return false;
  48. // if (string.IsNullOrWhiteSpace(jo["obj"].ToString()))
  49. // {
  50. // //不存在 允许上传
  51. // return true;
  52. // }
  53. // else
  54. // {
  55. // //已存在 不允许上传
  56. // return false;
  57. // }
  58. }
  59. else
  60. {
  61. Message = "";
  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. if (jo == null)
  94. {
  95. Message = "无法访问文件服务器,请检查网络或文件服务器地址。";
  96. return false;
  97. }
  98. Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString());
  99. APP.ResourceAddTwo = new Model_ResourceAddTwo();
  100. //0成功,1失败
  101. if (resultObj.code == 0 && resultObj.obj != null)
  102. {
  103. APP.ResourceAddTwo = resultObj.obj;
  104. //foreach (Model_WKData Vdata in APP.WKDataList)
  105. //{
  106. // if (Vdata.VideoList == null)
  107. // {
  108. // continue;
  109. // }
  110. // foreach (Model_Video videoinfo in Vdata.VideoList)
  111. // {
  112. // if (videoinfo.FileGuid == FileCode)
  113. // {
  114. // //videoinfo.IsUpload = true;
  115. // break;
  116. // }
  117. // }
  118. //}
  119. return true;
  120. }
  121. else
  122. {
  123. //Message = "上传失败!";
  124. Message = jo["msg"].ToString();
  125. return false;
  126. }
  127. }
  128. catch (Exception e)
  129. {
  130. Message = e.Message;
  131. ex = e;
  132. Thread.Sleep(1000);
  133. }
  134. }
  135. string ErrMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + Message;
  136. LogHelper.WriteErrLog(ErrMessage, ex);
  137. return false;
  138. }
  139. /// <summary>
  140. /// 上传视频
  141. /// </summary>
  142. /// <returns></returns>
  143. public bool UploadVideo(string VideoGuid, out string ErrMessage)
  144. {
  145. ErrMessage = "";
  146. try
  147. {
  148. Model_Video VideoInfo = null;
  149. foreach (Model_WKData Vdata in APP.WKDataList)
  150. {
  151. if (Vdata.VideoList == null)
  152. {
  153. continue;
  154. }
  155. foreach (Model_Video videoinfo in Vdata.VideoList)
  156. {
  157. if (videoinfo.FileGuid == VideoGuid)
  158. {
  159. VideoInfo = videoinfo;
  160. break;
  161. }
  162. }
  163. if (VideoInfo != null)
  164. {
  165. break;
  166. }
  167. }
  168. if (VideoInfo == null)
  169. {
  170. ErrMessage = "未找到课程!";
  171. return false;
  172. }
  173. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";//zxyceshi
  174. if (VideoInfo.IsUpload)
  175. {
  176. ErrMessage = "视频已上传";
  177. return false;
  178. }
  179. else
  180. {
  181. return true;
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  187. }
  188. return false;
  189. }
  190. /// <summary>
  191. /// 上传视频
  192. /// </summary>
  193. /// <returns></returns>
  194. public bool UploadVideoTwo(string VideoGuid, out string ErrMessage)
  195. {
  196. ErrMessage = "";
  197. try
  198. {
  199. Model_Video VideoInfo = null;
  200. foreach (Model_WKData Vdata in APP.WKDataList)
  201. {
  202. if (Vdata.VideoList == null)
  203. {
  204. continue;
  205. }
  206. foreach (Model_Video videoinfo in Vdata.VideoList)
  207. {
  208. if (videoinfo.FileGuid == VideoGuid)
  209. {
  210. VideoInfo = videoinfo;
  211. break;
  212. }
  213. }
  214. if (VideoInfo != null)
  215. {
  216. break;
  217. }
  218. }
  219. if (VideoInfo == null)
  220. {
  221. ErrMessage = "未找到课程!";
  222. return false;
  223. }
  224. string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";//zxyceshi
  225. if (VideoInfo.IsUpload)
  226. {
  227. ErrMessage = "视频已上传";
  228. return false;
  229. }
  230. else
  231. {
  232. if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
  233. {
  234. VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
  235. }
  236. //视频长度
  237. long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
  238. //每片的长度
  239. double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
  240. string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
  241. //是否续传
  242. bool IsSequel = true;
  243. //分块数为0时 从未上传过
  244. if (VideoInfo.Block > 0)
  245. {
  246. //是否已上传所有分块
  247. if (VideoInfo.Block > VideoInfo.Uploaded)
  248. {
  249. //已上传,未上传完
  250. IsSequel = true;
  251. }
  252. else
  253. {
  254. //上传完,未合并
  255. IsSequel = false;
  256. #region 合并文件
  257. //合并文件
  258. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  259. if (isres)
  260. {
  261. VideoInfo.IsUpload = true;
  262. return true;
  263. }
  264. else
  265. {
  266. return false;
  267. }
  268. #endregion
  269. }
  270. }
  271. else
  272. {
  273. //是否允许上传
  274. if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
  275. {
  276. IsSequel = true;
  277. if (VideoInfo.SliceLen == 0)
  278. {
  279. VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
  280. VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
  281. }
  282. }
  283. else
  284. {
  285. //文件在服务器上已存在 空间是否存在未知 若增加空间是否存在判断在此处添加
  286. IsSequel = false;
  287. VideoInfo.Block = 1;
  288. VideoInfo.Uploaded = 1;
  289. VideoInfo.IsUpload = true;
  290. return true;
  291. }
  292. }
  293. ////临时修改 没有断点续传,每次上传都重新上传。
  294. //VideoInfo.Uploaded = 0;
  295. if (IsSequel)
  296. {
  297. //已上传长度
  298. long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  299. #region 废弃
  300. ////分块
  301. //for (; len + VideoInfo.SliceLen < filelen; VideoInfo.Uploaded++)
  302. //{
  303. // len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  304. // //取指定长度的流
  305. // byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  306. // //参数
  307. // NameValueCollection formFields = new NameValueCollection();
  308. // formFields.Add("identifier", VideoInfo.FileGuid);
  309. // formFields.Add("chunkNumber", (VideoInfo.Uploaded + 1).ToString());
  310. // formFields.Add("filename", fileName);
  311. // formFields.Add("totalchunk", VideoInfo.Block.ToString());
  312. // formFields.Add("md5", VideoInfo.FileMD5);
  313. // JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  314. // //0成功,1失败
  315. // if (jo["code"].ToString() != "0")
  316. // {
  317. // ErrMessage = jo["msg"].ToString();
  318. // return false;
  319. // }
  320. //}
  321. #endregion
  322. //分块
  323. do
  324. {
  325. len = VideoInfo.Uploaded * VideoInfo.SliceLen;
  326. //取指定长度的流
  327. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
  328. //参数
  329. NameValueCollection formFields = new NameValueCollection
  330. {
  331. { "identifier", VideoInfo.FileGuid },
  332. { "chunkNumber", (VideoInfo.Uploaded + 1).ToString() },
  333. { "filename", fileName },
  334. { "totalchunk", VideoInfo.Block.ToString() },
  335. { "md5", VideoInfo.FileMD5 }
  336. };
  337. //formFields.Add();///教材
  338. //if()//章节若没选不传
  339. //{
  340. // formFields.Add();
  341. //}
  342. JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
  343. //0成功,1失败
  344. if (jo["code"].ToString() != "0")
  345. {
  346. ErrMessage = jo["msg"].ToString();
  347. return false;
  348. }
  349. else
  350. {
  351. VideoInfo.Uploaded++;
  352. }
  353. } while (len + VideoInfo.SliceLen < filelen);
  354. #region 合并文件
  355. //合并文件
  356. bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
  357. if (isres)
  358. {
  359. //VideoInfo.IsUpload = true;
  360. return true;
  361. }
  362. else
  363. {
  364. return false;
  365. }
  366. #endregion
  367. }
  368. else
  369. {
  370. if (string.IsNullOrWhiteSpace(ErrMessage))
  371. {
  372. //VideoInfo.IsUpload = true;
  373. return true;
  374. }
  375. else
  376. {
  377. return false;
  378. }
  379. }
  380. }
  381. }
  382. catch (Exception ex)
  383. {
  384. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  385. }
  386. return false;
  387. }
  388. }
  389. }