Skip to content

Commit

Permalink
fix: append base path to image urls
Browse files Browse the repository at this point in the history
- Append the base path to image urls served by the plugin, instead of overwriting the existing base path set on the base url.
  • Loading branch information
revam committed Jul 27, 2024
1 parent 7f362fb commit 4890504
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Shokofin/API/Models/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public virtual bool IsAvailable
/// </remarks>
/// <returns>The image URL</returns>
public string ToURLString()
=> new Uri(new Uri(Web.ImageHostUrl.Value), $"/Plugin/Shokofin/Host/Image/{Source}/{Type}/{ID}").ToString();
=> new Uri(new Uri(Web.ImageHostUrl.BaseUrl), $"{Web.ImageHostUrl.BasePath}/Plugin/Shokofin/Host/Image/{Source}/{Type}/{ID}").ToString();
}

/// <summary>
Expand Down
21 changes: 15 additions & 6 deletions Shokofin/Web/ImageHostUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ namespace Shokofin.Web;
public class ImageHostUrl : IAsyncActionFilter
{
/// <summary>
/// The current image host url base to use.
/// The current image host base url to use.
/// </summary>
public static string Value { get; private set; } = "http://localhost:8096/";
public static string BaseUrl { get; private set; } = "http://localhost:8096/";

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

private readonly object LockObj = new();

Expand All @@ -26,12 +31,16 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
var uriBuilder = new UriBuilder(request.Scheme, request.Host.Host, request.Host.Port ?? (request.Scheme == "https" ? 443 : 80), $"{request.PathBase}{request.Path}", request.QueryString.HasValue ? request.QueryString.Value : null);
var result = RemoteImagesRegex.Match(uriBuilder.Path);
if (result.Success) {
uriBuilder.Path = result.Length == uriBuilder.Path.Length ? "/" : uriBuilder.Path[..^result.Length] + "/";
var path = result.Length == uriBuilder.Path.Length ? "" : uriBuilder.Path[..^result.Length];
uriBuilder.Path = "";
uriBuilder.Query = "";
var uri = uriBuilder.ToString();
lock (LockObj)
if (!string.Equals(uri, Value))
Value = uri;
lock (LockObj) {
if (!string.Equals(uri, BaseUrl))
BaseUrl = uri;
if (!string.Equals(path, BasePath))
BasePath = path;
}
}
await next();
}
Expand Down

0 comments on commit 4890504

Please sign in to comment.