星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DAL_Upload.cs 14KB

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