Skip to content

Commit

Permalink
fix: Handle 404 NotFound in GetDominantColorAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Jul 23, 2022
1 parent d3d9ff6 commit ebf7bb4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions HuTao.Services/Image/ImageService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
Expand Down Expand Up @@ -102,8 +103,15 @@ public async ValueTask<Color> GetDominantColorAsync(Uri location)
if (_cache.TryGetValue(key, out Color color))
return color;

var imageBytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(location);
return _cache.Set(key, GetDominantColor(imageBytes), TimeSpan.FromDays(7));
try
{
var imageBytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(location);
return _cache.Set(key, GetDominantColor(imageBytes), TimeSpan.FromHours(1));
}
catch (HttpRequestException e) when (e.StatusCode is HttpStatusCode.NotFound)
{
return Color.Default;
}
}

private static object GetKey(Uri uri) => new { Target = nameof(GetDominantColor), uri.AbsoluteUri };
Expand Down

0 comments on commit ebf7bb4

Please sign in to comment.