From a60136c6b6170aff60d2a64d011223b1202d5a60 Mon Sep 17 00:00:00 2001 From: Mark Downie Date: Sat, 2 Dec 2023 22:35:58 -0500 Subject: [PATCH] The assumption is that upon first deployment the site will start up without a root URL defined. dasblog will allow you to login and change the root url through the web page and update the root URL. Some functions will still not work so this should still be the first thing you do. --- .../DasBlog.Services/Site/SiteHttpContext.cs | 34 +++++++++++++++++++ .../FileSystemBinaryManager.cs | 19 +++++++---- source/DasBlog.Web.UI/Config/site.config | 2 +- .../Settings/DasBlogSettings.cs | 18 ++++++++-- source/DasBlog.Web.UI/Startup.cs | 11 ++++-- .../Post/PostToFacebookTagHelper.cs | 2 +- .../Post/PostToLinkedInTagHelper.cs | 2 +- .../TagHelpers/Post/PostToRedditTagHelper.cs | 2 +- .../TagHelpers/Post/PostToTwitterTagHelper.cs | 2 +- 9 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 source/DasBlog.Services/Site/SiteHttpContext.cs diff --git a/source/DasBlog.Services/Site/SiteHttpContext.cs b/source/DasBlog.Services/Site/SiteHttpContext.cs new file mode 100644 index 000000000..428308860 --- /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 75f72c46f..5956326c6 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 8998da6e0..b7682391b 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 ee25cbeff..e211c63bc 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 b1544f6c9..0ebe6b3cf 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 11c2d6f2f..fcf1eb1df 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 c7d0be4ef..b87be23e6 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 845b0f4c3..3456f6bb9 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 b88aaec1c..b463c47e3 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)));