FileDatabase Improvements

- small bugfixes
 - Startup configuration to load an arbitrary file database for the API
This commit is contained in:
daniel-c-harvey
2025-09-04 14:19:57 -04:00
parent 27522c1e1c
commit c4c4662c67
5 changed files with 52 additions and 6 deletions
@@ -34,12 +34,21 @@ public class DirectoryIndexData : IndexData
}
}
/// <summary>
/// Entry data for vault index serialization
/// </summary>
public class VaultEntryData
{
public EntryKey Key { get; set; } = null!;
public MetaData Value { get; set; } = null!;
}
/// <summary>
/// Serializable data for vault indexes
/// </summary>
public class VaultIndexData : IndexData
{
public List<(EntryKey Key, MetaData Value)> Entries { get; set; } = new();
public List<VaultEntryData> Entries { get; set; } = new();
public VaultIndexData(string indexKey) : base(indexKey) { }
@@ -47,7 +56,7 @@ public class VaultIndexData : IndexData
{
var data = new VaultIndexData(index.GetKey())
{
Entries = index.Entries.Select(kvp => (kvp.Key, kvp.Value)).ToList()
Entries = index.Entries.Select(kvp => new VaultEntryData { Key = kvp.Key, Value = kvp.Value }).ToList()
};
return data;
}
@@ -92,9 +101,9 @@ public class VaultIndex : IndexData, IIndex
{
Entries = new StructuralMap<EntryKey, MetaData>();
// Load entries from data
foreach (var (key, value) in indexData.Entries)
foreach (var entry in indexData.Entries)
{
Entries.Set(key, value);
Entries.Set(entry.Key, entry.Value);
}
}
@@ -156,8 +156,15 @@ public static class MimeTypeExtensions
{ ".bmp", "image/bmp" }
};
private static readonly Dictionary<string, string> Extensions =
MimeTypes.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
private static readonly Dictionary<string, string> Extensions = new()
{
{ "image/jpeg", ".jpg" },
{ "image/png", ".png" },
{ "image/gif", ".gif" },
{ "image/webp", ".webp" },
{ "image/svg+xml", ".svg" },
{ "image/bmp", ".bmp" }
};
public static string GetMimeType(string extension)
{
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
@@ -5,6 +7,9 @@ namespace DeepDrftContent.FileDatabase.Models;
/// </summary>
/// <param name="MediaKey">The key used to identify the media file</param>
/// <param name="Extension">The file extension of the media</param>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(MetaData), typeDiscriminator: "media")]
[JsonDerivedType(typeof(ImageMetaData), typeDiscriminator: "image")]
public record MetaData(string MediaKey, string Extension);
/// <summary>
@@ -0,0 +1,7 @@
namespace DeepDrftContent.Models
{
public class FileDatabaseSettings
{
public required string VaultPath { get; set; }
}
}
+18
View File
@@ -0,0 +1,18 @@
using DeepDrftContent.Models;
namespace DeepDrftContent
{
public static class Startup
{
public static void ConfigureDomainServices(WebApplicationBuilder builder)
{
// File Database
builder.Configuration.AddJsonFile("environment/filedatabase.json", optional: false, reloadOnChange: true);
var fileDatabaseSettings = builder.Configuration.GetSection(nameof(FileDatabaseSettings)).Get<FileDatabaseSettings>();
if (fileDatabaseSettings is null) { throw new Exception("File database settings are not configured"); }
builder.Services.AddSingleton(
FileDatabase.Services.FileDatabase.FromAsync(
fileDatabaseSettings.VaultPath));
}
}
}