using LJLib; using LJProxy.LJLib.Net.SPI.Server; using LJProxy.Models; 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 void ExcuteInternal(GetUpdatePkgRequest request, object state, GetUpdatePkgResponse rslt) { rslt.ErrCode = "0"; var apkDirs = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\"; if (!Directory.Exists(apkDirs)) { rslt.ErrMsg = "apk file folder not exists"; return; } //cache file version ApkInfo apkInfo = null; string cacheKey = "NewestApkData"; lock (_syncRoot) { apkInfo = CacheHelper.DefaultCache.Get(cacheKey) as ApkInfo; 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; } 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 clientVer = apkHelper.GetVersionName(apkData); apkInfo = new ApkInfo(); apkInfo.Version = clientVer; apkInfo.ApkData = apkData; //TODO:文件夹缓存依赖不生效,待扩展 CacheHelper.DefaultCache.Add(cacheKey, apkInfo, CacheHelper.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List() { file.FullName })); } } rslt.Version = apkInfo.Version; rslt.ApkData = apkInfo.ApkData; } public class ApkInfo { public string Version { get; set; } public byte[] ApkData { get; set; } } } }