|
1 | | -using System.Threading.Tasks; |
| 1 | +using Microsoft.Extensions.DependencyInjection; |
2 | 2 | using Microsoft.Extensions.Logging; |
| 3 | +using Umbraco.Cms.Core.DependencyInjection; |
3 | 4 | using Umbraco.Cms.Core.Events; |
4 | 5 | using Umbraco.Cms.Core.Models; |
| 6 | +using Umbraco.Cms.Core.Models.PublishedContent; |
5 | 7 | using Umbraco.Cms.Core.Persistence.Repositories; |
| 8 | +using Umbraco.Cms.Core.Routing; |
6 | 9 | using Umbraco.Cms.Core.Scoping; |
7 | 10 |
|
8 | 11 | namespace Umbraco.Cms.Core.Services; |
9 | 12 |
|
10 | 13 | internal sealed class RedirectUrlService : RepositoryService, IRedirectUrlService |
11 | 14 | { |
12 | 15 | private readonly IRedirectUrlRepository _redirectUrlRepository; |
| 16 | + private readonly IPublishedUrlProvider _publishedUrlProvider; |
13 | 17 |
|
14 | 18 | public RedirectUrlService( |
15 | 19 | ICoreScopeProvider provider, |
16 | 20 | ILoggerFactory loggerFactory, |
17 | 21 | IEventMessagesFactory eventMessagesFactory, |
18 | | - IRedirectUrlRepository redirectUrlRepository) |
19 | | - : base(provider, loggerFactory, eventMessagesFactory) => |
| 22 | + IRedirectUrlRepository redirectUrlRepository, |
| 23 | + IPublishedUrlProvider publishedUrlProvider) |
| 24 | + : base(provider, loggerFactory, eventMessagesFactory) |
| 25 | + { |
20 | 26 | _redirectUrlRepository = redirectUrlRepository; |
| 27 | + _publishedUrlProvider = publishedUrlProvider; |
| 28 | + } |
| 29 | + |
| 30 | + [Obsolete("Use the constructor taking all parameters. Scheduled for removal in Umbraco 19")] |
| 31 | + public RedirectUrlService( |
| 32 | + ICoreScopeProvider provider, |
| 33 | + ILoggerFactory loggerFactory, |
| 34 | + IEventMessagesFactory eventMessagesFactory, |
| 35 | + IRedirectUrlRepository redirectUrlRepository) |
| 36 | + : this(provider, |
| 37 | + loggerFactory, |
| 38 | + eventMessagesFactory, |
| 39 | + StaticServiceProvider.Instance.GetRequiredService<IRedirectUrlRepository>(), |
| 40 | + StaticServiceProvider.Instance.GetRequiredService<IPublishedUrlProvider>()) |
| 41 | + { |
| 42 | + } |
21 | 43 |
|
22 | 44 | public void Register(string url, Guid contentKey, string? culture = null) |
23 | 45 | { |
24 | 46 | using (ICoreScope scope = ScopeProvider.CreateCoreScope()) |
25 | 47 | { |
| 48 | + var newUrl = _publishedUrlProvider.GetUrl(contentKey, UrlMode.Relative, culture).TrimEnd('/'); |
| 49 | + IEnumerable<IRedirectUrl> allRedirectUrls = _redirectUrlRepository.GetContentUrls(contentKey); |
| 50 | + |
| 51 | + // If there's a redirect URL that points to the new URL, delete it. |
| 52 | + foreach (IRedirectUrl redirectUrl in allRedirectUrls) |
| 53 | + { |
| 54 | + if (redirectUrl.Url == newUrl) |
| 55 | + { |
| 56 | + _redirectUrlRepository.Delete(redirectUrl.Key); |
| 57 | + } |
| 58 | + } |
| 59 | + |
26 | 60 | IRedirectUrl? redir = _redirectUrlRepository.Get(url, contentKey, culture); |
27 | 61 | if (redir != null) |
28 | 62 | { |
|
0 commit comments