-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from sdcb/dev
Dev
- Loading branch information
Showing
45 changed files
with
1,003 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/BE/Controllers/Admin/GlobalConfigs/Dtos/CheckUpdateResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chats.BE.Controllers.Admin.GlobalConfigs.Dtos; | ||
|
||
public record CheckUpdateResponse | ||
{ | ||
[JsonPropertyName("hasNewVersion")] | ||
public required bool HasNewVersion { get; init; } | ||
|
||
[JsonPropertyName("tagName")] | ||
public required string TagName { get; init; } | ||
|
||
[JsonPropertyName("currentVersion")] | ||
public required int CurrentVersion { get; init; } | ||
} |
66 changes: 66 additions & 0 deletions
66
src/BE/Controllers/Admin/GlobalConfigs/GitHubReleaseChecker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Net.Http.Headers; | ||
using System.Text.Json; | ||
|
||
namespace Chats.BE.Controllers.Admin.GlobalConfigs; | ||
|
||
public class GitHubReleaseChecker | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly string _owner; | ||
private readonly string _repo; | ||
|
||
public static GitHubReleaseChecker SdcbChats => new("sdcb", "chats"); | ||
|
||
public GitHubReleaseChecker(string owner, string repo) | ||
{ | ||
_owner = owner; | ||
_repo = repo; | ||
|
||
_httpClient = new HttpClient { BaseAddress = new Uri("https://api.github.com") }; | ||
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("SdcbChatsVersionChecker", "1.0")); | ||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); | ||
} | ||
|
||
public async Task<string> GetLatestReleaseTagNameAsync(CancellationToken cancellationToken) | ||
{ | ||
HttpResponseMessage response = await _httpClient.GetAsync($"/repos/{_owner}/{_repo}/releases/latest", cancellationToken); | ||
response.EnsureSuccessStatusCode(); // 如果请求失败,抛出异常 | ||
|
||
using Stream responseStream = await response.Content.ReadAsStreamAsync(cancellationToken); | ||
var jsonDocument = await JsonDocument.ParseAsync(responseStream, cancellationToken: cancellationToken); | ||
|
||
return jsonDocument.RootElement.GetProperty("tag_name").GetString()!; | ||
} | ||
|
||
public static bool IsNewVersionAvailableAsync(string latestTagName, int currentVersion) | ||
{ | ||
try | ||
{ | ||
if (latestTagName.StartsWith("r-")) | ||
{ | ||
int latestVersion = int.Parse(latestTagName.Substring(2)); | ||
return latestVersion > currentVersion; | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Warning: Latest tag name '{latestTagName}' does not follow the expected format 'r-XXX'."); | ||
return false; | ||
} | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
Console.WriteLine($"Error checking for updates: {ex.Message}"); | ||
return false; | ||
} | ||
catch (JsonException ex) | ||
{ | ||
Console.WriteLine($"Error parsing JSON response: {ex.Message}"); | ||
return false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"An unexpected error occurred: {ex.Message}"); | ||
return false; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/BE/Controllers/Admin/GlobalConfigs/VersionController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Chats.BE.Controllers.Admin.Common; | ||
using Chats.BE.Controllers.Admin.GlobalConfigs.Dtos; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Chats.BE.Controllers.Admin.GlobalConfigs; | ||
|
||
[AuthorizeAdmin, Route("api/version")] | ||
public class VersionController : ControllerBase | ||
{ | ||
const int buildVersion = 0; | ||
|
||
[HttpGet("current")] | ||
public ActionResult GetCurrentVersion() | ||
{ | ||
return Ok(buildVersion); | ||
} | ||
|
||
[HttpPost("check-update")] | ||
public async Task<ActionResult> CheckUpdate(CancellationToken cancellationToken) | ||
{ | ||
string tagName = await GitHubReleaseChecker.SdcbChats.GetLatestReleaseTagNameAsync(cancellationToken); | ||
bool hasNewVersion = GitHubReleaseChecker.IsNewVersionAvailableAsync(tagName, buildVersion); | ||
return Ok(new CheckUpdateResponse | ||
{ | ||
CurrentVersion = buildVersion, | ||
HasNewVersion = hasNewVersion, | ||
TagName = tagName, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.