123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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
- {
- /// <summary>
- /// 用户接口
- /// </summary>
- [AutoInject(typeof(IVersionService), InjectType.Scope)]
- public class VersionService : IVersionService
- {
- /// <summary>
- /// 系统操作仓储中转
- /// </summary>
- private IDataRepositoryContext _dataContext;
- /// <summary>
- /// 配置
- /// </summary>
- private IConfiguration _configuration;
- /// <summary>
- /// 程序版本路径
- /// </summary>
- private string _versionPath;
- /// <summary>
- /// 启动程序版本路径
- /// </summary>
- 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<VerCheckResult> 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<VerCheckResult>
- {
- 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<VerCheckResult>
- {
- 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<VersionFileInfo> FindFile(DirectoryInfo di, string filePath, string currentFilePaths)
- {
- var verList = new List<VersionFileInfo>();
- 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
- }
- }
|