UpdateLevelMigration.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (releases.Count >= 2)
  55. {
  56. var beta = releases[1];
  57. Version version;
  58. if (Version.TryParse(beta.tag_name, out version))
  59. {
  60. if (currentVersion >= version)
  61. {
  62. newUpdateLevel = PackageVersionClass.Beta;
  63. }
  64. }
  65. }
  66. if (releases.Count >= 3)
  67. {
  68. var dev = releases[2];
  69. Version version;
  70. if (Version.TryParse(dev.tag_name, out version))
  71. {
  72. if (currentVersion >= version)
  73. {
  74. newUpdateLevel = PackageVersionClass.Dev;
  75. }
  76. }
  77. }
  78. if (newUpdateLevel != updateLevel)
  79. {
  80. _config.Configuration.SystemUpdateLevel = newUpdateLevel;
  81. _config.SaveConfiguration();
  82. }
  83. }
  84. }
  85. }