refactor to unify data fetching and define abstract API for re-use
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
@@ -21,18 +21,22 @@ public class SRFImageProvider : IRemoteImageProvider, IHasOrder
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly ILogger<SRFImageProvider> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly IMediaCompositionFetcher _compositionFetcher;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SRFImageProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="httpClientFactory">The HTTP client factory.</param>
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
public SRFImageProvider(IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory)
|
||||
/// <param name="compositionFetcher">The media composition fetcher.</param>
|
||||
public SRFImageProvider(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILoggerFactory loggerFactory,
|
||||
IMediaCompositionFetcher compositionFetcher)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<SRFImageProvider>();
|
||||
_compositionFetcher = compositionFetcher;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -78,8 +82,7 @@ public class SRFImageProvider : IRemoteImageProvider, IHasOrder
|
||||
_logger.LogDebug("Fetching images for SRF URN: {Urn}", urn);
|
||||
|
||||
// Fetch media composition to get image URLs
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
var mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
var mediaComposition = await _compositionFetcher.GetMediaCompositionAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition == null)
|
||||
{
|
||||
@@ -151,9 +154,69 @@ public class SRFImageProvider : IRemoteImageProvider, IHasOrder
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
||||
return httpClient.GetAsync(new Uri(url), cancellationToken);
|
||||
|
||||
// Create request with proper headers - SRF CDN requires User-Agent
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
|
||||
request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
|
||||
request.Headers.Accept.ParseAdd("image/*");
|
||||
|
||||
var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Fix Content-Type if it's binary/octet-stream - SRF CDN returns wrong content type
|
||||
// Jellyfin needs correct Content-Type to process images
|
||||
if (response.IsSuccessStatusCode &&
|
||||
response.Content.Headers.ContentType?.MediaType == "binary/octet-stream")
|
||||
{
|
||||
// Determine correct content type from URL extension
|
||||
var contentType = GetContentTypeFromUrl(url);
|
||||
if (!string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
_logger.LogDebug("Fixing Content-Type from binary/octet-stream to {ContentType} for {Url}", contentType, url);
|
||||
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the correct content type based on URL file extension.
|
||||
/// </summary>
|
||||
private static string? GetContentTypeFromUrl(string url)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the file extension from the URL (ignore query string)
|
||||
var uri = new Uri(url);
|
||||
var path = uri.AbsolutePath.ToLowerInvariant();
|
||||
|
||||
if (path.EndsWith(".jpg", StringComparison.Ordinal) || path.EndsWith(".jpeg", StringComparison.Ordinal))
|
||||
{
|
||||
return "image/jpeg";
|
||||
}
|
||||
|
||||
if (path.EndsWith(".png", StringComparison.Ordinal))
|
||||
{
|
||||
return "image/png";
|
||||
}
|
||||
|
||||
if (path.EndsWith(".gif", StringComparison.Ordinal))
|
||||
{
|
||||
return "image/gif";
|
||||
}
|
||||
|
||||
if (path.EndsWith(".webp", StringComparison.Ordinal))
|
||||
{
|
||||
return "image/webp";
|
||||
}
|
||||
|
||||
// Default to JPEG for SRF images (most common)
|
||||
return "image/jpeg";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user