2022-11-19 21:30:43 +00:00
using Elsa.Http.Options ;
using Microsoft.Extensions.Options ;
2023-03-03 21:47:23 +00:00
namespace Elsa.Http.Services ;
2022-11-19 21:30:43 +00:00
2023-09-24 22:38:06 +00:00
/// <inheritdoc />
2022-11-19 21:30:43 +00:00
public class DefaultAbsoluteUrlProvider : IAbsoluteUrlProvider
{
private readonly IOptions < HttpActivityOptions > _options ;
2023-09-24 22:38:06 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="DefaultAbsoluteUrlProvider"/> class.
/// </summary>
2022-11-19 21:30:43 +00:00
public DefaultAbsoluteUrlProvider ( IOptions < HttpActivityOptions > options ) = > _options = options ;
2023-09-24 22:38:06 +00:00
/// <inheritdoc />
2022-11-19 21:30:43 +00:00
public Uri ToAbsoluteUrl ( string relativePath )
{
var baseUrl = _options . Value . BaseUrl ;
if ( baseUrl = = null )
throw new Exception (
"There was no base URL configured, which means no absolute URL can be generated from outside the context of an HTTP request. Please make sure that `HttpActivityOptions` is configured correctly. The configuration key used in most Elsa samples is usually: \"Elsa:Server:BaseUrl\"" ) ;
// To not lose any base path information, we need to ensure that:
// - Base path ends with a slash.
// - Relative path does NOT start with a slash.
var baseUri = new Uri ( baseUrl . ToString ( ) . TrimEnd ( '/' ) + '/' ) ;
relativePath = relativePath . TrimStart ( '/' ) ;
return new Uri ( baseUri , relativePath ) ;
}
}