CompareVersionController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Microsoft.AspNetCore.Mvc;
  2. using Cksoft.Data;
  3. using Cksoft.Data.Repository;
  4. using Cksoft.Unity;
  5. using Microsoft.AspNetCore.Hosting;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using WebApplet.Models;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Collections;
  15. using Microsoft.Extensions.Logging;
  16. using System.Xml;
  17. using Newtonsoft.Json;
  18. namespace WebApplet.Controllers
  19. {
  20. [Route("eap/api/[controller]/[action]")]
  21. public class CompareVersionController : ControllerBase
  22. {
  23. private readonly IHostingEnvironment _hostingEnvironment;
  24. private ILogger<CompareVersionController> myloger = null;
  25. public CompareVersionController(IHostingEnvironment hostingEnvironment, ILogger<CompareVersionController> ploger)
  26. {
  27. myloger = ploger;
  28. _hostingEnvironment = hostingEnvironment;
  29. }
  30. /// <summary>
  31. /// 比较是否需要更新文件
  32. /// </summary>
  33. /// <param name="programMst"></param>
  34. /// <returns></returns>
  35. public EapResponse CheckVersion([FromBody] CompareVersion programMst)
  36. {
  37. Hashtable ht2 = programMst.Versions;
  38. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  39. try
  40. {
  41. string path = $"/WebApp/update";
  42. string phyicPath = _hostingEnvironment.WebRootPath + path;
  43. this.CreateDirectory(phyicPath);
  44. Hashtable hashtable = new Hashtable();
  45. DirectoryInfo folder = new DirectoryInfo(phyicPath);
  46. foreach (FileInfo file in folder.GetFiles("*.*"))
  47. {
  48. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  49. }
  50. if (hashtable.Count > 0)
  51. {
  52. foreach (string key in hashtable.Keys)
  53. {
  54. if (ht2.Contains(key))
  55. {
  56. if (ht2[key].ToString() != hashtable[key].ToString())
  57. {
  58. response.Code = -1;
  59. break;
  60. }
  61. }
  62. else
  63. {
  64. response.Code = -1;
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. myloger.LogError(ex.ToString() + ex.StackTrace);
  73. }
  74. return response;
  75. }
  76. /// <summary>
  77. /// 比较是否需要下载文件,需要的传文件流
  78. /// </summary>
  79. /// <param name="programMst"></param>
  80. /// <returns></returns>
  81. public EapResponse CheckVersionName([FromBody] CompareVersion programMst)
  82. {
  83. Hashtable ht2 = programMst.Versions;
  84. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  85. try
  86. {
  87. string path = $"/WebApp/update";
  88. string phyicPath = _hostingEnvironment.WebRootPath + path;
  89. this.CreateDirectory(phyicPath);
  90. Hashtable hashtable = new Hashtable();
  91. DirectoryInfo folder = new DirectoryInfo(phyicPath);
  92. Hashtable names = new Hashtable();
  93. foreach (FileInfo file in folder.GetFiles("*.*"))
  94. {
  95. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  96. }
  97. if (hashtable.Count > 0)
  98. {
  99. foreach (string key in hashtable.Keys)
  100. {
  101. if (ht2 != null && ht2.Contains(key))
  102. {
  103. if (ht2[key].ToString() != hashtable[key].ToString())
  104. {
  105. names[key] = getInfo(phyicPath + @"/" + key);
  106. }
  107. }
  108. else
  109. {
  110. names[key] = getInfo(phyicPath + @"/" + key);
  111. }
  112. }
  113. }
  114. response.Data = names;
  115. }
  116. catch (Exception ex)
  117. {
  118. myloger.LogError(ex.ToString() + ex.StackTrace);
  119. }
  120. return response;
  121. }
  122. [HttpPost]
  123. public EapResponse DownloadAppFile([FromBody] CompareVersion programMst)
  124. {
  125. Hashtable ht2 = programMst.Versions;
  126. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  127. try
  128. {
  129. string path = $"/WebApp/v3";
  130. string phyicPath = _hostingEnvironment.WebRootPath + path;
  131. this.CreateDirectory(phyicPath);
  132. Hashtable hashtable = new Hashtable();
  133. DirectoryInfo folder = new DirectoryInfo(phyicPath);
  134. Hashtable names = new Hashtable();
  135. foreach (FileInfo file in folder.GetFiles("*.*"))
  136. {
  137. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  138. }
  139. if (hashtable.Count > 0)
  140. {
  141. foreach (string key in hashtable.Keys)
  142. {
  143. if (ht2 != null && ht2.Contains(key))
  144. {
  145. if (ht2[key].ToString() != hashtable[key].ToString())
  146. {
  147. names[key] = getInfo(phyicPath + @"/" + key);
  148. }
  149. }
  150. else
  151. {
  152. names[key] = getInfo(phyicPath + @"/" + key);
  153. }
  154. }
  155. }
  156. response.Data = names;
  157. }
  158. catch (Exception ex)
  159. {
  160. myloger.LogError(ex.ToString() + ex.StackTrace);
  161. }
  162. return response;
  163. }
  164. /// <summary>
  165. /// 获取远端APP版本
  166. /// </summary>
  167. /// <param name="version"></param>
  168. /// <returns></returns>
  169. public async Task<EapResponse> GetAppVersion()
  170. {
  171. var versionPath = Path.Combine(_hostingEnvironment.WebRootPath, "WebApp",
  172. "v3", "version.json");
  173. var file = new FileInfo(versionPath);
  174. var fs = file.OpenRead();
  175. var sr = new StreamReader(fs);
  176. var str = await sr.ReadToEndAsync();
  177. fs.Close();
  178. fs.Dispose();
  179. var webAppVersion = JsonConvert.DeserializeObject<WebAppVersion>(str);
  180. return new EapResponse { Code = 1, Data = webAppVersion };
  181. }
  182. /// <summary>
  183. /// 获取远端更新程序版本
  184. /// </summary>
  185. /// <param name="version"></param>
  186. /// <returns></returns>
  187. public async Task<EapResponse> GetUpdateAppVersion()
  188. {
  189. var versionPath = Path.Combine(_hostingEnvironment.WebRootPath, "WebApp",
  190. "UpdateApp", "update-version.json");
  191. var file = new FileInfo(versionPath);
  192. var fs = file.OpenRead();
  193. var sr = new StreamReader(fs);
  194. var str = await sr.ReadToEndAsync();
  195. fs.Close();
  196. fs.Dispose();
  197. var webAppVersion = JsonConvert.DeserializeObject<WebAppVersion>(str);
  198. return new EapResponse { Code = 1, Data = webAppVersion };
  199. }
  200. [HttpPost]
  201. public EapResponse DownloadUpdateAppFile([FromBody] CompareVersion programMst)
  202. {
  203. Hashtable ht2 = programMst.Versions;
  204. var response = new EapResponse() { Code = 1, Msg = string.Empty };
  205. try
  206. {
  207. string path = $"/WebApp/UpdateApp";
  208. string phyicPath = _hostingEnvironment.WebRootPath + path;
  209. this.CreateDirectory(phyicPath);
  210. Hashtable hashtable = new Hashtable();
  211. DirectoryInfo folder = new DirectoryInfo(phyicPath);
  212. Hashtable names = new Hashtable();
  213. foreach (FileInfo file in folder.GetFiles("*.*"))
  214. {
  215. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  216. }
  217. if (hashtable.Count > 0)
  218. {
  219. foreach (string key in hashtable.Keys)
  220. {
  221. if (ht2 != null && ht2.Contains(key))
  222. {
  223. if (ht2[key].ToString() != hashtable[key].ToString())
  224. {
  225. names[key] = getInfo(phyicPath + @"/" + key);
  226. }
  227. }
  228. else
  229. {
  230. names[key] = getInfo(phyicPath + @"/" + key);
  231. }
  232. }
  233. }
  234. response.Data = names;
  235. }
  236. catch (Exception ex)
  237. {
  238. myloger.LogError(ex.ToString() + ex.StackTrace);
  239. }
  240. return response;
  241. }
  242. public string getInfo(string filePath)
  243. {
  244. try
  245. {
  246. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  247. {
  248. BinaryReader r = new BinaryReader(fs);
  249. byte[] fileArray = r.ReadBytes((int)fs.Length);
  250. return Convert.ToBase64String(fileArray);
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. myloger.LogError(ex.ToString());
  256. return null;
  257. }
  258. }
  259. private void CreateDirectory(string path)
  260. {
  261. if (!Directory.Exists(path))
  262. {
  263. Directory.CreateDirectory(path);
  264. }
  265. }
  266. public string GetMD5HashFromFile(string fileName)
  267. {
  268. try
  269. {
  270. using (FileStream file = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read))
  271. {
  272. MD5 md5 = new MD5CryptoServiceProvider();
  273. byte[] retVal = md5.ComputeHash(file);
  274. StringBuilder sb = new StringBuilder();
  275. for (int i = 0; i < retVal.Length; i++)
  276. {
  277. sb.Append(retVal[i].ToString("x2"));
  278. }
  279. return sb.ToString();
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. myloger.LogError("GetMD5HashFromFile() fail,error:" + ex.Message);
  285. return null;
  286. }
  287. }
  288. }
  289. }