Skip to content

Commit

Permalink
Merge pull request #56 from DGP-Studio/cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx authored Nov 22, 2024
2 parents 8efd15d + c6670f4 commit 8de1571
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
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);
}
}
4 changes: 4 additions & 0 deletions src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public sealed class AppOptions

public string RSAPrivateKey { get; set; } = default!;

public string CdnEndpoint { get; set; } = default!;

public string CdnToken { get; set; } = default!;

public AfdianOptions Afdian { get; set; } = default!;

public DiscordOptions Discord { get; set; } = default!;
Expand Down
2 changes: 2 additions & 0 deletions src/Snap.Hutao.Server/Snap.Hutao.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Snap.Hutao.Server.Service.Announcement;
using Snap.Hutao.Server.Service.Authorization;
using Snap.Hutao.Server.Service.Discord;
using Snap.Hutao.Server.Service.Distribution;
using Snap.Hutao.Server.Service.GachaLog;
using Snap.Hutao.Server.Service.GachaLog.Statistics;
using Snap.Hutao.Server.Service.Github;
Expand Down Expand Up @@ -91,6 +92,7 @@ public static void Main(string[] args)
.AddResponseCompression()
.AddScoped<AccessionService>()
.AddScoped<AnnouncementService>()
.AddScoped<DistributionService>()
.AddScoped<GachaLogService>()
.AddScoped<GithubService>()
.AddScoped<PassportService>()
Expand Down
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;
}
}
}
}
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!;
}

0 comments on commit 8de1571

Please sign in to comment.