diff --git a/src/Lumina.Domain/Common/DependencyInjection/DomainLayerServices.cs b/src/Lumina.Domain/Common/DependencyInjection/DomainLayerServices.cs index c81e80d0..b96b2ced 100644 --- a/src/Lumina.Domain/Common/DependencyInjection/DomainLayerServices.cs +++ b/src/Lumina.Domain/Common/DependencyInjection/DomainLayerServices.cs @@ -40,6 +40,7 @@ public static class DomainLayerServices services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddSingleton(); return services; } diff --git a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemStructureSeedService.cs b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemStructureSeedService.cs new file mode 100644 index 00000000..86f85951 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/FileSystemStructureSeedService.cs @@ -0,0 +1,63 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Environment; +using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services; + +/// +/// Service for seeding initial file system structure. +/// +internal class FileSystemStructureSeedService : IFileSystemStructureSeedService +{ + private readonly IEnvironmentContext _environmentContext; + private readonly IPathService _pathService; + + /// + /// Initializes a new instance of the class. + /// + /// Injected facade service for environment contextual services. + /// Injected service for handling file system paths. + public FileSystemStructureSeedService(IEnvironmentContext environmentContext, IPathService pathService) + { + _environmentContext = environmentContext; + _pathService = pathService; + } + + /// + /// Sets up the default file system directories needed by the application. + /// + /// The base path where the directories will be created relative to. + /// An representing either a successfull operation, or an error. + public ErrorOr SetDefaultDirectories(string rootPath) + { + // make sure the root path exists + ErrorOr rootPathIdResult = FileSystemPathId.Create(rootPath); + if (rootPathIdResult.IsError) + return rootPathIdResult.Errors; + + // create the libraries directory + ErrorOr librariesPathResult = _pathService.CombinePath(rootPath, "libraries"); + if (librariesPathResult.IsError) + return librariesPathResult.Errors; + + ErrorOr libraryPathIdResult = FileSystemPathId.Create(librariesPathResult.Value); + if (libraryPathIdResult.IsError) + return libraryPathIdResult.Errors; + + ErrorOr librariesDirectoryExistsResult = _environmentContext.DirectoryProviderService.DirectoryExists(libraryPathIdResult.Value); + if (librariesDirectoryExistsResult.IsError) + return librariesDirectoryExistsResult.Errors; + + // only create it if it doesn't already exist + if (!librariesDirectoryExistsResult.Value) + { + ErrorOr createDirectoryResult = _environmentContext.DirectoryProviderService.CreateDirectory(rootPathIdResult.Value, "libraries"); + if (createDirectoryResult.IsError) + return createDirectoryResult.Errors; + } + return Result.Created; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/IFileSystemStructureSeedService.cs b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/IFileSystemStructureSeedService.cs new file mode 100644 index 00000000..402ea42d --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/FileSystemManagementBoundedContext/FileSystemManagementAggregate/Services/IFileSystemStructureSeedService.cs @@ -0,0 +1,18 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services; + +/// +/// Interface for the service for seeding initial file system structure. +/// +public interface IFileSystemStructureSeedService +{ + /// + /// Sets up the default file system directories needed by the application. + /// + /// The base path where the directories will be created relative to. + /// An representing either a successfull operation, or an error. + ErrorOr SetDefaultDirectories(string rootPath); +} diff --git a/src/Lumina.Infrastructure/appsettings.shared.json b/src/Lumina.Infrastructure/appsettings.shared.json index 366d50a5..215c4274 100644 --- a/src/Lumina.Infrastructure/appsettings.shared.json +++ b/src/Lumina.Infrastructure/appsettings.shared.json @@ -1,6 +1,6 @@ { "MediaSettings": { - "RootDirectory": "wwwroot/media" + "RootDirectory": "media" }, "CommonSettings": { "Theme": "LuminaDark" diff --git a/src/Lumina.Presentation.Api/Program.cs b/src/Lumina.Presentation.Api/Program.cs index bd9efc1d..702cf680 100644 --- a/src/Lumina.Presentation.Api/Program.cs +++ b/src/Lumina.Presentation.Api/Program.cs @@ -1,9 +1,11 @@ #region ========================================================================= USING ===================================================================================== +using ErrorOr; using FastEndpoints; using Lumina.Application.Common.DependencyInjection; using Lumina.DataAccess.Common.DependencyInjection; using Lumina.DataAccess.Core.UoW; using Lumina.Domain.Common.DependencyInjection; +using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services; using Lumina.Infrastructure.Common.DependencyInjection; using Lumina.Presentation.Api.Common.DependencyInjection; using Microsoft.AspNetCore.Builder; @@ -92,7 +94,7 @@ public class Program app.UseCors("AllowAll"); - //app.UseExceptionHandler("/error"); // uses a middleware which re-executes the request to the error path + //app.UseExceptionHandler("/error"); // uses a middleware which re-executes the request to the error path //app.UseHttpsRedirection(); @@ -120,12 +122,23 @@ public class Program .WithDotNetFlag(true); }); + // create a directory relative to the application's startup directory, and use it to store static files that are served at the /media route on the API string mediaRootDirectoryPathSetting = app.Configuration.GetValue("MediaSettings:RootDirectory") ?? string.Empty; string mediaRootPath = Path.Combine(AppContext.BaseDirectory, mediaRootDirectoryPathSetting); if (!Directory.Exists(mediaRootPath)) Directory.CreateDirectory(mediaRootPath); + // ensure the default file system structure is present + using (IServiceScope scope = app.Services.CreateScope()) + { + IServiceProvider services = scope.ServiceProvider; + IFileSystemStructureSeedService context = services.GetRequiredService(); + ErrorOr setDefaultDirectoriesResult = context.SetDefaultDirectories(mediaRootPath); + if (setDefaultDirectoriesResult.IsError) + throw new InvalidOperationException("Could not create default file system directories structure: " + setDefaultDirectoriesResult.FirstError.Description); + } + app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(mediaRootPath),