Skip to content

Commit

Permalink
fix: better base url for Person images & in general.
Browse files Browse the repository at this point in the history
Closes #67
  • Loading branch information
revam committed Aug 13, 2024
1 parent cfb7dc8 commit f7c361f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Shokofin/API/Info/SeasonInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public bool IsEmpty(int offset = 0)
}

private static string? GetImagePath(Image image)
=> image != null && image.IsAvailable ? image.ToURLString() : null;
=> image != null && image.IsAvailable ? image.ToURLString(internalUrl: true) : null;

private static PersonInfo? RoleToPersonInfo(Role role)
=> role.Type switch
Expand Down
4 changes: 2 additions & 2 deletions Shokofin/API/Models/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public virtual bool IsAvailable
/// set up, but better than nothing.
/// </remarks>
/// <returns>The image URL</returns>
public string ToURLString()
=> new Uri(new Uri(Web.ImageHostUrl.BaseUrl), $"{Web.ImageHostUrl.BasePath}/Plugin/Shokofin/Host/Image/{Source}/{Type}/{ID}").ToString();
public string ToURLString(bool internalUrl = false)
=> new Uri(new Uri(internalUrl ? Plugin.Instance.BaseUrl : Web.ImageHostUrl.BaseUrl), $"{Web.ImageHostUrl.BasePath}/Plugin/Shokofin/Host/Image/{Source}/{Type}/{ID}").ToString();
}

/// <summary>
Expand Down
20 changes: 19 additions & 1 deletion Shokofin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Linq;
using System.Runtime.InteropServices;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
Expand All @@ -15,6 +17,8 @@ namespace Shokofin;

public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{
private readonly IServerConfigurationManager ConfigurationManager;

public const string MetadataProviderName = "Shoko";

public override string Name => MetadataProviderName;
Expand All @@ -38,13 +42,27 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
/// </summary>
public readonly string VirtualRoot;

/// <summary>
/// Base URL where the Jellyfin is running.
/// </summary>
public string BaseUrl => ConfigurationManager.GetNetworkConfiguration() is { } networkOptions
? $"{
(networkOptions.RequireHttps && networkOptions.EnableHttps ? "https" : "http")
}://{
(networkOptions.LocalNetworkAddresses.FirstOrDefault() is { } address && address is not "0.0.0.0" ? address : "localhost")
}:{
(networkOptions.RequireHttps && networkOptions.EnableHttps ? networkOptions.InternalHttpsPort : networkOptions.InternalHttpPort)
}/"
: "http://localhost:8096/";

/// <summary>
/// Gets or sets the event handler that is triggered when this configuration changes.
/// </summary>
public new event EventHandler<PluginConfiguration>? ConfigurationChanged;

public Plugin(ILoggerFactory loggerFactory, IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, ILogger<Plugin> logger) : base(applicationPaths, xmlSerializer)
public Plugin(ILoggerFactory loggerFactory, IServerConfigurationManager configurationManager, IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, ILogger<Plugin> logger) : base(applicationPaths, xmlSerializer)
{
ConfigurationManager = configurationManager;
Instance = this;
base.ConfigurationChanged += OnConfigChanged;
VirtualRoot = Path.Join(applicationPaths.ProgramDataPath, "Shokofin", "VFS");
Expand Down
10 changes: 8 additions & 2 deletions Shokofin/Web/ImageHostUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ namespace Shokofin.Web;
/// </summary>
public class ImageHostUrl : IAsyncActionFilter
{
/// <summary>
/// The internal base url. Will be null if the base url haven't been used
/// yet.
/// </summary>
private static string? InternalBaseUrl { get; set; } = null;

/// <summary>
/// The current image host base url to use.
/// </summary>
public static string BaseUrl { get; private set; } = "http://localhost:8096/";
public static string BaseUrl { get => InternalBaseUrl ??= Plugin.Instance.BaseUrl; }

/// <summary>
/// The current image host base path to use.
Expand All @@ -37,7 +43,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
var uri = uriBuilder.ToString();
lock (LockObj) {
if (!string.Equals(uri, BaseUrl))
BaseUrl = uri;
InternalBaseUrl = uri;
if (!string.Equals(path, BasePath))
BasePath = path;
}
Expand Down

0 comments on commit f7c361f

Please sign in to comment.