星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DAL_Upload.cs 16KB

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