1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace LJProxy.Services
- {
- public class VersionService:ServiceBase
- {
- private static object _updateJsonSyncRoot = new object();
- private static object _apkSyncRoot = new object();
- public string GetUpdateJson()
- {
- string updateJson = string.Empty;
- string key = "UpdateJson";
- lock (_updateJsonSyncRoot)
- {
- updateJson = Cache.DefaultCache.Get(key) as string;
- if (string.IsNullOrEmpty(updateJson))
- {
- string updateJsonPath = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\update.json";
- updateJson = System.IO.File.ReadAllText(updateJsonPath, Encoding.UTF8);
- Cache.DefaultCache.Add(key, updateJson, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List<string>() { updateJsonPath }));
- }
- }
- return updateJson;
- }
- public byte[] GetAPK(string apkName)
- {
- if (string.IsNullOrEmpty(apkName)) return null;
- var updateJson = GetUpdateJson();
- var updateInfoObj = JObject.Parse(updateJson);
- string url = updateInfoObj["url"]?.ToString();
- if (string.IsNullOrEmpty(url))
- {
- return null;
- }
- Regex regx = new Regex(@"GetAppUpdate/(?<apkName>.*?\.apk)", RegexOptions.IgnoreCase);
- var matchResult = regx.Match(url);
- if (matchResult.Success)
- {
- var jsonApkName = matchResult.Groups["apkName"]?.Value;
- if (!apkName.Equals(jsonApkName, StringComparison.OrdinalIgnoreCase)) return null;
- string key = apkName.ToLower();
- byte[] result = null;
- lock (_apkSyncRoot)
- {
- result = Cache.DefaultCache.Get(key) as byte[];
- if (result == null)
- {
- string apkFilePath = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\{apkName}";
- if (!System.IO.File.Exists(apkFilePath))
- {
- return null;
- }
- result = System.IO.File.ReadAllBytes(apkFilePath);
- Cache.DefaultCache.Add(key, result, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List<string>() { apkFilePath }));
- }
- }
- return result;
- }
- return null;
- }
- }
- }
|