Files
jellyfin-plugin-yandexmusic/Jellyfin.Plugin.YandexMusic/Providers/ImageResponseFactory.cs
T

27 lines
939 B
C#

using System.Net;
using System.Net.Http.Headers;
using Jellyfin.Plugin.YandexMusic.Yandex;
namespace Jellyfin.Plugin.YandexMusic.Providers;
/// <summary>Creates image responses for search results.</summary>
internal static class ImageResponseFactory
{
/// <summary>Downloads an image and wraps it into an HTTP response.</summary>
public static async Task<HttpResponseMessage> FromUrlAsync(
YandexMusicClient client,
string url,
CancellationToken cancellationToken)
{
var bytes = await client.GetImageBytesAsync(url, cancellationToken).ConfigureAwait(false);
if (bytes is null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
var content = new ByteArrayContent(bytes);
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return new HttpResponseMessage(HttpStatusCode.OK) { Content = content };
}
}