Skip to content

Commit

Permalink
fix: split base path from base url
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Aug 18, 2024
1 parent fddf8d0 commit b720a39
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Shokofin/API/Models/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public virtual bool IsAvailable
/// </remarks>
/// <returns>The image URL</returns>
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();
=> new Uri(new Uri(internalUrl ? Plugin.Instance.BaseUrl : Web.ImageHostUrl.BaseUrl), $"{(internalUrl ? Plugin.Instance.BasePath : Web.ImageHostUrl.BasePath)}/Plugin/Shokofin/Host/Image/{Source}/{Type}/{ID}").ToString();
}

/// <summary>
Expand Down
96 changes: 72 additions & 24 deletions Shokofin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,88 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages

private readonly IServerConfigurationManager _configurationManager;

private readonly ILogger<Plugin> _logger;
private readonly ILogger<Plugin> Logger;

private DateTime? _lastBaseUrlUpdate = null;
/// <summary>
/// The last time the base URL and base path was updated.
/// </summary>
private DateTime? LastBaseUrlUpdate = null;

private string? _baseUrl = null;
/// <summary>
/// Cached base URL of the Jellyfin server, to avoid calculating it all the
/// time.
/// </summary>
private string? CachedBaseUrl = null;

/// <summary>
/// Base URL where the Jellyfin is running.
/// Base URL where the Jellyfin server is running.
/// </summary>
public string BaseUrl
{
get
{
if (_baseUrl is not null && _lastBaseUrlUpdate is not null && DateTime.Now - _lastBaseUrlUpdate < BaseUrlUpdateDelay)
return _baseUrl;

_lastBaseUrlUpdate = DateTime.Now;
if (_configurationManager.GetNetworkConfiguration() is not { } networkOptions)
{
_baseUrl = "http://localhost:8096/";
return _baseUrl;
if (CachedBaseUrl is not null && LastBaseUrlUpdate is not null && DateTime.Now - LastBaseUrlUpdate < BaseUrlUpdateDelay)
return CachedBaseUrl;

lock(this) {
LastBaseUrlUpdate = DateTime.Now;
if (_configurationManager.GetNetworkConfiguration() is not { } networkOptions)
{
CachedBaseUrl = "http://localhost:8096/";
CachedBasePath = string.Empty;
return CachedBaseUrl;
}

var protocol = networkOptions.RequireHttps && networkOptions.EnableHttps ? "https" : "http";
var hostname = networkOptions.LocalNetworkAddresses.FirstOrDefault() is { } address && address is not "0.0.0.0" and not "::" ? address : "localhost";
var port = networkOptions.RequireHttps && networkOptions.EnableHttps ? networkOptions.InternalHttpsPort : networkOptions.InternalHttpPort;
var basePath = networkOptions.BaseUrl is { } baseUrl ? baseUrl : string.Empty;
if (basePath.Length > 0 && basePath[0] == '/')
basePath = basePath[1..];
CachedBaseUrl = new UriBuilder(protocol, hostname, port).ToString();
CachedBasePath = basePath;
return CachedBaseUrl;
}

var protocol = networkOptions.RequireHttps && networkOptions.EnableHttps ? "https" : "http";
var hostname = networkOptions.LocalNetworkAddresses.FirstOrDefault() is { } address && address is not "0.0.0.0" and not "::" ? address : "localhost";
var port = networkOptions.RequireHttps && networkOptions.EnableHttps ? networkOptions.InternalHttpsPort : networkOptions.InternalHttpPort;
var basePath = networkOptions.BaseUrl is { } baseUrl ? baseUrl : string.Empty;
if (basePath.Length > 0 && basePath[0] != '/')
basePath = "/" + basePath;
_baseUrl = new UriBuilder(protocol, hostname, port, basePath).ToString();
return _baseUrl;
}
}

/// <summary>
/// Cached base path of the Jellyfin server, to avoid calculating it all the
/// time.
/// </summary>
private string? CachedBasePath = null;

/// <summary>
/// Base path where the Jellyfin server is running on the domain.
/// </summary>
public string BasePath
{
get
{
if (CachedBasePath is not null && LastBaseUrlUpdate is not null && DateTime.Now - LastBaseUrlUpdate < BaseUrlUpdateDelay)
return CachedBasePath;

lock(this) {
LastBaseUrlUpdate = DateTime.Now;
if (_configurationManager.GetNetworkConfiguration() is not { } networkOptions)
{
CachedBaseUrl = "http://localhost:8096/";
CachedBasePath = string.Empty;
return CachedBaseUrl;
}

var protocol = networkOptions.RequireHttps && networkOptions.EnableHttps ? "https" : "http";
var hostname = networkOptions.LocalNetworkAddresses.FirstOrDefault() is { } address && address is not "0.0.0.0" and not "::" ? address : "localhost";
var port = networkOptions.RequireHttps && networkOptions.EnableHttps ? networkOptions.InternalHttpsPort : networkOptions.InternalHttpPort;
var basePath = networkOptions.BaseUrl is { } baseUrl ? baseUrl : string.Empty;
if (basePath.Length > 0 && basePath[0] == '/')
basePath = basePath[1..];
CachedBaseUrl = new UriBuilder(protocol, hostname, port).ToString();
CachedBasePath = basePath;
return CachedBasePath;
}
}
}

public const string MetadataProviderName = "Shoko";

Expand Down Expand Up @@ -89,7 +137,7 @@ public Plugin(ILoggerFactory loggerFactory, IServerConfigurationManager configur
base.ConfigurationChanged += OnConfigChanged;
VirtualRoot = Path.Join(applicationPaths.ProgramDataPath, "Shokofin", "VFS");
Tracker = new(loggerFactory.CreateLogger<UsageTracker>(), TimeSpan.FromSeconds(60));
_logger = logger;
Logger = logger;
CanCreateSymbolicLinks = true;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
var target = Path.Join(Path.GetDirectoryName(VirtualRoot)!, "TestTarget.txt");
Expand All @@ -112,8 +160,8 @@ public Plugin(ILoggerFactory loggerFactory, IServerConfigurationManager configur
}
IgnoredFolders = Configuration.IgnoredFolders.ToHashSet();
Tracker.UpdateTimeout(TimeSpan.FromSeconds(Configuration.UsageTracker_StalledTimeInSeconds));
_logger.LogDebug("Virtual File System Location; {Path}", VirtualRoot);
_logger.LogDebug("Can create symbolic links; {Value}", CanCreateSymbolicLinks);
Logger.LogDebug("Virtual File System Location; {Path}", VirtualRoot);
Logger.LogDebug("Can create symbolic links; {Value}", CanCreateSymbolicLinks);
}

public void UpdateConfiguration()
Expand Down
14 changes: 10 additions & 4 deletions Shokofin/Web/ImageHostUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ public class ImageHostUrl : IAsyncActionFilter
/// </summary>
public static string BaseUrl { get => InternalBaseUrl ??= Plugin.Instance.BaseUrl; }

/// <summary>
/// The internal base path. Will be null if the base path haven't been used
/// yet.
/// </summary>
private static string? InternalBasePath { get; set; } = null;

/// <summary>
/// The current image host base path to use.
/// </summary>
public static string BasePath { get; private set; } = "";
public static string BasePath { get => InternalBasePath ??= Plugin.Instance.BasePath; }

private readonly object LockObj = new();

Expand All @@ -42,10 +48,10 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
uriBuilder.Query = "";
var uri = uriBuilder.ToString();
lock (LockObj) {
if (!string.Equals(uri, BaseUrl))
if (!string.Equals(uri, InternalBaseUrl))
InternalBaseUrl = uri;
if (!string.Equals(path, BasePath))
BasePath = path;
if (!string.Equals(path, InternalBasePath))
InternalBasePath = path;
}
}
await next();
Expand Down

0 comments on commit b720a39

Please sign in to comment.