GetUpdatePkgExcutor.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using LJLib;
  2. using LJProxy.LJLib.Net.SPI.Server;
  3. using LJProxy.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace LJProxy.Excutor
  10. {
  11. public class GetUpdatePkgExcutor : ExcutorBase<GetUpdatePkgRequest, GetUpdatePkgResponse>
  12. {
  13. private static object _syncRoot = new object();
  14. protected override void ExcuteInternal(GetUpdatePkgRequest request, object state, GetUpdatePkgResponse rslt)
  15. {
  16. rslt.ErrCode = "0";
  17. var apkDirs = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\";
  18. if (!Directory.Exists(apkDirs))
  19. {
  20. rslt.ErrMsg = "apk file folder not exists";
  21. return;
  22. }
  23. //cache file version
  24. ApkInfo apkInfo = null;
  25. string cacheKey = "NewestApkData";
  26. lock (_syncRoot)
  27. {
  28. apkInfo = CacheHelper.DefaultCache.Get(cacheKey) as ApkInfo;
  29. if (apkInfo == null)
  30. {
  31. var dirinfo = new DirectoryInfo(apkDirs);
  32. var file = dirinfo.GetFiles()
  33. .Where(x => x.Name.EndsWith(".apk", StringComparison.CurrentCultureIgnoreCase))
  34. .OrderByDescending(x => x.LastWriteTime).FirstOrDefault();
  35. if (file == null)
  36. {
  37. rslt.ErrMsg = "can not found apk";
  38. return;
  39. }
  40. byte[] apkData = null;
  41. using (var fs = file.OpenRead())
  42. using (var ms = new MemoryStream())
  43. {
  44. byte[] buff = new byte[10240];
  45. int read = 0;
  46. int total = 0;
  47. while ((read = fs.Read(buff, 0, buff.Length)) > 0)
  48. {
  49. ms.Write(buff, 0, read);
  50. ms.Flush();
  51. total += read;
  52. }
  53. apkData = ms.ToArray();
  54. }
  55. APKHelper apkHelper = new APKHelper();
  56. var clientVer = apkHelper.GetVersionName(apkData);
  57. apkInfo = new ApkInfo();
  58. apkInfo.Version = clientVer;
  59. apkInfo.ApkData = apkData;
  60. //TODO:文件夹缓存依赖不生效,待扩展
  61. CacheHelper.DefaultCache.Add(cacheKey, apkInfo, CacheHelper.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List<string>() { file.FullName }));
  62. }
  63. }
  64. rslt.Version = apkInfo.Version;
  65. rslt.ApkData = apkInfo.ApkData;
  66. }
  67. public class ApkInfo
  68. {
  69. public string Version { get; set; }
  70. public byte[] ApkData { get; set; }
  71. }
  72. }
  73. }