40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
using Microsoft.Extensions.Options;
|
|
|
|
namespace MilkStream.Client.Services;
|
|
|
|
/// <summary>
|
|
/// Builds media URLs (original, thumbnail, preview) authenticated via a token query parameter.
|
|
/// Media is served by <c>MediaController</c> through <c><img></c>-accessible URLs, so the
|
|
/// JWT is passed as a <c>token</c> query parameter rather than an Authorization header.
|
|
/// </summary>
|
|
/// <param name="options">Service configuration options.</param>
|
|
/// <param name="loginService">The login service providing the current auth token.</param>
|
|
public sealed class MediaService(IOptions<ServiceOptions> options, LoginService loginService) {
|
|
string BaseUrl => options.Value.BaseUrl.TrimEnd('/');
|
|
|
|
string TokenParam {
|
|
get {
|
|
var token = loginService.AuthInfo?.Token;
|
|
return string.IsNullOrEmpty(token) ? "" : $"?token={token}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the thumbnail URL for an asset.
|
|
/// </summary>
|
|
/// <param name="id">The asset ID.</param>
|
|
public string ThumbUrl(Guid id) => $"{BaseUrl}/api/media/thumb/{id}{TokenParam}";
|
|
|
|
/// <summary>
|
|
/// Gets the preview URL for an asset.
|
|
/// </summary>
|
|
/// <param name="id">The asset ID.</param>
|
|
public string PreviewUrl(Guid id) => $"{BaseUrl}/api/media/preview/{id}{TokenParam}";
|
|
|
|
/// <summary>
|
|
/// Gets the original image URL for an asset.
|
|
/// </summary>
|
|
/// <param name="id">The asset ID.</param>
|
|
public string OriginalUrl(Guid id) => $"{BaseUrl}/api/media/original/{id}{TokenParam}";
|
|
}
|