123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- 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<CompareVersionController> myloger = null;
- public CompareVersionController(IHostingEnvironment hostingEnvironment, ILogger<CompareVersionController> ploger)
- {
- myloger = ploger;
- _hostingEnvironment = hostingEnvironment;
- }
- /// <summary>
- /// 比较是否需要更新文件
- /// </summary>
- /// <param name="programMst"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 比较是否需要下载文件,需要的传文件流
- /// </summary>
- /// <param name="programMst"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取远端APP版本
- /// </summary>
- /// <param name="version"></param>
- /// <returns></returns>
- public async Task<EapResponse> 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<WebAppVersion>(str);
- return new EapResponse { Code = 1, Data = webAppVersion };
- }
- /// <summary>
- /// 获取远端更新程序版本
- /// </summary>
- /// <param name="version"></param>
- /// <returns></returns>
- public async Task<EapResponse> 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<WebAppVersion>(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;
- }
- }
- }
- }
|