jellyfin/MediaBrowser.Server.Startup.Common/Migrations/UpdateLevelMigration.cs

101 lines
3.5 KiB
C#
Raw Normal View History

2016-08-22 20:28:24 +02:00
using System;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Implementations.Updates;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Updates;
namespace MediaBrowser.Server.Startup.Common.Migrations
{
public class UpdateLevelMigration : IVersionMigration
{
private readonly IServerConfigurationManager _config;
private readonly IServerApplicationHost _appHost;
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
private readonly string _releaseAssetFilename;
public UpdateLevelMigration(IServerConfigurationManager config, IServerApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, string releaseAssetFilename)
{
_config = config;
_appHost = appHost;
_httpClient = httpClient;
_jsonSerializer = jsonSerializer;
_releaseAssetFilename = releaseAssetFilename;
}
public async void Run()
{
var lastVersion = _config.Configuration.LastVersion;
var currentVersion = _appHost.ApplicationVersion;
if (string.Equals(lastVersion, currentVersion.ToString(), StringComparison.OrdinalIgnoreCase))
{
return;
}
try
{
var updateLevel = _config.Configuration.SystemUpdateLevel;
2016-08-25 23:30:35 +02:00
if (updateLevel == PackageVersionClass.Dev)
2016-08-22 20:28:24 +02:00
{
// It's already dev, there's nothing to check
return;
}
await CheckVersion(currentVersion, updateLevel, CancellationToken.None).ConfigureAwait(false);
}
catch
{
}
}
private async Task CheckVersion(Version currentVersion, PackageVersionClass updateLevel, CancellationToken cancellationToken)
{
2016-08-25 23:30:35 +02:00
var releases = await new GithubUpdater(_httpClient, _jsonSerializer, TimeSpan.FromMinutes(5))
.GetLatestReleases("MediaBrowser", "Emby", _releaseAssetFilename, cancellationToken).ConfigureAwait(false);
2016-08-22 20:28:24 +02:00
2016-08-25 23:30:35 +02:00
var newUpdateLevel = updateLevel;
2016-08-22 20:28:24 +02:00
2016-08-26 19:24:04 +02:00
// If the current version is later than current stable, set the update level to beta
if (releases.Count >= 1)
2016-08-22 20:28:24 +02:00
{
2016-08-26 19:24:04 +02:00
var release = releases[0];
2016-08-25 23:30:35 +02:00
Version version;
2016-08-26 19:24:04 +02:00
if (Version.TryParse(release.tag_name, out version))
2016-08-25 23:30:35 +02:00
{
2016-08-26 21:29:28 +02:00
if (currentVersion > version)
2016-08-25 23:30:35 +02:00
{
newUpdateLevel = PackageVersionClass.Beta;
}
}
2016-08-22 20:28:24 +02:00
}
2016-08-25 23:30:35 +02:00
2016-08-26 19:24:04 +02:00
// If the current version is later than current beta, set the update level to dev
if (releases.Count >= 2)
2016-08-22 20:28:24 +02:00
{
2016-08-26 19:24:04 +02:00
var release = releases[1];
2016-08-25 23:30:35 +02:00
Version version;
2016-08-26 19:24:04 +02:00
if (Version.TryParse(release.tag_name, out version))
2016-08-25 23:30:35 +02:00
{
2016-08-26 21:29:28 +02:00
if (currentVersion > version)
2016-08-25 23:30:35 +02:00
{
newUpdateLevel = PackageVersionClass.Dev;
}
}
2016-08-22 20:28:24 +02:00
}
2016-08-25 23:30:35 +02:00
if (newUpdateLevel != updateLevel)
2016-08-22 20:28:24 +02:00
{
2016-08-25 23:30:35 +02:00
_config.Configuration.SystemUpdateLevel = newUpdateLevel;
_config.SaveConfiguration();
2016-08-22 20:28:24 +02:00
}
}
}
}