Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support importing redirects with culture in path #35

Open
wants to merge 1 commit into
base: v13/main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Skybrud.Umbraco.Redirects.Import/RedirectsImportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using Skybrud.Essentials.Common;
using Skybrud.Umbraco.Redirects.Import.Importers;
Expand Down Expand Up @@ -110,6 +111,21 @@ public bool TryGetContent(string route, [NotNullWhen(true)] out IPublishedConten
return result != null;
}

/// <summary>
/// Attempts to get the culture for the specified <paramref name="route"/>
/// </summary>
/// <param name="route">The route of the content.</param>
/// <param name="culture">When this method returns, holds the matching culture.</param>
/// <returns><see langword="true"/> if successful; otherwise, <see langword="false"/>.</returns>
public bool TryGetCulture(string route, [NotNullWhen(true)] out string? culture) {
culture = DomainService
.GetAll(includeWildcards: false)
.FirstOrDefault(d => route.InvariantStartsWith($"/{d.LanguageIsoCode}"))?
.LanguageIsoCode;

return culture != null;
}

/// <summary>
/// Attempts to get a <see cref="IPublishedContent"/> instance representing the media with the specified <paramref name="id"/>.
/// </summary>
Expand Down Expand Up @@ -400,6 +416,7 @@ public virtual void ParseDestination(DataRow row, RedirectImportItem item) {
string? destinationUrl = row.GetString(Columns.DestinationUrl);
string? destinationQuery = null;
string? destinationFragment = null;
string? culture = null;

IPublishedContent? destination = null;

Expand Down Expand Up @@ -511,6 +528,11 @@ public virtual void ParseDestination(DataRow row, RedirectImportItem item) {
destination = content;
destinationUrl = content.Url();
destinationType = RedirectDestinationType.Content;
} else if (TryGetCulture(destinationUrl, out culture) &&
TryGetContent(destinationUrl.Replace($"/{culture}", "", StringComparison.InvariantCultureIgnoreCase), out content)) {
destination = content;
destinationUrl = content.Url(culture);
destinationType = RedirectDestinationType.Content;
} else {
item.Errors.Add($"No destination found with URL '{destinationUrl}'. ({route}) ({item.AddOptions.RootNodeId})");
return;
Expand All @@ -531,7 +553,6 @@ public virtual void ParseDestination(DataRow row, RedirectImportItem item) {
if (!string.IsNullOrWhiteSpace(value)) destinationFragment = value;
}

string? culture = null;
if (Columns.DestinationCulture is not null) {
string? value = row.GetString(Columns.DestinationCulture);
if (!string.IsNullOrWhiteSpace(value)) {
Expand Down