using Microsoft.AspNetCore.Mvc; using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using Microsoft.AspNetCore.Hosting; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using WebApplet.Models; using System.Security.Cryptography; using System.Text; using System.Collections; using Microsoft.Extensions.Logging; using System.Xml; using Newtonsoft.Json; namespace WebApplet.Controllers { [Route("eap/api/[controller]/[action]")] public class CompareVersionController : ControllerBase { private readonly IHostingEnvironment _hostingEnvironment; private ILogger myloger = null; public CompareVersionController(IHostingEnvironment hostingEnvironment, ILogger ploger) { myloger = ploger; _hostingEnvironment = hostingEnvironment; } /// /// 比较是否需要更新文件 /// /// /// public EapResponse CheckVersion([FromBody] CompareVersion programMst) { Hashtable ht2 = programMst.Versions; var response = new EapResponse() { Code = 1, Msg = string.Empty }; try { string path = $"/WebApp/update"; string phyicPath = _hostingEnvironment.WebRootPath + path; this.CreateDirectory(phyicPath); Hashtable hashtable = new Hashtable(); DirectoryInfo folder = new DirectoryInfo(phyicPath); foreach (FileInfo file in folder.GetFiles("*.*")) { hashtable[file.Name] = GetMD5HashFromFile(file.FullName); } if (hashtable.Count > 0) { foreach (string key in hashtable.Keys) { if (ht2.Contains(key)) { if (ht2[key].ToString() != hashtable[key].ToString()) { response.Code = -1; break; } } else { response.Code = -1; break; } } } } catch (Exception ex) { myloger.LogError(ex.ToString() + ex.StackTrace); } return response; } /// /// 比较是否需要下载文件,需要的传文件流 /// /// /// public EapResponse CheckVersionName([FromBody] CompareVersion programMst) { Hashtable ht2 = programMst.Versions; var response = new EapResponse() { Code = 1, Msg = string.Empty }; try { string path = $"/WebApp/update"; string phyicPath = _hostingEnvironment.WebRootPath + path; this.CreateDirectory(phyicPath); Hashtable hashtable = new Hashtable(); DirectoryInfo folder = new DirectoryInfo(phyicPath); Hashtable names = new Hashtable(); foreach (FileInfo file in folder.GetFiles("*.*")) { hashtable[file.Name] = GetMD5HashFromFile(file.FullName); } if (hashtable.Count > 0) { foreach (string key in hashtable.Keys) { if (ht2 != null && ht2.Contains(key)) { if (ht2[key].ToString() != hashtable[key].ToString()) { names[key] = getInfo(phyicPath + @"/" + key); } } else { names[key] = getInfo(phyicPath + @"/" + key); } } } response.Data = names; } catch (Exception ex) { myloger.LogError(ex.ToString() + ex.StackTrace); } return response; } [HttpPost] public EapResponse DownloadAppFile([FromBody] CompareVersion programMst) { Hashtable ht2 = programMst.Versions; var response = new EapResponse() { Code = 1, Msg = string.Empty }; try { string path = $"/WebApp/v3"; string phyicPath = _hostingEnvironment.WebRootPath + path; this.CreateDirectory(phyicPath); Hashtable hashtable = new Hashtable(); DirectoryInfo folder = new DirectoryInfo(phyicPath); Hashtable names = new Hashtable(); foreach (FileInfo file in folder.GetFiles("*.*")) { hashtable[file.Name] = GetMD5HashFromFile(file.FullName); } if (hashtable.Count > 0) { foreach (string key in hashtable.Keys) { if (ht2 != null && ht2.Contains(key)) { if (ht2[key].ToString() != hashtable[key].ToString()) { names[key] = getInfo(phyicPath + @"/" + key); } } else { names[key] = getInfo(phyicPath + @"/" + key); } } } response.Data = names; } catch (Exception ex) { myloger.LogError(ex.ToString() + ex.StackTrace); } return response; } /// /// 获取远端APP版本 /// /// /// public async Task GetAppVersion() { var versionPath = Path.Combine(_hostingEnvironment.WebRootPath, "WebApp", "v3", "version.json"); var file = new FileInfo(versionPath); var fs = file.OpenRead(); var sr = new StreamReader(fs); var str = await sr.ReadToEndAsync(); fs.Close(); fs.Dispose(); var webAppVersion = JsonConvert.DeserializeObject(str); return new EapResponse { Code = 1, Data = webAppVersion }; } /// /// 获取远端更新程序版本 /// /// /// public async Task GetUpdateAppVersion() { var versionPath = Path.Combine(_hostingEnvironment.WebRootPath, "WebApp", "UpdateApp", "update-version.json"); var file = new FileInfo(versionPath); var fs = file.OpenRead(); var sr = new StreamReader(fs); var str = await sr.ReadToEndAsync(); fs.Close(); fs.Dispose(); var webAppVersion = JsonConvert.DeserializeObject(str); return new EapResponse { Code = 1, Data = webAppVersion }; } [HttpPost] public EapResponse DownloadUpdateAppFile([FromBody] CompareVersion programMst) { Hashtable ht2 = programMst.Versions; var response = new EapResponse() { Code = 1, Msg = string.Empty }; try { string path = $"/WebApp/UpdateApp"; string phyicPath = _hostingEnvironment.WebRootPath + path; this.CreateDirectory(phyicPath); Hashtable hashtable = new Hashtable(); DirectoryInfo folder = new DirectoryInfo(phyicPath); Hashtable names = new Hashtable(); foreach (FileInfo file in folder.GetFiles("*.*")) { hashtable[file.Name] = GetMD5HashFromFile(file.FullName); } if (hashtable.Count > 0) { foreach (string key in hashtable.Keys) { if (ht2 != null && ht2.Contains(key)) { if (ht2[key].ToString() != hashtable[key].ToString()) { names[key] = getInfo(phyicPath + @"/" + key); } } else { names[key] = getInfo(phyicPath + @"/" + key); } } } response.Data = names; } catch (Exception ex) { myloger.LogError(ex.ToString() + ex.StackTrace); } return response; } public string getInfo(string filePath) { try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { BinaryReader r = new BinaryReader(fs); byte[] fileArray = r.ReadBytes((int)fs.Length); return Convert.ToBase64String(fileArray); } } catch (Exception ex) { myloger.LogError(ex.ToString()); return null; } } private void CreateDirectory(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } public string GetMD5HashFromFile(string fileName) { try { using (FileStream file = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read)) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } } catch (Exception ex) { myloger.LogError("GetMD5HashFromFile() fail,error:" + ex.Message); return null; } } } }