XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.UpgradeHelper

【DLL】对接ApplicationUpgradeManager的nuget包,可以快速实现软件版本更新请求

公开
关注 0 Fork 0 Star 0
UTF-8
using ApplicationUpgradeManager.Core.Model;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using XFEExtension.NetCore.UpgradeHelper.Models;
using XFEExtension.NetCore.UpgradeHelper.Utilities.Converter;
using XFEExtension.NetCore.XFETransform.JsonConverter;

namespace XFEExtension.NetCore.UpgradeHelper.Utilities;

public class Upgrader(string serverIp)
{
    private static readonly JsonSerializerOptions jsonSerializerOptions = new();
    public string ServerIp { get; set; } = serverIp;

    static Upgrader() => jsonSerializerOptions.Converters.Add(new JsonDateTimeConverter());

    public async Task<UpgradeInfoNotes> GetReleaseNotes(string appName, string version) => await GetReleaseNotes(appName, ServerIp, version);

    public async Task<UpgradeInfoObject> GetReleaseObject(string appName, string version) => await GetReleaseObject(appName, ServerIp, version);

    public static async Task<UpgradeInfoNotes> GetReleaseNotes(string appName, string serverIp, string version)
    {
        using var client = new HttpClient();
        QueryableJsonNode jsonNode = await (await client.PostAsJsonAsync(serverIp, new
        {
            execute = "get_versionInfo",
            applicationName = appName,
            clientVersion = version,
            useJson = false
        })).Content.ReadAsStringAsync();
        return new()
        {
            IsLatest = bool.Parse(jsonNode["isLatest"]),
            LatestVersion = jsonNode["latestVersion"],
            ReleaseNotes = Encoding.UTF8.GetString(Convert.FromBase64String(jsonNode["releaseNotes"])),
            DownloadUrl = jsonNode["downloadUrl"]
        };
    }

    public static async Task<UpgradeInfoObject> GetReleaseObject(string appName, string serverIp, string version)
    {
        using var client = new HttpClient();
        var result = await (await client.PostAsJsonAsync(serverIp, new
        {
            execute = "get_versionInfo",
            applicationName = appName,
            clientVersion = version,
            useJson = true
        })).Content.ReadAsStringAsync();
        QueryableJsonNode jsonNode = result;
        return new()
        {
            IsLatest = bool.Parse(jsonNode["isLatest"]),
            LatestVersion = jsonNode["latestVersion"],
            VersionInfo = JsonSerializer.Deserialize<List<VersionInfo>>(Encoding.UTF8.GetString(Convert.FromBase64String(jsonNode["releaseNotes"])), jsonSerializerOptions) ?? [],
            DownloadUrl = jsonNode["downloadUrl"]
        };
    }
}