mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 20:01:10 +00:00
Merge pull request #101 from BinaryAlley/feature/implement-directory-structure-seed-service
Implements directory structure seed service
This commit is contained in:
5 files changed
+97
-2
No files matched your search
@@ -40,6 +40,7 @@ public static class DomainLayerServices
|
||||
services.AddScoped<IUnixPathStrategy, UnixPathStrategy>();
|
||||
services.AddScoped<IWindowsPathStrategy, WindowsPathStrategy>();
|
||||
services.AddScoped<IOperatingSystemInfo, OperatingSystemInfo>();
|
||||
services.AddScoped<IFileSystemStructureSeedService, FileSystemStructureSeedService>();
|
||||
services.AddSingleton<IFileSystem, FileSystem>();
|
||||
return services;
|
||||
}
|
||||
|
||||
+63
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Service for seeding initial file system structure.
|
||||
/// </summary>
|
||||
internal class FileSystemStructureSeedService : IFileSystemStructureSeedService
|
||||
{
|
||||
private readonly IEnvironmentContext _environmentContext;
|
||||
private readonly IPathService _pathService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FileSystemStructureSeedService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="environmentContext">Injected facade service for environment contextual services.</param>
|
||||
/// <param name="pathService">Injected service for handling file system paths.</param>
|
||||
public FileSystemStructureSeedService(IEnvironmentContext environmentContext, IPathService pathService)
|
||||
{
|
||||
_environmentContext = environmentContext;
|
||||
_pathService = pathService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up the default file system directories needed by the application.
|
||||
/// </summary>
|
||||
/// <param name="rootPath">The base path where the directories will be created relative to.</param>
|
||||
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
|
||||
public ErrorOr<Created> SetDefaultDirectories(string rootPath)
|
||||
{
|
||||
// make sure the root path exists
|
||||
ErrorOr<FileSystemPathId> rootPathIdResult = FileSystemPathId.Create(rootPath);
|
||||
if (rootPathIdResult.IsError)
|
||||
return rootPathIdResult.Errors;
|
||||
|
||||
// create the libraries directory
|
||||
ErrorOr<string> librariesPathResult = _pathService.CombinePath(rootPath, "libraries");
|
||||
if (librariesPathResult.IsError)
|
||||
return librariesPathResult.Errors;
|
||||
|
||||
ErrorOr<FileSystemPathId> libraryPathIdResult = FileSystemPathId.Create(librariesPathResult.Value);
|
||||
if (libraryPathIdResult.IsError)
|
||||
return libraryPathIdResult.Errors;
|
||||
|
||||
ErrorOr<bool> 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<FileSystemPathId> createDirectoryResult = _environmentContext.DirectoryProviderService.CreateDirectory(rootPathIdResult.Value, "libraries");
|
||||
if (createDirectoryResult.IsError)
|
||||
return createDirectoryResult.Errors;
|
||||
}
|
||||
return Result.Created;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for the service for seeding initial file system structure.
|
||||
/// </summary>
|
||||
public interface IFileSystemStructureSeedService
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets up the default file system directories needed by the application.
|
||||
/// </summary>
|
||||
/// <param name="rootPath">The base path where the directories will be created relative to.</param>
|
||||
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
|
||||
ErrorOr<Created> SetDefaultDirectories(string rootPath);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"MediaSettings": {
|
||||
"RootDirectory": "wwwroot/media"
|
||||
"RootDirectory": "media"
|
||||
},
|
||||
"CommonSettings": {
|
||||
"Theme": "LuminaDark"
|
||||
|
||||
@@ -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<string>("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<IFileSystemStructureSeedService>();
|
||||
ErrorOr<Created> 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),
|
||||
|
||||
Reference in new issue
Block a user