Skip to content

Commit

Permalink
Merge pull request #19 from Syriiin/fix/missing-beatmap-crash
Browse files Browse the repository at this point in the history
Fix crashing on missing beatmaps
  • Loading branch information
Syriiin authored May 18, 2024
2 parents 5506dfd + e656d9f commit de076f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
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

0 comments on commit de076f1

Please sign in to comment.