using ReleaseHelper.Models;
using ReleaseHelper.Utils;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ReleaseHelper.Services
{
public static class Release
{
///
/// 设置更新进度
///
///
///
public delegate void DeleSetProgress(int max, int now);
public static async Task ReleaseApplication(string username, string password, double version,
string objFilename, string targetOssPath, DeleSetProgress deleSetProgress)
{
string res = "";
int max = 4, now=0;
List localFilenames = new List() { objFilename, GetUpdateInfoFilename(objFilename) };
// 生成版本信息文件
await SaveLocalUpdateInfo(objFilename, version);
deleSetProgress(max, ++now);
// 登录
await Login(username, password);
deleSetProgress(max, ++now);
// 上传相应文件到OSS服务器
foreach (string f in localFilenames)
{
if (!AliyunOss.SimpleUpload($"{targetOssPath}/{Path.GetFileName(f)}", f))
{
res = $"{f} 更新失败!";
}
deleSetProgress(max, ++now);
}
// 退出登录
Logout();
return res;
}
//private static async Task
public static async Task GetLatestVersionInfo(string ossUri)
{
return await Api.GetUpdateInfo(ossUri);
}
private static async Task SaveLocalUpdateInfo(string objFilename, double version)
{
await Task.Run(() =>
{
string f = GetUpdateInfoFilename(objFilename);
UpdateInfo info = new UpdateInfo
{
MD5 = FileExtensions.FileMd5(objFilename),
Version = version
};
FileExtensions.WriteAllText(f, Json.Encode(info), Encoding.UTF8);
});
}
private static string GetUpdateInfoFilename(string ObjFilename)
{
return Regex.Replace(ObjFilename, "\\.[^.]{2,5}$", ".txt", RegexOptions.IgnoreCase);
}
private static async Task Login(string username, string password)
{
await Api.Login(username, password);
//await Api.GetAliyunAccess();
}
private static void Logout()
{
Variables.Variables.RequestHeaders["Authorization"] = "";
AliyunOss.ClearAccess();
}
}
}