123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using LJProxy.LJLib.Net.SPI.Server;
- using LJProxy.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using LJLib;
- using Microsoft.AspNetCore.Mvc;
- namespace LJProxy.Excutor
- {
- public class CheckUpdateExcutor : ExcutorBase<CheckUpdateRequest>
- {
- private static object _syncRoot = new object();
- protected override IActionResult ExcuteInternal(CheckUpdateRequest request, object state)
- {
- CheckUpdateResponse rslt = new CheckUpdateResponse();
- rslt.ErrCode = "0";
- if (string.IsNullOrEmpty(request.CurrentVersion))
- {
- rslt.ErrMsg = "CurrentVersion can not be empty";
- return new JsonResult(rslt);
- }
- var apkDirs =@$"{AppDomain.CurrentDomain.BaseDirectory}apk\";
- if (!Directory.Exists(apkDirs))
- {
- rslt.ErrMsg = "apk file folder not exists";
- return new JsonResult(rslt);
- }
- //cache file version
- string newVersion = string.Empty;
- string cacheKey = "NewestApkVersion";
- lock (_syncRoot)
- {
- newVersion = Cache.DefaultCache.Get(cacheKey) as string;
- if (string.IsNullOrEmpty(newVersion))
- {
- 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);
- }
- APKHelper apkHelper = new APKHelper();
- newVersion = apkHelper.GetVersionName(file.FullName);
- //TODO:文件夹缓存依赖不生效,待扩展
- Cache.DefaultCache.Add(cacheKey, newVersion, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List<string>() { file.FullName }));
- }
- }
- if (string.Compare(newVersion, request.CurrentVersion) > 0)
- {
- rslt.ErrCode = "1";
- rslt.NewVersion = newVersion;
- }
- return new JsonResult(rslt);
- }
- }
- }
|