diff --git a/Difficalcy.Tests/DummyCalculatorServiceTest.cs b/Difficalcy.Tests/DummyCalculatorServiceTest.cs index f18745f..9e8efba 100644 --- a/Difficalcy.Tests/DummyCalculatorServiceTest.cs +++ b/Difficalcy.Tests/DummyCalculatorServiceTest.cs @@ -45,7 +45,7 @@ protected override DummyCalculation CalculatePerformance(DummyScore score, objec protected override object DeserialiseDifficultyAttributes(string difficultyAttributesJson) => double.Parse(difficultyAttributesJson); - protected override Task EnsureBeatmap(string beatmapId) => + protected override Task EnsureBeatmap(string beatmapId) => Task.FromResult(true); } diff --git a/Difficalcy/Services/IBeatmapProvider.cs b/Difficalcy/Services/IBeatmapProvider.cs index cadea5a..d01217a 100644 --- a/Difficalcy/Services/IBeatmapProvider.cs +++ b/Difficalcy/Services/IBeatmapProvider.cs @@ -5,7 +5,7 @@ namespace Difficalcy.Services { public interface IBeatmapProvider { - public Task EnsureBeatmap(string beatmapId); + public Task EnsureBeatmap(string beatmapId); public Stream GetBeatmapStream(string beatmapId); } diff --git a/Difficalcy/Services/TestBeatmapProvider.cs b/Difficalcy/Services/TestBeatmapProvider.cs index fd28942..8491efe 100644 --- a/Difficalcy/Services/TestBeatmapProvider.cs +++ b/Difficalcy/Services/TestBeatmapProvider.cs @@ -2,27 +2,30 @@ using System.IO; using System.Reflection; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; namespace Difficalcy.Services { public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider { - public Task EnsureBeatmap(string beatmapId) + public Task EnsureBeatmap(string beatmapId) { - var resourceName = $"{resourceAssemblyName}.Resources.{beatmapId}"; - var info = ResourceAssembly.GetManifestResourceInfo(resourceName); - return Task.FromResult(info != null); + var resourceName = GetResourceName(beatmapId); + _ = ResourceAssembly.GetManifestResourceInfo(resourceName) ?? throw new BadHttpRequestException($"Beatmap not found: {beatmapId}"); + return Task.CompletedTask; } public Stream GetBeatmapStream(string beatmapId) + { + var resourceName = GetResourceName(beatmapId); + return ResourceAssembly.GetManifestResourceStream(resourceName); + } + + private string GetResourceName(string beatmapId) { var resourceNamespace = "Testing.Beatmaps"; var resourceName = $"{resourceNamespace}.{beatmapId}.osu"; - var fullResourceName = $"{resourceAssemblyName}.Resources.{resourceName}"; - var stream = ResourceAssembly.GetManifestResourceStream(fullResourceName); - if (stream == null) - throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{resourceAssemblyName}"""); - return stream; + return $"{resourceAssemblyName}.Resources.{resourceName}"; } private Assembly ResourceAssembly diff --git a/Difficalcy/Services/WebBeatmapProvider.cs b/Difficalcy/Services/WebBeatmapProvider.cs index 0765e30..9368ba8 100644 --- a/Difficalcy/Services/WebBeatmapProvider.cs +++ b/Difficalcy/Services/WebBeatmapProvider.cs @@ -1,6 +1,7 @@ using System.IO; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; namespace Difficalcy.Services @@ -10,21 +11,21 @@ public class WebBeatmapProvider(IConfiguration configuration) : IBeatmapProvider private readonly string _beatmapDirectory = configuration["BEATMAP_DIRECTORY"]; private readonly HttpClient _httpClient = new(); - public async Task EnsureBeatmap(string beatmapId) + public async Task EnsureBeatmap(string beatmapId) { var beatmapPath = GetBeatmapPath(beatmapId); if (!File.Exists(beatmapPath)) { using var response = await _httpClient.GetAsync($"https://osu.ppy.sh/osu/{beatmapId}"); if (!response.IsSuccessStatusCode) - { - return false; - } + throw new BadHttpRequestException("Beatmap not found"); using var fs = new FileStream(beatmapPath, FileMode.CreateNew); + if (fs.Length == 0) + throw new BadHttpRequestException("Beatmap not found"); + await response.Content.CopyToAsync(fs); } - return true; } public Stream GetBeatmapStream(string beatmapId)