VersionService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using DapperORMCore.Context.DataContext;
  2. using DapperORMCore.Context.Extend;
  3. using DapperORMCore.Model.CoreModel;
  4. using Microsoft.Extensions.Configuration;
  5. using NXWMS.IService.NXWMS;
  6. using NXWMS.Model.AppModels.Condition;
  7. using NXWMS.Model.AppModels.Result;
  8. using NXWMS.Model.Common;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using WestDistance.DapperORM.Repository.Repositorys;
  16. namespace NXWMS.Service.NXWMS
  17. {
  18. /// <summary>
  19. /// 用户接口
  20. /// </summary>
  21. [AutoInject(typeof(IVersionService), InjectType.Scope)]
  22. public class VersionService : IVersionService
  23. {
  24. /// <summary>
  25. /// 系统操作仓储中转
  26. /// </summary>
  27. private IDataRepositoryContext _dataContext;
  28. /// <summary>
  29. /// 配置
  30. /// </summary>
  31. private IConfiguration _configuration;
  32. /// <summary>
  33. /// 程序版本路径
  34. /// </summary>
  35. private string _versionPath;
  36. /// <summary>
  37. /// 启动程序版本路径
  38. /// </summary>
  39. private string _runVersionPath;
  40. public VersionService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration)
  41. {
  42. this._dataContext = dataRepositoryContext;
  43. this._configuration = configuration;
  44. _versionPath = AppDomain.CurrentDomain.BaseDirectory + $"/{configuration["path:version"]}";
  45. _runVersionPath = AppDomain.CurrentDomain.BaseDirectory + configuration["path:run_version_file"];
  46. if (!File.Exists(_versionPath))
  47. {
  48. Directory.CreateDirectory(_versionPath);
  49. }
  50. }
  51. public OperateResultInfo<VerCheckResult> AppCheck(VerAppCheckCondtion info)
  52. {
  53. DirectoryInfo theFolder = new DirectoryInfo(_versionPath);
  54. var directoryNewInfo = theFolder.GetDirectories().OrderByDescending(m => m.Name).FirstOrDefault();
  55. var VerNewPath = _versionPath + @"\" + directoryNewInfo.Name;
  56. var directionInfo = new DirectoryInfo(VerNewPath);
  57. var verPathList = FindFile(directionInfo, $"{_configuration["path:version"]}/" + directoryNewInfo.Name + @"/",
  58. VerNewPath);
  59. if (!string.IsNullOrWhiteSpace(_configuration["path:no_update_suffix"]))
  60. {
  61. //后缀筛选
  62. _configuration["path:no_update_suffix"].Split(',')
  63. .ToList().ForEach(m => { verPathList.RemoveAll(s => s.Name.LastIndexOf($".{m}") > 0); });
  64. }
  65. if (!string.IsNullOrWhiteSpace(_configuration["path:no_update_file"]))
  66. {
  67. //文件筛选
  68. _configuration["path:no_update_file"].Split(',')
  69. .ToList().ForEach(m => { verPathList.RemoveAll(s => s.Name.LastIndexOf($"{m}") > 0); });
  70. }
  71. if (info.Ver.ToUpper() == directoryNewInfo.Name.ToUpper())
  72. {
  73. return new OperateResultInfo<VerCheckResult>
  74. {
  75. AffectedRows = 1,
  76. Status = OperateStatus.Fail,
  77. Data = new VerCheckResult
  78. {
  79. Content = "",
  80. Time = directionInfo.LastWriteTime,
  81. Ver = directoryNewInfo.Name,
  82. TotalSize = verPathList.Sum(m => m.Size),
  83. VersionFileList = verPathList
  84. }
  85. };
  86. }
  87. else
  88. {
  89. //返回成功结果
  90. return new OperateResultInfo<VerCheckResult>
  91. {
  92. AffectedRows = 1,
  93. Status = OperateStatus.Success,
  94. Data = new VerCheckResult
  95. {
  96. Content = "",
  97. Time = directionInfo.LastWriteTime,
  98. Ver = directoryNewInfo.Name,
  99. TotalSize = verPathList.Sum(m => m.Size),
  100. VersionFileList = verPathList
  101. }
  102. };
  103. };
  104. }
  105. #region private
  106. private string ReadFile(string path)
  107. {
  108. byte[] byData = new byte[100];
  109. char[] charData = new char[1000];
  110. try
  111. {
  112. FileStream file = new FileStream(path, FileMode.Open);
  113. file.Seek(0, SeekOrigin.Begin);
  114. file.Read(byData, 0, 100);
  115. Decoder d = Encoding.Default.GetDecoder();
  116. d.GetChars(byData, 0, byData.Length, charData, 0);
  117. file.Close();
  118. return new string(charData);
  119. }
  120. catch (IOException e)
  121. {
  122. Console.WriteLine(e.ToString());
  123. return null;
  124. }
  125. }
  126. private List<VersionFileInfo> FindFile(DirectoryInfo di, string filePath, string currentFilePaths)
  127. {
  128. var verList = new List<VersionFileInfo>();
  129. FileInfo[] fis = di.GetFiles();
  130. for (int i = 0; i < fis.Length; i++)
  131. {
  132. var currentPath = currentFilePaths + @"\" + fis[i].Name;
  133. verList.Add(new VersionFileInfo
  134. {
  135. Name = fis[i].Name,
  136. Path = filePath + fis[i].Name,
  137. Size = GetMB(fis[i].Length),
  138. MD5 = GetMD5HashFromFile(currentPath).ToUpper(),
  139. });
  140. }
  141. return verList;
  142. }
  143. private string GetMD5HashFromFile(string fileName)
  144. {
  145. try
  146. {
  147. FileStream file = new FileStream(fileName, FileMode.Open);
  148. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  149. byte[] retVal = md5.ComputeHash(file);
  150. file.Close();
  151. StringBuilder sb = new StringBuilder();
  152. for (int i = 0; i < retVal.Length; i++)
  153. {
  154. sb.Append(retVal[i].ToString("x2"));
  155. }
  156. return sb.ToString();
  157. }
  158. catch
  159. {
  160. return "";
  161. }
  162. }
  163. private double GetMB(double b)
  164. {
  165. for (int i = 0; i < 2; i++)
  166. {
  167. b /= 1024;
  168. }
  169. return b;
  170. }
  171. #endregion
  172. }
  173. }