UpdateLevelMigration.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Common.Implementations.Updates;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Model.Serialization;
  9. using MediaBrowser.Model.Updates;
  10. namespace MediaBrowser.Server.Startup.Common.Migrations
  11. {
  12. public class UpdateLevelMigration : IVersionMigration
  13. {
  14. private readonly IServerConfigurationManager _config;
  15. private readonly IServerApplicationHost _appHost;
  16. private readonly IHttpClient _httpClient;
  17. private readonly IJsonSerializer _jsonSerializer;
  18. private readonly string _releaseAssetFilename;
  19. public UpdateLevelMigration(IServerConfigurationManager config, IServerApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, string releaseAssetFilename)
  20. {
  21. _config = config;
  22. _appHost = appHost;
  23. _httpClient = httpClient;
  24. _jsonSerializer = jsonSerializer;
  25. _releaseAssetFilename = releaseAssetFilename;
  26. }
  27. public async void Run()
  28. {
  29. var lastVersion = _config.Configuration.LastVersion;
  30. var currentVersion = _appHost.ApplicationVersion;
  31. if (string.Equals(lastVersion, currentVersion.ToString(), StringComparison.OrdinalIgnoreCase))
  32. {
  33. return;
  34. }
  35. try
  36. {
  37. var updateLevel = _config.Configuration.SystemUpdateLevel;
  38. if (updateLevel == PackageVersionClass.Dev)
  39. {
  40. // It's already dev, there's nothing to check
  41. return;
  42. }
  43. await CheckVersion(currentVersion, updateLevel, CancellationToken.None).ConfigureAwait(false);
  44. }
  45. catch
  46. {
  47. }
  48. }
  49. private async Task CheckVersion(Version currentVersion, PackageVersionClass updateLevel, CancellationToken cancellationToken)
  50. {
  51. var releases = await new GithubUpdater(_httpClient, _jsonSerializer, TimeSpan.FromMinutes(5))
  52. .GetLatestReleases("MediaBrowser", "Emby", _releaseAssetFilename, cancellationToken).ConfigureAwait(false);
  53. var newUpdateLevel = updateLevel;
  54. // If the current version is later than current stable, set the update level to beta
  55. if (releases.Count >= 1)
  56. {
  57. var release = releases[0];
  58. Version version;
  59. if (Version.TryParse(release.tag_name, out version))
  60. {
  61. if (currentVersion >= version)
  62. {
  63. newUpdateLevel = PackageVersionClass.Beta;
  64. }
  65. }
  66. }
  67. // If the current version is later than current beta, set the update level to dev
  68. if (releases.Count >= 2)
  69. {
  70. var release = releases[1];
  71. Version version;
  72. if (Version.TryParse(release.tag_name, out version))
  73. {
  74. if (currentVersion >= version)
  75. {
  76. newUpdateLevel = PackageVersionClass.Dev;
  77. }
  78. }
  79. }
  80. if (newUpdateLevel != updateLevel)
  81. {
  82. _config.Configuration.SystemUpdateLevel = newUpdateLevel;
  83. _config.SaveConfiguration();
  84. }
  85. }
  86. }
  87. }