Browse Source

Fix update datetime formatting to be culture invariant

pull/109/head
Ionite 1 year ago
parent
commit
46db84f092
No known key found for this signature in database
  1. 86
      StabilityMatrix.Core/Updater/UpdateHelper.cs

86
StabilityMatrix.Core/Updater/UpdateHelper.cs

@ -1,4 +1,5 @@
using System.Text.Json; using System.Globalization;
using System.Text.Json;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Extensions;
@ -19,23 +20,30 @@ public class UpdateHelper : IUpdateHelper
private readonly DebugOptions debugOptions; private readonly DebugOptions debugOptions;
private readonly System.Timers.Timer timer = new(TimeSpan.FromMinutes(5)); private readonly System.Timers.Timer timer = new(TimeSpan.FromMinutes(5));
private string UpdateManifestUrl => debugOptions.UpdateManifestUrl ?? private string UpdateManifestUrl =>
"https://cdn.lykos.ai/update-v2.json"; debugOptions.UpdateManifestUrl ?? "https://cdn.lykos.ai/update-v2.json";
public const string UpdateFolderName = ".StabilityMatrixUpdate"; public const string UpdateFolderName = ".StabilityMatrixUpdate";
public static DirectoryPath UpdateFolder => Compat.AppCurrentDir.JoinDir(UpdateFolderName); public static DirectoryPath UpdateFolder => Compat.AppCurrentDir.JoinDir(UpdateFolderName);
public static FilePath ExecutablePath => UpdateFolder.JoinFile(Compat.GetExecutableName()); public static FilePath ExecutablePath => UpdateFolder.JoinFile(Compat.GetExecutableName());
public UpdateHelper(ILogger<UpdateHelper> logger, IHttpClientFactory httpClientFactory, public UpdateHelper(
IDownloadService downloadService, IOptions<DebugOptions> debugOptions) ILogger<UpdateHelper> logger,
IHttpClientFactory httpClientFactory,
IDownloadService downloadService,
IOptions<DebugOptions> debugOptions
)
{ {
this.logger = logger; this.logger = logger;
this.httpClientFactory = httpClientFactory; this.httpClientFactory = httpClientFactory;
this.downloadService = downloadService; this.downloadService = downloadService;
this.debugOptions = debugOptions.Value; this.debugOptions = debugOptions.Value;
timer.Elapsed += async (_, _) => { await CheckForUpdate().ConfigureAwait(false); }; timer.Elapsed += async (_, _) =>
{
await CheckForUpdate().ConfigureAwait(false);
};
} }
public async Task StartCheckingForUpdates() public async Task StartCheckingForUpdates()
@ -45,18 +53,33 @@ public class UpdateHelper : IUpdateHelper
await CheckForUpdate().ConfigureAwait(false); await CheckForUpdate().ConfigureAwait(false);
} }
public async Task DownloadUpdate(UpdateInfo updateInfo, public async Task DownloadUpdate(UpdateInfo updateInfo, IProgress<ProgressReport> progress)
IProgress<ProgressReport> progress)
{ {
var downloadUrl = updateInfo.DownloadUrl; var downloadUrl = updateInfo.DownloadUrl;
Directory.CreateDirectory(UpdateFolder); Directory.CreateDirectory(UpdateFolder);
// download the file from URL // download the file from URL
await downloadService.DownloadToFileAsync(downloadUrl, ExecutablePath, progress: progress, await downloadService
httpClientName: "UpdateClient").ConfigureAwait(false); .DownloadToFileAsync(
downloadUrl,
ExecutablePath,
progress: progress,
httpClientName: "UpdateClient"
)
.ConfigureAwait(false);
} }
/// <summary>
/// Format a DatetimeOffset to a culture invariant string for use in signature verification.
/// </summary>
private static string FormatDateTimeOffsetInvariant(DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.ToString(
@"yyyy-MM-ddTHH\:mm\:ss.ffffffzzz",
CultureInfo.InvariantCulture
);
}
/// <summary> /// <summary>
/// Data for use in signature verification. /// Data for use in signature verification.
@ -66,10 +89,10 @@ public class UpdateHelper : IUpdateHelper
private static string GetUpdateInfoSignedData(UpdateInfo updateInfo) private static string GetUpdateInfoSignedData(UpdateInfo updateInfo)
{ {
var channel = updateInfo.Channel.GetStringValue().ToLowerInvariant(); var channel = updateInfo.Channel.GetStringValue().ToLowerInvariant();
var date = updateInfo.ReleaseDate.ToString("yyyy-MM-ddTHH:mm:ss.ffffffzzz"); var date = FormatDateTimeOffsetInvariant(updateInfo.ReleaseDate);
return $"{updateInfo.Version};{date};{channel};" + return $"{updateInfo.Version};{date};{channel};"
$"{(int) updateInfo.Type};{updateInfo.DownloadUrl};{updateInfo.ChangelogUrl};" + + $"{(int)updateInfo.Type};{updateInfo.DownloadUrl};{updateInfo.ChangelogUrl};"
$"{updateInfo.HashBlake3}"; + $"{updateInfo.HashBlake3}";
} }
private async Task CheckForUpdate() private async Task CheckForUpdate()
@ -80,15 +103,19 @@ public class UpdateHelper : IUpdateHelper
var response = await httpClient.GetAsync(UpdateManifestUrl).ConfigureAwait(false); var response = await httpClient.GetAsync(UpdateManifestUrl).ConfigureAwait(false);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
logger.LogWarning("Error while checking for update {StatusCode} - {Content}", logger.LogWarning(
response.StatusCode, await response.Content.ReadAsStringAsync().ConfigureAwait(false)); "Error while checking for update {StatusCode} - {Content}",
response.StatusCode,
await response.Content.ReadAsStringAsync().ConfigureAwait(false)
);
return; return;
} }
var updateCollection = var updateCollection = await JsonSerializer
await JsonSerializer.DeserializeAsync<UpdateCollection>( .DeserializeAsync<UpdateCollection>(
await response.Content.ReadAsStreamAsync() await response.Content.ReadAsStreamAsync().ConfigureAwait(false)
.ConfigureAwait(false)).ConfigureAwait(false); )
.ConfigureAwait(false);
if (updateCollection is null) if (updateCollection is null)
{ {
@ -106,7 +133,10 @@ public class UpdateHelper : IUpdateHelper
if (updateInfo is null) if (updateInfo is null)
{ {
logger.LogWarning("Could not find compatible update info for the platform {Platform}", Compat.Platform); logger.LogWarning(
"Could not find compatible update info for the platform {Platform}",
Compat.Platform
);
return; return;
} }
@ -129,8 +159,11 @@ public class UpdateHelper : IUpdateHelper
if (order > 0) if (order > 0)
{ {
// Newer version available // Newer version available
logger.LogInformation("Update available {AppVer} -> {UpdateVer}", logger.LogInformation(
Compat.AppVersion, updateInfo.Version); "Update available {AppVer} -> {UpdateVer}",
Compat.AppVersion,
updateInfo.Version
);
EventManager.Instance.OnUpdateAvailable(updateInfo); EventManager.Instance.OnUpdateAvailable(updateInfo);
return; return;
} }
@ -142,8 +175,11 @@ public class UpdateHelper : IUpdateHelper
// If different, we can update // If different, we can update
if (updateHash != appHash) if (updateHash != appHash)
{ {
logger.LogInformation("Update available {AppVer} -> {UpdateVer}", logger.LogInformation(
Compat.AppVersion, updateInfo.Version); "Update available {AppVer} -> {UpdateVer}",
Compat.AppVersion,
updateInfo.Version
);
EventManager.Instance.OnUpdateAvailable(updateInfo); EventManager.Instance.OnUpdateAvailable(updateInfo);
return; return;
} }

Loading…
Cancel
Save