AppUpdateTool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.8.1 - 2017.8.1
  5. // desc: 程序更新工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using Azylee.Core.DataUtils.GuidUtils;
  9. using Azylee.Core.DelegateUtils.ProcessDelegateUtils;
  10. using Azylee.Core.IOUtils.DirUtils;
  11. using Azylee.Core.IOUtils.FileUtils;
  12. using Azylee.Core.IOUtils.PathUtils;
  13. using Azylee.Core.VersionUtils;
  14. using Azylee.Jsons;
  15. using Azylee.YeahWeb.FTPUtils;
  16. using Azylee.YeahWeb.HttpUtils;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.IO;
  21. namespace Azylee.Update.UpdateUtils
  22. {
  23. /// <summary>
  24. /// 程序更新工具
  25. /// </summary>
  26. public class AppUpdateTool
  27. {
  28. /// <summary>
  29. /// 获取新版本
  30. /// </summary>
  31. /// <param name="s"></param>
  32. /// <param name="version"></param>
  33. /// <param name="info"></param>
  34. /// <returns>
  35. /// -10;//请求版本失败
  36. /// -20;//没有更新的版本
  37. /// </returns>
  38. public int GetNewVersionByString(string s, Version version, out AppUpdateInfo info)
  39. {
  40. Stopwatch stopwatch = new Stopwatch();
  41. stopwatch.Start();
  42. info = Json.String2Object<AppUpdateInfo>(s);
  43. if (info != null)
  44. {
  45. Version newVersion = VersionTool.Format(info.Version);
  46. if (newVersion != null && newVersion > version)
  47. {
  48. stopwatch.Stop();
  49. return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
  50. }
  51. else
  52. {
  53. return -20;//没有更新的版本
  54. }
  55. }
  56. return -10;//请求版本失败
  57. }
  58. /// <summary>
  59. /// 获取新版本
  60. /// </summary>
  61. /// <param name="url"></param>
  62. /// <param name="version"></param>
  63. /// <param name="info"></param>
  64. /// <returns>
  65. /// -10;//请求版本失败
  66. /// -20;//没有更新的版本
  67. /// </returns>
  68. public int GetNewVersion(string url, Version version, out AppUpdateInfo info)
  69. {
  70. Stopwatch stopwatch = new Stopwatch();
  71. stopwatch.Start();
  72. info = HttpTool.Get<AppUpdateInfo>(url);
  73. if (info != null)
  74. {
  75. Version newVersion = VersionTool.Format(info.Version);
  76. if (newVersion != null && newVersion > version)
  77. {
  78. stopwatch.Stop();
  79. return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
  80. }
  81. else
  82. {
  83. return -20;//没有更新的版本
  84. }
  85. }
  86. return -10;//请求版本失败
  87. }
  88. /// <summary>
  89. /// 更新
  90. /// </summary>
  91. /// <param name="info">新版本信息</param>
  92. /// <param name="tempPath">文件下载位置</param>
  93. /// <param name="dictionary">文件相对位置字典</param>
  94. /// <param name="downProgress">下载进度回调</param>
  95. /// <param name="downSender">下载进度事件数据</param>
  96. /// <param name="releaseProgress">释放进度回调</param>
  97. /// <param name="releaseSender">释放进度事件数据</param>
  98. /// <returns>
  99. /// -10000;//没有新版本
  100. /// -20000;//文件下载失败
  101. /// -30000;//文件释放失败
  102. /// </returns>
  103. public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
  104. ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
  105. ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
  106. {
  107. Stopwatch stopwatch = new Stopwatch();
  108. stopwatch.Start();
  109. //请求最新版本信息
  110. if (info != null)
  111. {
  112. string file = DirTool.Combine(tempPath, GuidTool.Short() + "-" + info.Version);
  113. //准备更新(下载)
  114. string downfile = Download(file, info, downProgress, downSender);
  115. if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
  116. {
  117. //格式化释放文件目录(相对路径转换为绝对路径)
  118. string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
  119. //释放文件,释放完成后删除临时文件
  120. int unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender);
  121. File.Delete(file);
  122. if (unpackCode > 0)
  123. {
  124. stopwatch.Stop();
  125. return (int)stopwatch.Elapsed.TotalSeconds;
  126. }
  127. else
  128. {
  129. return -30000 + unpackCode;//文件释放失败
  130. }
  131. }
  132. else
  133. {
  134. return -20000;//文件下载失败
  135. }
  136. }
  137. else
  138. {
  139. return -10000;//没有新版本
  140. }
  141. }
  142. private string Download(string file, AppUpdateInfo info, ProgressDelegate.ProgressHandler progress = null, object sender = null)
  143. {
  144. if (info != null)
  145. {
  146. switch (info.DownloadMode)
  147. {
  148. case 0://http 下载
  149. {
  150. if (HttpTool.Download(info.HttpUrl, file, progress, sender))
  151. return file;
  152. }
  153. break;
  154. case 1://ftp 下载
  155. {
  156. FtpTool ftp = new FtpTool(info.FtpIp, info.FtpAccount, info.FtpPassword);
  157. if (ftp.Download(info.FtpFile, file, progress, sender))
  158. return file;
  159. }
  160. break;
  161. }
  162. }
  163. return null;
  164. }
  165. }
  166. }