using DapperORMCore.Context.DataContext; using DapperORMCore.Context.Extend; using DapperORMCore.Model.CoreModel; using Microsoft.Extensions.Configuration; using NXWMS.IService.NXWMS; using NXWMS.Model.AppModels.Condition; using NXWMS.Model.AppModels.Result; using NXWMS.Model.Common; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using WestDistance.DapperORM.Repository.Repositorys; namespace NXWMS.Service.NXWMS { /// /// 用户接口 /// [AutoInject(typeof(IVersionService), InjectType.Scope)] public class VersionService : IVersionService { /// /// 系统操作仓储中转 /// private IDataRepositoryContext _dataContext; /// /// 配置 /// private IConfiguration _configuration; /// /// 程序版本路径 /// private string _versionPath; /// /// 启动程序版本路径 /// private string _runVersionPath; public VersionService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration) { this._dataContext = dataRepositoryContext; this._configuration = configuration; _versionPath = AppDomain.CurrentDomain.BaseDirectory + $"/{configuration["path:version"]}"; _runVersionPath = AppDomain.CurrentDomain.BaseDirectory + configuration["path:run_version_file"]; if (!File.Exists(_versionPath)) { Directory.CreateDirectory(_versionPath); } } public OperateResultInfo AppCheck(VerAppCheckCondtion info) { DirectoryInfo theFolder = new DirectoryInfo(_versionPath); var directoryNewInfo = theFolder.GetDirectories().OrderByDescending(m => m.Name).FirstOrDefault(); var VerNewPath = _versionPath + @"\" + directoryNewInfo.Name; var directionInfo = new DirectoryInfo(VerNewPath); var verPathList = FindFile(directionInfo, $"{_configuration["path:version"]}/" + directoryNewInfo.Name + @"/", VerNewPath); if (!string.IsNullOrWhiteSpace(_configuration["path:no_update_suffix"])) { //后缀筛选 _configuration["path:no_update_suffix"].Split(',') .ToList().ForEach(m => { verPathList.RemoveAll(s => s.Name.LastIndexOf($".{m}") > 0); }); } if (!string.IsNullOrWhiteSpace(_configuration["path:no_update_file"])) { //文件筛选 _configuration["path:no_update_file"].Split(',') .ToList().ForEach(m => { verPathList.RemoveAll(s => s.Name.LastIndexOf($"{m}") > 0); }); } if (info.Ver.ToUpper() == directoryNewInfo.Name.ToUpper()) { return new OperateResultInfo { AffectedRows = 1, Status = OperateStatus.Fail, Data = new VerCheckResult { Content = "", Time = directionInfo.LastWriteTime, Ver = directoryNewInfo.Name, TotalSize = verPathList.Sum(m => m.Size), VersionFileList = verPathList } }; } else { //返回成功结果 return new OperateResultInfo { AffectedRows = 1, Status = OperateStatus.Success, Data = new VerCheckResult { Content = "", Time = directionInfo.LastWriteTime, Ver = directoryNewInfo.Name, TotalSize = verPathList.Sum(m => m.Size), VersionFileList = verPathList } }; }; } #region private private string ReadFile(string path) { byte[] byData = new byte[100]; char[] charData = new char[1000]; try { FileStream file = new FileStream(path, FileMode.Open); file.Seek(0, SeekOrigin.Begin); file.Read(byData, 0, 100); Decoder d = Encoding.Default.GetDecoder(); d.GetChars(byData, 0, byData.Length, charData, 0); file.Close(); return new string(charData); } catch (IOException e) { Console.WriteLine(e.ToString()); return null; } } private List FindFile(DirectoryInfo di, string filePath, string currentFilePaths) { var verList = new List(); FileInfo[] fis = di.GetFiles(); for (int i = 0; i < fis.Length; i++) { var currentPath = currentFilePaths + @"\" + fis[i].Name; verList.Add(new VersionFileInfo { Name = fis[i].Name, Path = filePath + fis[i].Name, Size = GetMB(fis[i].Length), MD5 = GetMD5HashFromFile(currentPath).ToUpper(), }); } return verList; } private string GetMD5HashFromFile(string fileName) { try { FileStream file = new FileStream(fileName, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch { return ""; } } private double GetMB(double b) { for (int i = 0; i < 2; i++) { b /= 1024; } return b; } #endregion } }