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

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