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

Add custom Examine FileSystemDirectoryFactory using Umbraco SiteName #15571

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private IDirectoryFactory CreateFactory()
case LuceneDirectoryFactory.SyncedTempFileSystemDirectoryFactory:
return _services.GetRequiredService<SyncedFileSystemDirectoryFactory>();
case LuceneDirectoryFactory.TempFileSystemDirectoryFactory:
return _services.GetRequiredService<TempEnvFileSystemDirectoryFactory>();
return _services.GetRequiredService<UmbracoTempEnvFileSystemDirectoryFactory>();
case LuceneDirectoryFactory.Default:
default:
return _services.GetRequiredService<FileSystemDirectoryFactory>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Examine;
using Examine.Lucene.Directories;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;

namespace Umbraco.Cms.Infrastructure.Examine.DependencyInjection;

Expand All @@ -22,8 +24,26 @@ public static IUmbracoBuilder AddExamineIndexes(this IUmbracoBuilder umbracoBuil

services.AddExamine();

// Create the indexes
services
services.AddSingleton<UmbracoTempEnvFileSystemDirectoryFactory>();
services.RemoveAll<SyncedFileSystemDirectoryFactory>();
services.AddSingleton<SyncedFileSystemDirectoryFactory>(
s =>
{
var baseDir = s.GetRequiredService<IApplicationRoot>().ApplicationRoot;

var tempDir = UmbracoTempEnvFileSystemDirectoryFactory.GetTempPath(
s.GetRequiredService<IApplicationIdentifier>(), s.GetRequiredService<IHostingEnvironment>());

return ActivatorUtilities.CreateInstance<SyncedFileSystemDirectoryFactory>(
s,
new object[]
{
new DirectoryInfo(tempDir), s.GetRequiredService<IApplicationRoot>().ApplicationRoot
});
});

// Create the indexes
services
.AddExamineLuceneIndex<UmbracoContentIndex, ConfigurationEnabledDirectoryFactory>(Constants.UmbracoIndexes
.InternalIndexName)
.AddExamineLuceneIndex<UmbracoContentIndex, ConfigurationEnabledDirectoryFactory>(Constants.UmbracoIndexes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Examine;
using Examine.Lucene.Directories;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;

namespace Umbraco.Cms.Infrastructure.Examine
{
/// <summary>
/// Custom version of https://github.com/Shazwazza/Examine/blob/release/3.0/src/Examine.Lucene/Directories/TempEnvFileSystemDirectoryFactory.cs that includes the Umbraco SiteName property in the path hash
/// </summary>
public class UmbracoTempEnvFileSystemDirectoryFactory : FileSystemDirectoryFactory
{
public UmbracoTempEnvFileSystemDirectoryFactory(
IApplicationIdentifier applicationIdentifier,
ILockFactory lockFactory,
IHostingEnvironment hostingEnvironment)
: base(new DirectoryInfo(GetTempPath(applicationIdentifier, hostingEnvironment)), lockFactory)
{
}

public static string GetTempPath(IApplicationIdentifier applicationIdentifier, IHostingEnvironment hostingEnvironment)
{
var hashString = hostingEnvironment.SiteName + "::" + applicationIdentifier.GetApplicationUniqueIdentifier();
var appDomainHash = hashString.GenerateHash();

var cachePath = Path.Combine(
Path.GetTempPath(),
"ExamineIndexes",
//include the appdomain hash is just a safety check, for example if a website is moved from worker A to worker B and then back
// to worker A again, in theory the %temp% folder should already be empty but we really want to make sure that its not
// utilizing an old index
appDomainHash);

return cachePath;
}
}
}
Loading