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

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