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

DalUpload.cs 14KB

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