Skip to content

Commit

Permalink
Adds debug logging to provider and resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
skttl committed Jan 19, 2024
1 parent 6d40bf4 commit ad6208d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/ImageSharpCommunity.Providers.Remote/RemoteImageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ImageSharpCommunity.Providers.Remote.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Web.Providers;
using SixLabors.ImageSharp.Web.Resolvers;
Expand All @@ -10,11 +11,15 @@ public class RemoteImageProvider : IImageProvider
{
private readonly IHttpClientFactory _clientFactory;
private readonly RemoteImageProviderOptions _options;
private readonly ILogger<RemoteImageProvider> _logger;
private readonly ILogger<RemoteImageResolver> _resolverLogger;

public RemoteImageProvider(IHttpClientFactory clientFactory, IOptions<RemoteImageProviderOptions> options)
public RemoteImageProvider(IHttpClientFactory clientFactory, IOptions<RemoteImageProviderOptions> options, ILogger<RemoteImageProvider> logger, ILogger<RemoteImageResolver> resolverLogger)
{
_clientFactory = clientFactory;
_options = options.Value;
_logger = logger;
_resolverLogger = resolverLogger;
}

public ProcessingBehavior ProcessingBehavior => ProcessingBehavior.All;
Expand Down Expand Up @@ -42,11 +47,13 @@ public bool IsValidRequest(HttpContext context)
|| context.Request.Path.GetMatchingRemoteImageProviderSetting(_options) is not RemoteImageProviderSetting options
)
{
_logger.LogDebug("No matching remote image provider setting found for path: {path}", context.Request.Path);
return Task.FromResult((IImageResolver?)null);
}
else
{
return Task.FromResult((IImageResolver?)new RemoteImageResolver(_clientFactory, url, options));
_logger.LogDebug("Found matching remote image provider setting for path: {path}", context.Request.Path);
return Task.FromResult((IImageResolver?)new RemoteImageResolver(_clientFactory, url, options, _resolverLogger));
}
}
private bool IsMatch(HttpContext context)
Expand Down
17 changes: 12 additions & 5 deletions src/ImageSharpCommunity.Providers.Remote/RemoteImageResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageSharpCommunity.Providers.Remote.Configuration;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp.Web.Resolvers;
namespace ImageSharpCommunity.Providers.Remote;

Expand All @@ -7,35 +8,41 @@ public class RemoteImageResolver : IImageResolver
private readonly IHttpClientFactory _clientFactory;
private readonly string _url;
private readonly RemoteImageProviderSetting _setting;
private readonly ILogger<RemoteImageResolver> _logger;

public RemoteImageResolver(IHttpClientFactory clientFactory, string url, RemoteImageProviderSetting setting)
public RemoteImageResolver(IHttpClientFactory clientFactory, string url, RemoteImageProviderSetting setting, ILogger<RemoteImageResolver> logger)
{
_clientFactory = clientFactory;
_url = url;
_setting = setting;
_logger = logger;
}

public async Task<ImageMetadata> GetMetaDataAsync()
{
_logger.LogDebug("Requesting metadata from {Url}", _url);

var client = _clientFactory.GetRemoteImageProviderHttpClient(_setting);

var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, _url), HttpCompletionOption.ResponseHeadersRead);

if (!response.Content.Headers.ContentLength.HasValue)
{
throw new Exception("Required header ContentLength is missing.");
_logger.LogDebug("ContentLength header missing from {Url}", _url);
}

if (!response.Content.Headers.LastModified.HasValue || !response.Content.Headers.ContentLength.HasValue)
if (!response.Content.Headers.LastModified.HasValue)
{
throw new Exception("Required header LastModified is missing.");
_logger.LogDebug("LastModified header missing from {Url}", _url);
}

return new ImageMetadata(response.Content.Headers.LastModified.Value.UtcDateTime, response.Content.Headers.ContentLength.Value);
return new ImageMetadata(response.Content.Headers.LastModified.GetValueOrDefault().UtcDateTime, response.Content.Headers.ContentLength.GetValueOrDefault());
}

public async Task<Stream> OpenReadAsync()
{
_logger.LogDebug("Requesting image from {Url}", _url);

var client = _clientFactory.GetRemoteImageProviderHttpClient(_setting);

var response = await client.GetAsync(_url);
Expand Down

0 comments on commit ad6208d

Please sign in to comment.