forked from wkallhof/Simple301
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from patrickdemooij9/feature/ImageRedirects
Allow for media redirects
- Loading branch information
Showing
7 changed files
with
128 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
source/SimpleRedirects.Core/Middleware/SimpleRedirectsMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using SimpleRedirects.Core.Options; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Core.Web; | ||
|
||
namespace SimpleRedirects.Core.Middleware | ||
{ | ||
public class SimpleRedirectsMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly RedirectRepository _redirectRepository; | ||
private readonly IRuntimeState _runtimeState; | ||
private readonly IUmbracoContextAccessor _umbracoContextAccessor; | ||
|
||
private SimpleRedirectsOptions _config; | ||
|
||
public SimpleRedirectsMiddleware(RequestDelegate next, | ||
RedirectRepository redirectRepository, | ||
IRuntimeState runtimeState, | ||
IUmbracoContextAccessor umbracoContextAccessor, | ||
IOptionsMonitor<SimpleRedirectsOptions> options) | ||
{ | ||
_next = next; | ||
_redirectRepository = redirectRepository; | ||
_runtimeState = runtimeState; | ||
_umbracoContextAccessor = umbracoContextAccessor; | ||
_config = options.CurrentValue; | ||
options.OnChange((newValue) => _config = newValue); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (_runtimeState.Level != RuntimeLevel.Run) | ||
{ | ||
await _next.Invoke(context); | ||
return; | ||
} | ||
|
||
var pathAndQuery = context.Request.GetEncodedPathAndQuery(); | ||
|
||
if (pathAndQuery.IndexOf("/umbraco", StringComparison.InvariantCultureIgnoreCase) == 0) | ||
{ | ||
await _next(context); | ||
return; | ||
} | ||
|
||
if (_config.OnlyRedirectOn404) | ||
{ | ||
context.Response.OnStarting(() => | ||
{ | ||
if (context.Response.StatusCode != (int)HttpStatusCode.NotFound) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
HandleRedirect(context); | ||
return Task.CompletedTask; | ||
}); | ||
await _next(context); | ||
} | ||
else | ||
{ | ||
if (!HandleRedirect(context)) | ||
{ | ||
await _next(context); | ||
} | ||
} | ||
} | ||
|
||
private bool HandleRedirect(HttpContext context) | ||
{ | ||
var url = new Uri(context.Request.GetEncodedUrl()); | ||
var matchedRedirect = _redirectRepository.FindRedirect(url); | ||
if (matchedRedirect == null) | ||
{ | ||
return false; | ||
}; | ||
|
||
var isPerm = matchedRedirect.RedirectCode == (int)HttpStatusCode.MovedPermanently; | ||
context.Response.Redirect(matchedRedirect.GetNewUrl(url), isPerm); | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
using System.Configuration; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SimpleRedirects.Core.Components; | ||
using SimpleRedirects.Core.Middleware; | ||
using SimpleRedirects.Core.Options; | ||
using SimpleRedirects.Core.Utilities.Caching; | ||
using Umbraco.Cms.Core.Composing; | ||
using Umbraco.Cms.Core.DependencyInjection; | ||
using Umbraco.Cms.Core.Routing; | ||
using Umbraco.Cms.Web.Common.ApplicationBuilder; | ||
using Umbraco.Extensions; | ||
|
||
namespace SimpleRedirects.Core | ||
{ | ||
public class RedirectUserComposer : IUserComposer | ||
public class RedirectUserComposer : IComposer | ||
{ | ||
public void Compose(IUmbracoBuilder builder) | ||
{ | ||
builder.Dashboards().Add<RedirectDashboard>(); | ||
builder.Components().Append<DatabaseUpgradeComponent>(); | ||
builder.ContentFinders().InsertBefore<ContentFinderByUrl, RedirectContentFinder>(); | ||
|
||
builder.Services.AddUnique<RedirectRepository>(); | ||
builder.Services.AddUnique<ICacheManager, CacheManager>(); | ||
|
||
builder.Services.Configure<SimpleRedirectsOptions>(builder.Config.GetSection( | ||
SimpleRedirectsOptions.Position)); | ||
SimpleRedirectsOptions.Position)); | ||
var onlyRedirectOn404 = builder.Config.GetSection(SimpleRedirectsOptions.Position)?.Get<SimpleRedirectsOptions>()?.OnlyRedirectOn404 ?? false; | ||
|
||
builder.Services.Configure<UmbracoPipelineOptions>(options => { | ||
options.AddFilter(new UmbracoPipelineFilter( | ||
"SimpleRedirects", | ||
applicationBuilder => | ||
{ | ||
if (!onlyRedirectOn404) | ||
applicationBuilder.UseMiddleware<SimpleRedirectsMiddleware>(); | ||
}, | ||
applicationBuilder => { | ||
if (onlyRedirectOn404) | ||
applicationBuilder.UseMiddleware<SimpleRedirectsMiddleware>(); | ||
}, | ||
applicationBuilder => { } | ||
)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+47.7 KB
...-tacos-with-salsa-vege-241652656-oavgzeva9xuxn2j080ugxbxc6vylzmix6enq0p220o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.