-
Notifications
You must be signed in to change notification settings - Fork 6
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 #56 from DGP-Studio/cdn
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.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,31 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Snap.Hutao.Server.Controller.Filter; | ||
using Snap.Hutao.Server.Model.Response; | ||
using Snap.Hutao.Server.Service.Distribution; | ||
|
||
namespace Snap.Hutao.Server.Controller; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
[ServiceFilter(typeof(CountRequests))] | ||
[ApiExplorerSettings(GroupName = "Distribution")] | ||
public class DistributionController : ControllerBase | ||
{ | ||
private readonly DistributionService distributionService; | ||
|
||
public DistributionController(DistributionService distributionService) | ||
{ | ||
this.distributionService = distributionService; | ||
} | ||
|
||
[Authorize] | ||
[ServiceFilter(typeof(ValidateGachaLogPermission))] | ||
[HttpGet("GetAcceleratedMirror")] | ||
public async Task<IActionResult> GetAcceleratedMirror([FromQuery(Name = "Filename")] string filename) | ||
{ | ||
HutaoPackageMirror? mirror = await distributionService.GetAcceleratedMirrorAsync(filename); | ||
return Response<HutaoPackageMirror?>.Success("获取加速镜像成功", mirror); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.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,46 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Snap.Hutao.Server.Model.Response; | ||
using Snap.Hutao.Server.Option; | ||
|
||
namespace Snap.Hutao.Server.Service.Distribution; | ||
|
||
// Scoped | ||
public sealed class DistributionService | ||
{ | ||
private readonly ILogger<DistributionService> logger; | ||
private readonly HttpClient httpClient; | ||
private readonly string cdnEndpoint; | ||
private readonly string cdnToken; | ||
|
||
public DistributionService(IServiceProvider serviceProvider) | ||
{ | ||
logger = serviceProvider.GetRequiredService<ILogger<DistributionService>>(); | ||
httpClient = serviceProvider.GetRequiredService<HttpClient>(); | ||
cdnEndpoint = serviceProvider.GetRequiredService<AppOptions>().CdnEndpoint; | ||
cdnToken = serviceProvider.GetRequiredService<AppOptions>().CdnToken; | ||
} | ||
|
||
public async ValueTask<HutaoPackageMirror?> GetAcceleratedMirrorAsync(string filename) | ||
{ | ||
using (HttpRequestMessage req = new(HttpMethod.Get, string.Format(cdnEndpoint, filename))) | ||
{ | ||
req.Headers.UserAgent.ParseAdd("Snap Hutao Server/1.0"); | ||
req.Headers.Authorization = new("Bearer", cdnToken); | ||
|
||
using (HttpResponseMessage resp = await httpClient.SendAsync(req)) | ||
{ | ||
Response<HutaoPackageMirror>? mirrorResp = await resp.Content.ReadFromJsonAsync<Response<HutaoPackageMirror>>(); | ||
ArgumentNullException.ThrowIfNull(mirrorResp); | ||
if (mirrorResp is not { Code: ReturnCode.Success }) | ||
{ | ||
logger.LogWarning("Failed to get accelerated mirror for {Filename}, raw message: {Message}", filename, mirrorResp.Message); | ||
return default; | ||
} | ||
|
||
return mirrorResp.Data; | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/HutaoPackageMirror.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,16 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Server.Service.Distribution; | ||
|
||
public sealed class HutaoPackageMirror | ||
{ | ||
[JsonPropertyName("url")] | ||
public string Url { get; set; } = default!; | ||
|
||
[JsonPropertyName("mirror_name")] | ||
public string MirrorName { get; set; } = default!; | ||
|
||
[JsonPropertyName("mirror_type")] | ||
public string MirrorType { get; set; } = default!; | ||
} |