diff --git a/source/DasBlog.Services/Site/SiteHttpContext.cs b/source/DasBlog.Services/Site/SiteHttpContext.cs new file mode 100644 index 00000000..42830886 --- /dev/null +++ b/source/DasBlog.Services/Site/SiteHttpContext.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace DasBlog.Services.Site +{ + public class SiteHttpContext + { + private static IHttpContextAccessor m_httpContextAccessor; + + public static HttpContext Current => m_httpContextAccessor.HttpContext; + + public static string AppBaseUrl => $"{Current.Request.Scheme}://{Current.Request.Host}{Current.Request.PathBase}"; + + internal static void Configure(IHttpContextAccessor contextAccessor) + { + m_httpContextAccessor = contextAccessor; + } + } + + public static class HttpContextExtensions + { + public static IApplicationBuilder UseHttpContext(this IApplicationBuilder app) + { + SiteHttpContext.Configure(app.ApplicationServices.GetRequiredService()); + return app; + } + } +} diff --git a/source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs b/source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs index 75f72c46..5956326c 100644 --- a/source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs +++ b/source/DasBlog.Web.Repositories/FileSystemBinaryManager.cs @@ -3,6 +3,7 @@ using DasBlog.Services.ConfigFile; using DasBlog.Services.FileManagement; using DasBlog.Services.FileManagement.Interfaces; +using DasBlog.Services.Site; using Microsoft.Extensions.Options; using newtelligence.DasBlog.Runtime; using System; @@ -18,7 +19,6 @@ public class FileSystemBinaryManager : IFileSystemBinaryManager private readonly IConfigFileService oembedProvidersService; private readonly IConfigFileService siteConfigFileService; private readonly ConfigFilePathsDataOption options; - private readonly string contentBinaryUrl; public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileService metaTagFileService, IConfigFileService oembedProvidersService, @@ -27,17 +27,24 @@ public FileSystemBinaryManager(IDasBlogSettings dasBlogSettings, IConfigFileServ this.dasBlogSettings = dasBlogSettings; this.metaTagFileService = metaTagFileService; this.oembedProvidersService = oembedProvidersService; - this.siteConfigFileService = siteConfigFileService; + this.siteConfigFileService = siteConfigFileService;; options = optionsAccessor.Value; - contentBinaryUrl = dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative); - var physBinaryPathUrl = new Uri(contentBinaryUrl); + Uri physBinaryPathUrl; - var loggingDataService = LoggingDataServiceFactory.GetService(Path.Combine(dasBlogSettings.WebRootDirectory, dasBlogSettings.SiteConfiguration.LogDir)); + if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root)) + { + physBinaryPathUrl = new Uri(dasBlogSettings.RelativeToRoot(options.BinaryUrlRelative)); + } + else + { + physBinaryPathUrl = new Uri(new Uri(SiteHttpContext.AppBaseUrl), options.BinaryUrlRelative); + } + var loggingDataService = LoggingDataServiceFactory.GetService(Path.Combine(dasBlogSettings.WebRootDirectory, dasBlogSettings.SiteConfiguration.LogDir)); var cdnManager = CdnManagerFactory.GetService(dasBlogSettings.SiteConfiguration.CdnFrom, dasBlogSettings.SiteConfiguration.CdnTo); - binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager); + this.binaryDataService = BinaryDataServiceFactory.GetService(options.BinaryFolder, physBinaryPathUrl, loggingDataService, cdnManager); } public string SaveFile(Stream inputFile, string fileName) diff --git a/source/DasBlog.Web.UI/Config/site.config b/source/DasBlog.Web.UI/Config/site.config index 8998da6e..b7682391 100644 --- a/source/DasBlog.Web.UI/Config/site.config +++ b/source/DasBlog.Web.UI/Config/site.config @@ -3,7 +3,7 @@ - https://localhost:5001/ + diff --git a/source/DasBlog.Web.UI/Settings/DasBlogSettings.cs b/source/DasBlog.Web.UI/Settings/DasBlogSettings.cs index ee25cbef..e211c63b 100644 --- a/source/DasBlog.Web.UI/Settings/DasBlogSettings.cs +++ b/source/DasBlog.Web.UI/Settings/DasBlogSettings.cs @@ -78,12 +78,26 @@ public DasBlogSettings(IWebHostEnvironment env, IOptionsMonitor site public string GetBaseUrl() { - return new Uri(SiteConfiguration.Root).AbsoluteUri; + if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root)) + { + return new Uri(SiteConfiguration.Root).AbsoluteUri; + } + else + { + return "/"; + } } public string RelativeToRoot(string relative) { - return new Uri(new Uri(SiteConfiguration.Root), relative).AbsoluteUri; + if (!string.IsNullOrWhiteSpace(SiteConfiguration.Root)) + { + return new Uri(new Uri(GetBaseUrl()), relative).AbsoluteUri; + } + else + { + return relative; + } } public string GetPermaLinkUrl(string entryId) diff --git a/source/DasBlog.Web.UI/Startup.cs b/source/DasBlog.Web.UI/Startup.cs index b1544f6c..0ebe6b3c 100644 --- a/source/DasBlog.Web.UI/Startup.cs +++ b/source/DasBlog.Web.UI/Startup.cs @@ -208,6 +208,7 @@ public void ConfigureServices(IServiceCollection services) .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() @@ -297,8 +298,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog app.UseRouting(); //if you've configured it at /blog or /whatever, set that pathbase so ~ will generate correctly - var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root); - var path = rootUri.AbsolutePath; + var path = "/"; + if (!string.IsNullOrWhiteSpace(dasBlogSettings.SiteConfiguration.Root)) + { + var rootUri = new Uri(dasBlogSettings.SiteConfiguration.Root); + path = rootUri.AbsolutePath; + } //Deal with path base and proxies that change the request path if (path != "/") @@ -432,6 +437,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDasBlog endpoints.MapControllerRoute( name: "default", "~/{controller=Home}/{action=Index}/{id?}"); }); + + app.UseHttpContext(); } /// diff --git a/source/DasBlog.Web.UI/TagHelpers/Post/PostToFacebookTagHelper.cs b/source/DasBlog.Web.UI/TagHelpers/Post/PostToFacebookTagHelper.cs index 11c2d6f2..fcf1eb1d 100644 --- a/source/DasBlog.Web.UI/TagHelpers/Post/PostToFacebookTagHelper.cs +++ b/source/DasBlog.Web.UI/TagHelpers/Post/PostToFacebookTagHelper.cs @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", "dasblog-a-share-facebook"); output.Attributes.SetAttribute("href", string.Format(FACEBOOK_SHARE_URL, - UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri))); + UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)))); var content = await output.GetChildContentAsync(); diff --git a/source/DasBlog.Web.UI/TagHelpers/Post/PostToLinkedInTagHelper.cs b/source/DasBlog.Web.UI/TagHelpers/Post/PostToLinkedInTagHelper.cs index c7d0be4e..b87be23e 100644 --- a/source/DasBlog.Web.UI/TagHelpers/Post/PostToLinkedInTagHelper.cs +++ b/source/DasBlog.Web.UI/TagHelpers/Post/PostToLinkedInTagHelper.cs @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", "dasblog-a-share-linkedin"); output.Attributes.SetAttribute("href", string.Format(LINKEDIN_SHARE_URL, - UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri))); + UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)))); var content = await output.GetChildContentAsync(); diff --git a/source/DasBlog.Web.UI/TagHelpers/Post/PostToRedditTagHelper.cs b/source/DasBlog.Web.UI/TagHelpers/Post/PostToRedditTagHelper.cs index 845b0f4c..3456f6bb 100644 --- a/source/DasBlog.Web.UI/TagHelpers/Post/PostToRedditTagHelper.cs +++ b/source/DasBlog.Web.UI/TagHelpers/Post/PostToRedditTagHelper.cs @@ -24,7 +24,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.SetAttribute("class", "dasblog-a-share-reddit"); output.Attributes.SetAttribute("href", string.Format(REDDIT_SHARE_URL, - UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri), + UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)), UrlEncoder.Default.Encode(Post.Title) )); diff --git a/source/DasBlog.Web.UI/TagHelpers/Post/PostToTwitterTagHelper.cs b/source/DasBlog.Web.UI/TagHelpers/Post/PostToTwitterTagHelper.cs index b88aaec1..b463c47e 100644 --- a/source/DasBlog.Web.UI/TagHelpers/Post/PostToTwitterTagHelper.cs +++ b/source/DasBlog.Web.UI/TagHelpers/Post/PostToTwitterTagHelper.cs @@ -29,7 +29,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.Attributes.SetAttribute("class", "dasblog-a-share-twitter"); output.Attributes.SetAttribute("href", string.Format(TWITTER_SHARE_URL, - UrlEncoder.Default.Encode(new Uri(new Uri(dasBlogSettings.GetBaseUrl()), Post.PermaLink).AbsoluteUri), + UrlEncoder.Default.Encode(dasBlogSettings.RelativeToRoot(Post.PermaLink)), UrlEncoder.Default.Encode(Post.Title), UrlEncoder.Default.Encode(author.TrimStart('@')), RetrieveFormattedCategories(Post.Categories)));