| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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
- {
- /// <summary>
- /// 设置更新进度
- /// </summary>
- /// <param name="max"></param>
- /// <param name="now"></param>
- public delegate void DeleSetProgress(int max, int now);
- public static async Task<string> ReleaseApplication(string username, string password, double version,
- string objFilename, string targetOssPath, DeleSetProgress deleSetProgress)
- {
- string res = "";
- int max = 4, now=0;
- List<string> localFilenames = new List<string>() { 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<Models.UpdateInfo> 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();
- }
- }
- }
|