using LJLib; using LJLib.Net.SPI.Com; using LJLib.Tools.Encry; using LJProxy.LJLib.Net.SPI.Server; using LJProxy.Models; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace LJProxy.Excutor { public class GetUpdatePkgExcutor : ExcutorBase { private static object _syncRoot = new object(); protected override IActionResult ExcuteInternal(GetUpdatePkgRequest request, object state) { GetUpdatePkgResponse rslt = new GetUpdatePkgResponse(); rslt.ErrCode = "0"; var apkDirs = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\"; if (!Directory.Exists(apkDirs)) { rslt.ErrMsg = "apk file folder not exists"; return new JsonResult(rslt); } //cache file version GetUpdatePkgResponse apkInfo = null; string cacheKey = "NewestApkData"; lock (_syncRoot) { apkInfo = Cache.DefaultCache.Get(cacheKey) as GetUpdatePkgResponse; if (apkInfo == null) { var dirinfo = new DirectoryInfo(apkDirs); var file = dirinfo.GetFiles() .Where(x => x.Name.EndsWith(".apk", StringComparison.CurrentCultureIgnoreCase)) .OrderByDescending(x => x.LastWriteTime).FirstOrDefault(); if (file == null) { rslt.ErrMsg = "can not found apk"; return new JsonResult(rslt); } byte[] apkData = null; using (var fs = file.OpenRead()) using (var ms = new MemoryStream()) { byte[] buff = new byte[10240]; int read = 0; int total = 0; while ((read = fs.Read(buff, 0, buff.Length)) > 0) { ms.Write(buff, 0, read); ms.Flush(); total += read; } apkData = ms.ToArray(); } APKHelper apkHelper = new APKHelper(); var md5Helper = new MD5(); var clientVer = apkHelper.GetVersionName(apkData); apkInfo = new GetUpdatePkgResponse(); apkInfo.Version = clientVer; apkInfo.ApkData = apkData; apkInfo.Checksum = md5Helper.GetMD5(apkData); //TODO:文件夹缓存依赖不生效,待扩展 Cache.DefaultCache.Add(cacheKey, apkInfo, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List() { file.FullName })); } } rslt.Version = apkInfo.Version; rslt.ApkData = apkInfo.ApkData; rslt.Checksum = apkInfo.Checksum; return new JsonResult(rslt); } } }