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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 =
  246. ReportFileMerge(
  247. APP.UserInfo.cloudcode + "/" + APP.UserInfo.Schoolid.ToString() + "/resource",
  248. videoInfo.FileGuid, out errMessage);
  249. if (isres)
  250. {
  251. videoInfo.IsUpload = true;
  252. return true;
  253. }
  254. else
  255. {
  256. return false;
  257. }
  258. #endregion 合并文件
  259. }
  260. }
  261. else
  262. {
  263. //是否允许上传
  264. if (IsAllowUploaded(videoInfo.FileMD5, out errMessage))
  265. {
  266. if (videoInfo.SliceLen == 0)
  267. {
  268. videoInfo.SliceLen = (long)(uploadSliceLenMb * 1024 * 1024);
  269. videoInfo.Block = (int)(filelen / videoInfo.SliceLen +
  270. (filelen % videoInfo.SliceLen > 0 ? 1 : 0));
  271. }
  272. }
  273. else
  274. {
  275. //文件在服务器上已存在 空间是否存在未知 若增加空间是否存在判断在此处添加
  276. videoInfo.Block = 1;
  277. videoInfo.Uploaded = 1;
  278. videoInfo.IsUpload = true;
  279. return true;
  280. }
  281. }
  282. ////临时修改 没有断点续传,每次上传都重新上传。
  283. //VideoInfo.Uploaded = 0;
  284. {
  285. //已上传长度
  286. long len;
  287. //分块
  288. do
  289. {
  290. len = videoInfo.Uploaded * videoInfo.SliceLen;
  291. //取指定长度的流
  292. byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(videoInfo.VideoPath, len,
  293. (int)videoInfo.SliceLen);
  294. //参数
  295. NameValueCollection formFields = new NameValueCollection
  296. {
  297. { "identifier", videoInfo.FileGuid },
  298. { "chunkNumber", (videoInfo.Uploaded + 1).ToString() },
  299. { "filename", fileName },
  300. { "totalchunk", videoInfo.Block.ToString() },
  301. { "md5", videoInfo.FileMD5 }
  302. };
  303. //formFields.Add();///教材
  304. //if()//章节若没选不传
  305. //{
  306. // formFields.Add();
  307. //}
  308. JObject jo = HttpHelper.UploadRequestflow(uploadUrl, byteArray, fileName, formFields);
  309. //0成功,1失败
  310. if (jo["code"]?.ToString() != "0")
  311. {
  312. errMessage = jo["msg"]?.ToString();
  313. return false;
  314. }
  315. else
  316. {
  317. videoInfo.Uploaded++;
  318. }
  319. } while (len + videoInfo.SliceLen < filelen);
  320. #region 合并文件
  321. //合并文件
  322. bool isres =
  323. ReportFileMerge(
  324. APP.UserInfo.cloudcode + "/" + APP.UserInfo.Schoolid.ToString() + "/resource",
  325. videoInfo.FileGuid, out errMessage);
  326. if (isres)
  327. {
  328. //VideoInfo.IsUpload = true;
  329. return true;
  330. }
  331. else
  332. {
  333. return false;
  334. }
  335. #endregion 合并文件
  336. }
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
  342. }
  343. return false;
  344. }
  345. }
  346. }