Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashing on missing beatmaps #19

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Difficalcy.Tests/DummyCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override DummyCalculation CalculatePerformance(DummyScore score, objec
protected override object DeserialiseDifficultyAttributes(string difficultyAttributesJson) =>
double.Parse(difficultyAttributesJson);

protected override Task<bool> EnsureBeatmap(string beatmapId) =>
protected override Task EnsureBeatmap(string beatmapId) =>
Task.FromResult(true);
}

Expand Down
2 changes: 1 addition & 1 deletion Difficalcy/Services/IBeatmapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Difficalcy.Services
{
public interface IBeatmapProvider
{
public Task<bool> EnsureBeatmap(string beatmapId);
public Task EnsureBeatmap(string beatmapId);

public Stream GetBeatmapStream(string beatmapId);
}
Expand Down
21 changes: 12 additions & 9 deletions Difficalcy/Services/TestBeatmapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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
Expand Down
11 changes: 6 additions & 5 deletions Difficalcy/Services/WebBeatmapProvider.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<bool> 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)
Expand Down
Loading