| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //************************************************************************
- // https://github.com/yuzhengyang
- // author: yuzhengyang
- // date: 2017.8.1 - 2017.8.1
- // desc: 程序更新工具
- // Copyright (c) yuzhengyang. All rights reserved.
- //************************************************************************
- using Azylee.Core.DataUtils.GuidUtils;
- using Azylee.Core.DelegateUtils.ProcessDelegateUtils;
- using Azylee.Core.IOUtils.DirUtils;
- using Azylee.Core.IOUtils.FileUtils;
- using Azylee.Core.IOUtils.PathUtils;
- using Azylee.Core.VersionUtils;
- using Azylee.Jsons;
- using Azylee.YeahWeb.FTPUtils;
- using Azylee.YeahWeb.HttpUtils;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- namespace Azylee.Update.UpdateUtils
- {
- /// <summary>
- /// 程序更新工具
- /// </summary>
- public class AppUpdateTool
- {
- /// <summary>
- /// 获取新版本
- /// </summary>
- /// <param name="s"></param>
- /// <param name="version"></param>
- /// <param name="info"></param>
- /// <returns>
- /// -10;//请求版本失败
- /// -20;//没有更新的版本
- /// </returns>
- public int GetNewVersionByString(string s, Version version, out AppUpdateInfo info)
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- info = Json.String2Object<AppUpdateInfo>(s);
- if (info != null)
- {
- Version newVersion = VersionTool.Format(info.Version);
- if (newVersion != null && newVersion > version)
- {
- stopwatch.Stop();
- return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
- }
- else
- {
- return -20;//没有更新的版本
- }
- }
- return -10;//请求版本失败
- }
- /// <summary>
- /// 获取新版本
- /// </summary>
- /// <param name="url"></param>
- /// <param name="version"></param>
- /// <param name="info"></param>
- /// <returns>
- /// -10;//请求版本失败
- /// -20;//没有更新的版本
- /// </returns>
- public int GetNewVersion(string url, Version version, out AppUpdateInfo info)
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- info = HttpTool.Get<AppUpdateInfo>(url);
- if (info != null)
- {
- Version newVersion = VersionTool.Format(info.Version);
- if (newVersion != null && newVersion > version)
- {
- stopwatch.Stop();
- return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
- }
- else
- {
- return -20;//没有更新的版本
- }
- }
- return -10;//请求版本失败
- }
- /// <summary>
- /// 更新
- /// </summary>
- /// <param name="info">新版本信息</param>
- /// <param name="tempPath">文件下载位置</param>
- /// <param name="dictionary">文件相对位置字典</param>
- /// <param name="downProgress">下载进度回调</param>
- /// <param name="downSender">下载进度事件数据</param>
- /// <param name="releaseProgress">释放进度回调</param>
- /// <param name="releaseSender">释放进度事件数据</param>
- /// <returns>
- /// -10000;//没有新版本
- /// -20000;//文件下载失败
- /// -30000;//文件释放失败
- /// </returns>
- public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
- ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
- ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- //请求最新版本信息
- if (info != null)
- {
- string file = DirTool.Combine(tempPath, GuidTool.Short() + "-" + info.Version);
- //准备更新(下载)
- string downfile = Download(file, info, downProgress, downSender);
- if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
- {
- //格式化释放文件目录(相对路径转换为绝对路径)
- string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
- //释放文件,释放完成后删除临时文件
- int unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender);
- File.Delete(file);
- if (unpackCode > 0)
- {
- stopwatch.Stop();
- return (int)stopwatch.Elapsed.TotalSeconds;
- }
- else
- {
- return -30000 + unpackCode;//文件释放失败
- }
- }
- else
- {
- return -20000;//文件下载失败
- }
- }
- else
- {
- return -10000;//没有新版本
- }
- }
- private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
- {
- if (info != null)
- {
- switch (info.DownloadMode)
- {
- case 0://http 下载
- {
- if (HttpTool.Download(info.HttpUrl, file, progress, sender))
- return file;
- }
- break;
- case 1://ftp 下载
- {
- FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
- if (ftp.Download(info.FtpFile, file, progress, sender))
- return file;
- }
- break;
- }
- }
- return null;
- }
- }
- }
|