FileDatabase refactor for normalization and consistency

This commit is contained in:
daniel-c-harvey
2025-09-04 16:26:10 -04:00
parent e82366e47f
commit 6fefcbcfb5
24 changed files with 3069 additions and 186 deletions
@@ -0,0 +1,27 @@
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Services;
using Microsoft.AspNetCore.Mvc;
namespace DeepDrftContent.Controllers;
[ApiController]
[Route("api/[controller]")]
public class TrackController : ControllerBase
{
private readonly EntryKey _vaultKey = new("tracks", MediaVaultType.Audio);
private readonly FileDatabase.Services.FileDatabase _fileDatabase;
public TrackController(FileDatabase.Services.FileDatabase fileDatabase)
{
_fileDatabase = fileDatabase;
}
[HttpGet("{trackId}")]
public async Task<ActionResult<AudioBinaryDto>> GetTrack([FromQuery] string trackId)
{
if (_fileDatabase.GetVault(_vaultKey) is not AudioVault vault) { return NotFound(); }
var file = await vault.GetEntryAsync<AudioBinary>(MediaVaultType.Audio, new EntryKey(trackId, MediaVaultType.Audio));
if (file == null) { return NotFound(); }
return File(file.Buffer, MimeTypeExtensions.GetMimeType(file.Extension));
}
}
@@ -0,0 +1,41 @@
using DeepDrftContent.FileDatabase.Models;
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
namespace DeepDrftContent.FileDatabase.Abstractions;
/// <summary>
/// Interface for creating index instances
/// </summary>
public interface IIndexFactory
{
/// <summary>
/// Creates an index of the specified type
/// </summary>
Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath);
/// <summary>
/// Loads an existing index of the specified type
/// </summary>
Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath);
/// <summary>
/// Loads existing index or creates new one if loading fails
/// </summary>
Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath);
}
/// <summary>
/// Interface for creating index data objects
/// </summary>
public interface IIndexDataFactory
{
/// <summary>
/// Creates index data for serialization
/// </summary>
object CreateIndexData(IndexType type, IIndex index);
/// <summary>
/// Creates index instance from data
/// </summary>
IIndex CreateIndexFromData(IndexType type, object indexData);
}
@@ -0,0 +1,69 @@
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Services;
namespace DeepDrftContent.FileDatabase.Abstractions;
/// <summary>
/// Interface for registering media type factories
/// </summary>
public interface IMediaTypeRegistry
{
/// <summary>
/// Register a factory for a specific media vault type
/// </summary>
void RegisterMediaType<TBinary, TParams, TDto, TMetaData, TVault>(MediaVaultType vaultType)
where TBinary : FileBinary
where TParams : FileBinaryParams
where TDto : FileBinaryDto
where TMetaData : MetaData;
/// <summary>
/// Create a binary object from parameters
/// </summary>
FileBinary CreateBinary(MediaVaultType vaultType, object parameters);
/// <summary>
/// Create a binary object from DTO
/// </summary>
FileBinary CreateBinaryFromDto(MediaVaultType vaultType, object dto);
/// <summary>
/// Create a DTO from binary object
/// </summary>
FileBinaryDto CreateDto(MediaVaultType vaultType, FileBinary binary);
/// <summary>
/// Create metadata from media object
/// </summary>
MetaData CreateMetaDataFromMedia(MediaVaultType vaultType, string entryKey, string extension, object media);
/// <summary>
/// Create parameters from binary and metadata
/// </summary>
object CreateParams(MediaVaultType vaultType, FileBinary fileBinary, MetaData metaData);
/// <summary>
/// Create media vault
/// </summary>
Task<MediaVault?> CreateVaultAsync(MediaVaultType vaultType, string rootPath);
/// <summary>
/// Get the binary type for a vault type
/// </summary>
Type GetBinaryType(MediaVaultType vaultType);
/// <summary>
/// Get the DTO type for a vault type
/// </summary>
Type GetDtoType(MediaVaultType vaultType);
/// <summary>
/// Get the parameters type for a vault type
/// </summary>
Type GetParamsType(MediaVaultType vaultType);
/// <summary>
/// Get the metadata type for a vault type
/// </summary>
Type GetMetaDataType(MediaVaultType vaultType);
}
+34 -1
View File
@@ -1,7 +1,7 @@
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Base interface for all index types
/// Base interface for all index types - minimal contract
/// </summary>
public interface IIndex
{
@@ -9,7 +9,13 @@ public interface IIndex
/// Gets the key identifier for this index
/// </summary>
string GetKey();
}
/// <summary>
/// Interface for indexes that support entry queries
/// </summary>
public interface IEntryQueryable : IIndex
{
/// <summary>
/// Gets all entry keys in this index
/// </summary>
@@ -25,3 +31,30 @@ public interface IIndex
/// </summary>
bool HasEntry(EntryKey entryKey);
}
/// <summary>
/// Interface for indexes that support directory operations
/// </summary>
public interface IDirectoryIndex : IEntryQueryable
{
/// <summary>
/// Adds an entry to the directory index
/// </summary>
void PutEntry(EntryKey entryKey);
}
/// <summary>
/// Interface for indexes that support vault operations with metadata
/// </summary>
public interface IVaultIndex : IEntryQueryable
{
/// <summary>
/// Gets metadata for a specific entry
/// </summary>
MetaData? GetEntry(EntryKey entryKey);
/// <summary>
/// Adds an entry with metadata to the vault index
/// </summary>
void PutEntry(EntryKey entryKey, MetaData metaData);
}
@@ -65,7 +65,7 @@ public class VaultIndexData : IndexData
/// <summary>
/// Directory index implementation using StructuralSet for entries
/// </summary>
public class DirectoryIndex : IndexData, IIndex
public class DirectoryIndex : IndexData, IDirectoryIndex
{
public StructuralSet<EntryKey> Entries { get; }
@@ -93,7 +93,7 @@ public class DirectoryIndex : IndexData, IIndex
/// <summary>
/// Vault index implementation using StructuralMap for entries with metadata
/// </summary>
public class VaultIndex : IndexData, IIndex
public class VaultIndex : IndexData, IVaultIndex
{
public StructuralMap<EntryKey, MetaData> Entries { get; }
@@ -1,37 +1,22 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Services;
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Type mappings for media vault types to their corresponding classes
/// Type mappings for media vault types - simple dictionary-based approach
/// </summary>
public static class MediaVaultTypeMap
{
public static Type GetBinaryType(MediaVaultType vaultType) => vaultType switch
{
MediaVaultType.Media => typeof(MediaBinary),
MediaVaultType.Image => typeof(ImageBinary),
_ => throw new ArgumentException($"Unknown vault type: {vaultType}")
};
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static Type GetDtoType(MediaVaultType vaultType) => vaultType switch
{
MediaVaultType.Media => typeof(MediaBinaryDto),
MediaVaultType.Image => typeof(ImageBinaryDto),
_ => throw new ArgumentException($"Unknown vault type: {vaultType}")
};
public static Type GetBinaryType(MediaVaultType vaultType) => _registry.GetBinaryType(vaultType);
public static Type GetParamsType(MediaVaultType vaultType) => vaultType switch
{
MediaVaultType.Media => typeof(MediaBinaryParams),
MediaVaultType.Image => typeof(ImageBinaryParams),
_ => throw new ArgumentException($"Unknown vault type: {vaultType}")
};
public static Type GetDtoType(MediaVaultType vaultType) => _registry.GetDtoType(vaultType);
public static Type GetMetaDataType(MediaVaultType vaultType) => vaultType switch
{
MediaVaultType.Media => typeof(MetaData),
MediaVaultType.Image => typeof(ImageMetaData),
_ => throw new ArgumentException($"Unknown vault type: {vaultType}")
};
public static Type GetParamsType(MediaVaultType vaultType) => _registry.GetParamsType(vaultType);
public static Type GetMetaDataType(MediaVaultType vaultType) => _registry.GetMetaDataType(vaultType);
}
/// <summary>
@@ -39,38 +24,52 @@ public static class MediaVaultTypeMap
/// </summary>
public static class MetaDataFactory
{
public static MetaData Create(MediaVaultType type, string entryKey, string extension, double aspectRatio = 1.0)
public static MetaData Create(MediaVaultType type, string entryKey, string extension)
{
return type switch
{
MediaVaultType.Media => new MetaData(entryKey, extension),
MediaVaultType.Image => new ImageMetaData(entryKey, extension, aspectRatio),
MediaVaultType.Image => throw new ArgumentException("Image metadata requires aspect ratio. Use CreateImageMetaData instead."),
MediaVaultType.Audio => throw new ArgumentException("Audio metadata requires duration and bitrate. Use CreateAudioMetaData instead."),
_ => throw new ArgumentException($"Unknown vault type: {type}")
};
}
public static T Create<T>(MediaVaultType type, string entryKey, string extension, double aspectRatio = 1.0)
public static ImageMetaData CreateImageMetaData(string entryKey, string extension, double aspectRatio)
{
return new ImageMetaData(entryKey, extension, aspectRatio);
}
public static AudioMetaData CreateAudioMetaData(string entryKey, string extension, double duration, int bitrate)
{
return new AudioMetaData(entryKey, extension, duration, bitrate);
}
private static readonly IMediaTypeRegistry _metaDataRegistry = new SimpleMediaTypeRegistry();
public static MetaData CreateFromMedia(MediaVaultType type, string entryKey, string extension, object media)
{
return _metaDataRegistry.CreateMetaDataFromMedia(type, entryKey, extension, media);
}
public static T Create<T>(MediaVaultType type, string entryKey, string extension)
where T : MetaData
{
var metaData = Create(type, entryKey, extension, aspectRatio);
var metaData = Create(type, entryKey, extension);
return (T)metaData;
}
}
/// <summary>
/// Factory for creating media parameter objects
/// Factory for creating media parameter objects - simple dictionary-based approach
/// </summary>
public static class MediaParamsFactory
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static object Create(MediaVaultType type, FileBinary fileBinary, MetaData metaData)
{
return type switch
{
MediaVaultType.Media => new MediaBinaryParams(fileBinary.Buffer, fileBinary.Size, metaData.Extension),
MediaVaultType.Image when metaData is ImageMetaData imageMetaData =>
new ImageBinaryParams(fileBinary.Buffer, fileBinary.Size, metaData.Extension, imageMetaData.AspectRatio),
_ => throw new ArgumentException($"Invalid vault type {type} or metadata type mismatch")
};
return _registry.CreateParams(type, fileBinary, metaData);
}
public static T Create<T>(MediaVaultType type, FileBinary fileBinary, MetaData metaData)
@@ -81,20 +80,15 @@ public static class MediaParamsFactory
}
/// <summary>
/// Factory for creating media binary objects from parameters
/// Factory for creating media binary objects - simple dictionary-based approach
/// </summary>
public static class FileBinaryFactory
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static object Create(MediaVaultType vaultType, object parameters)
{
return vaultType switch
{
MediaVaultType.Media when parameters is MediaBinaryParams mediaParams =>
new MediaBinary(mediaParams),
MediaVaultType.Image when parameters is ImageBinaryParams imageParams =>
new ImageBinary(imageParams),
_ => throw new ArgumentException($"Invalid vault type {vaultType} or parameter type mismatch")
};
return _registry.CreateBinary(vaultType, parameters);
}
public static T Create<T>(MediaVaultType vaultType, object parameters) where T : FileBinary
@@ -105,14 +99,7 @@ public static class FileBinaryFactory
public static object From(MediaVaultType type, object mediaBinaryDto)
{
return type switch
{
MediaVaultType.Media when mediaBinaryDto is MediaBinaryDto mediaDto =>
MediaBinary.From(mediaDto),
MediaVaultType.Image when mediaBinaryDto is ImageBinaryDto imageDto =>
ImageBinary.From(imageDto),
_ => throw new ArgumentException($"Invalid type {type} or DTO type mismatch")
};
return _registry.CreateBinaryFromDto(type, mediaBinaryDto);
}
public static T From<T>(MediaVaultType type, object mediaBinaryDto) where T : FileBinary
@@ -123,20 +110,18 @@ public static class FileBinaryFactory
}
/// <summary>
/// Factory for creating DTO objects from media binaries
/// Factory for creating DTO objects from media binaries - simple dictionary-based approach
/// </summary>
public static class FileBinaryDtoFactory
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static object From(MediaVaultType type, object mediaBinary)
{
return type switch
{
MediaVaultType.Media when mediaBinary is MediaBinary media =>
new MediaBinaryDto(media),
MediaVaultType.Image when mediaBinary is ImageBinary image =>
new ImageBinaryDto(image),
_ => throw new ArgumentException($"Invalid type {type} or binary type mismatch")
};
if (mediaBinary is not FileBinary fileBinary)
throw new ArgumentException($"Expected FileBinary but got {mediaBinary.GetType()}");
return _registry.CreateDto(type, fileBinary);
}
public static T From<T>(MediaVaultType type, object mediaBinary)
@@ -140,6 +140,63 @@ public record ImageBinaryDto(string Base64, int Size, string Mime, double Aspect
imageBinary.AspectRatio) { }
}
/// <summary>
/// Parameters for creating an AudioBinary
/// </summary>
/// <param name="Buffer">The binary data</param>
/// <param name="Size">The size of the data in bytes</param>
/// <param name="Extension">The file extension</param>
/// <param name="Duration">The duration of the audio in seconds</param>
/// <param name="Bitrate">The bitrate of the audio in kbps</param>
public record AudioBinaryParams(byte[] Buffer, int Size, string Extension, double Duration, int Bitrate)
: MediaBinaryParams(Buffer, Size, Extension);
/// <summary>
/// Audio binary with duration and bitrate information
/// </summary>
public class AudioBinary : MediaBinary
{
public double Duration { get; }
public int Bitrate { get; }
public AudioBinary(AudioBinaryParams parameters) : base(parameters)
{
Duration = parameters.Duration;
Bitrate = parameters.Bitrate;
}
public static AudioBinary From(AudioBinaryDto dto)
{
var buffer = Convert.FromBase64String(dto.Base64);
var extension = GetExtensionType(dto.Mime);
return new AudioBinary(new AudioBinaryParams(buffer, dto.Size, extension, dto.Duration, dto.Bitrate));
}
private static string GetExtensionType(string mime)
{
return MimeTypeExtensions.GetExtension(mime);
}
}
/// <summary>
/// DTO for AudioBinary serialization
/// </summary>
/// <param name="Base64">Base64 encoded binary data</param>
/// <param name="Size">Size of the original data</param>
/// <param name="Mime">MIME type of the media</param>
/// <param name="Duration">The duration of the audio in seconds</param>
/// <param name="Bitrate">The bitrate of the audio in kbps</param>
public record AudioBinaryDto(string Base64, int Size, string Mime, double Duration, int Bitrate)
: MediaBinaryDto(Base64, Size, Mime)
{
public AudioBinaryDto(AudioBinary audioBinary) : this(
Convert.ToBase64String(audioBinary.Buffer),
audioBinary.Size,
MimeTypeExtensions.GetMimeType(audioBinary.Extension),
audioBinary.Duration,
audioBinary.Bitrate) { }
}
/// <summary>
/// Utility class for MIME type and extension conversions
/// </summary>
@@ -153,7 +210,13 @@ public static class MimeTypeExtensions
{ ".gif", "image/gif" },
{ ".webp", "image/webp" },
{ ".svg", "image/svg+xml" },
{ ".bmp", "image/bmp" }
{ ".bmp", "image/bmp" },
{ ".mp3", "audio/mpeg" },
{ ".wav", "audio/wav" },
{ ".flac", "audio/flac" },
{ ".aac", "audio/aac" },
{ ".ogg", "audio/ogg" },
{ ".m4a", "audio/mp4" }
};
private static readonly Dictionary<string, string> Extensions = new()
@@ -163,7 +226,13 @@ public static class MimeTypeExtensions
{ "image/gif", ".gif" },
{ "image/webp", ".webp" },
{ "image/svg+xml", ".svg" },
{ "image/bmp", ".bmp" }
{ "image/bmp", ".bmp" },
{ "audio/mpeg", ".mp3" },
{ "audio/wav", ".wav" },
{ "audio/flac", ".flac" },
{ "audio/aac", ".aac" },
{ "audio/ogg", ".ogg" },
{ "audio/mp4", ".m4a" }
};
public static string GetMimeType(string extension)
@@ -6,5 +6,6 @@ namespace DeepDrftContent.FileDatabase.Models;
public enum MediaVaultType
{
Media,
Image
Image,
Audio
}
@@ -10,6 +10,7 @@ namespace DeepDrftContent.FileDatabase.Models;
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(MetaData), typeDiscriminator: "media")]
[JsonDerivedType(typeof(ImageMetaData), typeDiscriminator: "image")]
[JsonDerivedType(typeof(AudioMetaData), typeDiscriminator: "audio")]
public record MetaData(string MediaKey, string Extension);
/// <summary>
@@ -20,3 +21,13 @@ public record MetaData(string MediaKey, string Extension);
/// <param name="AspectRatio">The aspect ratio of the image</param>
public record ImageMetaData(string MediaKey, string Extension, double AspectRatio)
: MetaData(MediaKey, Extension);
/// <summary>
/// Extended metadata for audio entries, including duration and bitrate
/// </summary>
/// <param name="MediaKey">The key used to identify the media file</param>
/// <param name="Extension">The file extension of the media</param>
/// <param name="Duration">The duration of the audio in seconds</param>
/// <param name="Bitrate">The bitrate of the audio in kbps</param>
public record AudioMetaData(string MediaKey, string Extension, double Duration, int Bitrate)
: MetaData(MediaKey, Extension);
@@ -50,7 +50,7 @@ public class FileDatabase : DirectoryIndexDirectory
private async Task InitVaultAsync(EntryKey vaultKey)
{
var path = Path.Combine(RootPath, vaultKey.Key);
var directoryVault = await ImageDirectoryVault.FromAsync(path);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
if (directoryVault != null)
{
@@ -82,7 +82,7 @@ public class FileDatabase : DirectoryIndexDirectory
try
{
var path = Path.Combine(RootPath, vaultKey.Key);
var directoryVault = await ImageDirectoryVault.FromAsync(path);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
if (directoryVault != null)
{
@@ -92,7 +92,6 @@ public class FileDatabase : DirectoryIndexDirectory
}
catch
{
// Re-throw to maintain the same error behavior as TypeScript version
throw;
}
}
@@ -0,0 +1,111 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Factory service for creating and managing indexes
/// </summary>
public class IndexFactoryService : IIndexFactory, IIndexDataFactory
{
private readonly Dictionary<IndexType, Func<string, IIndex>> _indexCreators;
private readonly Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators;
private readonly Dictionary<IndexType, Func<IIndex, object>> _indexDataCreators;
public IndexFactoryService()
{
_indexCreators = new Dictionary<IndexType, Func<string, IIndex>>
{
{ IndexType.Directory, rootPath => new DirectoryIndex(new DirectoryIndexData(Path.GetFileName(rootPath))) },
{ IndexType.Vault, rootPath => new VaultIndex(new VaultIndexData(Path.GetFileName(rootPath))) }
};
_indexFromDataCreators = new Dictionary<IndexType, Func<object, IIndex>>
{
{ IndexType.Directory, data => new DirectoryIndex((DirectoryIndexData)data) },
{ IndexType.Vault, data => new VaultIndex((VaultIndexData)data) }
};
_indexDataCreators = new Dictionary<IndexType, Func<IIndex, object>>
{
{ IndexType.Directory, index => DirectoryIndexData.FromIndex((DirectoryIndex)index) },
{ IndexType.Vault, index => VaultIndexData.FromIndex((VaultIndex)index) }
};
}
public async Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath)
{
if (!_indexCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
var index = creator(rootPath);
// Ensure directory exists and save the index
await FileUtils.MakeVaultDirectoryAsync(rootPath);
await SaveIndexAsync(rootPath, type, index);
return index;
}
public async Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath)
{
if (!_indexFromDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
var indexPath = Path.Combine(rootPath, "index");
object indexData = type switch
{
IndexType.Directory => await FileUtils.FetchObjectAsync<DirectoryIndexData>(indexPath),
IndexType.Vault => await FileUtils.FetchObjectAsync<VaultIndexData>(indexPath),
_ => throw new ArgumentException($"Unknown index type: {type}")
};
return creator(indexData);
}
public async Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath)
{
try
{
return await LoadIndexAsync(type, rootPath);
}
catch
{
return await CreateIndexAsync(type, rootPath);
}
}
public object CreateIndexData(IndexType type, IIndex index)
{
if (!_indexDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
return creator(index);
}
public IIndex CreateIndexFromData(IndexType type, object indexData)
{
if (!_indexFromDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
return creator(indexData);
}
private async Task SaveIndexAsync(string rootPath, IndexType type, IIndex index)
{
var indexPath = Path.Combine(rootPath, "index");
var indexData = CreateIndexData(type, index);
await FileUtils.PutObjectAsync(indexPath, indexData);
}
}
@@ -1,3 +1,4 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
@@ -19,11 +20,13 @@ public abstract class AbstractIndexContainer
{
protected IndexType Type { get; }
public string RootPath { get; }
private readonly IIndexDataFactory _indexDataFactory;
protected AbstractIndexContainer(string path, IndexType type)
protected AbstractIndexContainer(string path, IndexType type, IIndexDataFactory? indexDataFactory = null)
{
RootPath = path;
Type = type;
_indexDataFactory = indexDataFactory ?? new IndexFactoryService();
}
public string GetKey() => Path.GetFileName(RootPath);
@@ -31,78 +34,30 @@ public abstract class AbstractIndexContainer
protected async Task SaveIndexAsync<T>(T index) where T : IIndex
{
var indexPath = Path.Combine(RootPath, "index");
object indexData = Type switch
{
IndexType.Directory when index is DirectoryIndex dirIndex => DirectoryIndexData.FromIndex(dirIndex),
IndexType.Vault when index is VaultIndex vaultIndex => VaultIndexData.FromIndex(vaultIndex),
_ => throw new ArgumentException($"Invalid index type {Type} for index {typeof(T)}")
};
var indexData = _indexDataFactory.CreateIndexData(Type, index);
await FileUtils.PutObjectAsync(indexPath, indexData);
}
}
/// <summary>
/// Factory for creating and loading indexes
/// Factory for creating and loading indexes - delegates to IIndexFactory
/// </summary>
public class IndexFactory : AbstractIndexContainer
{
public IndexFactory(string path, IndexType type) : base(path, type) { }
private readonly IIndexFactory _factoryService;
public IndexFactory(string path, IndexType type, IIndexFactory? factoryService = null, IIndexDataFactory? indexDataFactory = null)
: base(path, type, indexDataFactory)
{
_factoryService = factoryService ?? new IndexFactoryService();
}
/// <summary>
/// Builds an index by loading existing or creating new
/// </summary>
public async Task<IIndex?> BuildIndexAsync()
{
try
{
return await LoadOrCreateIndexAsync();
}
catch
{
return null;
}
}
private async Task<IIndex?> LoadOrCreateIndexAsync()
{
try
{
return await LoadIndexAsync();
}
catch
{
return await CreateIndexAsync();
}
}
private async Task<IIndex?> LoadIndexAsync()
{
var indexPath = Path.Combine(RootPath, "index");
IIndex result = Type switch
{
IndexType.Directory => new DirectoryIndex(await FileUtils.FetchObjectAsync<DirectoryIndexData>(indexPath)),
IndexType.Vault => new VaultIndex(await FileUtils.FetchObjectAsync<VaultIndexData>(indexPath)),
_ => throw new ArgumentException($"Unknown index type: {Type}")
};
return result;
}
private async Task<IIndex?> CreateIndexAsync()
{
IIndex index = Type switch
{
IndexType.Directory => new DirectoryIndex(new DirectoryIndexData(RootPath)),
IndexType.Vault => new VaultIndex(new VaultIndexData(RootPath)),
_ => throw new ArgumentException($"Unknown index type: {Type}")
};
await FileUtils.MakeVaultDirectoryAsync(RootPath);
await SaveIndexAsync(index);
return index;
return await _factoryService.LoadOrCreateIndexAsync(Type, RootPath);
}
}
@@ -111,9 +66,10 @@ public class IndexFactory : AbstractIndexContainer
/// </summary>
public abstract class IndexDirectory : AbstractIndexContainer
{
protected IIndex Index { get; }
protected IEntryQueryable Index { get; }
protected IndexDirectory(string rootPath, IndexType type, IIndex index) : base(rootPath, type)
protected IndexDirectory(string rootPath, IndexType type, IEntryQueryable index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, type, indexDataFactory)
{
Index = index;
}
@@ -130,16 +86,18 @@ public abstract class IndexDirectory : AbstractIndexContainer
/// </summary>
public class DirectoryIndexDirectory : IndexDirectory
{
public DirectoryIndexDirectory(string rootPath, DirectoryIndex index)
: base(rootPath, IndexType.Directory, index) { }
private readonly IDirectoryIndex _directoryIndex;
public DirectoryIndexDirectory(string rootPath, IDirectoryIndex index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, IndexType.Directory, index, indexDataFactory)
{
_directoryIndex = index;
}
protected async Task AddToIndexAsync(EntryKey entryKey)
{
if (Index is DirectoryIndex dirIndex)
{
dirIndex.PutEntry(entryKey);
await SaveIndexAsync(dirIndex);
}
_directoryIndex.PutEntry(entryKey);
await SaveIndexAsync(_directoryIndex);
}
}
@@ -148,15 +106,17 @@ public class DirectoryIndexDirectory : IndexDirectory
/// </summary>
public class VaultIndexDirectory : IndexDirectory
{
public VaultIndexDirectory(string rootPath, VaultIndex index)
: base(rootPath, IndexType.Vault, index) { }
private readonly IVaultIndex _vaultIndex;
public VaultIndexDirectory(string rootPath, IVaultIndex index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, IndexType.Vault, index, indexDataFactory)
{
_vaultIndex = index;
}
protected async Task AddToIndexAsync(EntryKey entryKey, MetaData metaData)
{
if (Index is VaultIndex vaultIndex)
{
vaultIndex.PutEntry(entryKey, metaData);
await SaveIndexAsync(vaultIndex);
}
_vaultIndex.PutEntry(entryKey, metaData);
await SaveIndexAsync(_vaultIndex);
}
}
@@ -45,7 +45,7 @@ public abstract class MediaVault : VaultIndexDirectory
var (buffer, extension) = ExtractMediaProperties(media);
var mediaPath = GetMediaPathFromEntryKey(entryKey.Key, extension);
var metaData = MetaDataFactory.Create(vaultType, entryKey.Key, extension, GetAspectRatio(media));
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryKey.Key, extension, media);
await AddToIndexAsync(entryKey, metaData);
await FileUtils.PutFileAsync(mediaPath, buffer);
@@ -86,42 +86,51 @@ public abstract class MediaVault : VaultIndexDirectory
return media switch
{
ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension),
AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension),
MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension),
_ => throw new ArgumentException($"Unsupported media type: {media.GetType()}")
};
}
/// <summary>
/// Extracts aspect ratio from media object if it's an image
/// </summary>
private static double GetAspectRatio(object media)
{
return media is ImageBinary imageBinary ? imageBinary.AspectRatio : 1.0;
}
}
/// <summary>
/// Concrete implementation of MediaVault for image storage
/// </summary>
public class ImageDirectoryVault : MediaVault
public class ImageVault : MediaVault
{
private ImageDirectoryVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
private ImageVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
/// <summary>
/// Factory method to create an ImageDirectoryVault instance
/// Factory method to create an ImageVault instance
/// </summary>
public static async Task<ImageDirectoryVault?> FromAsync(string rootPath)
public static async Task<ImageVault?> FromAsync(string rootPath)
{
var factory = new IndexFactory(rootPath, IndexType.Vault);
var index = await factory.BuildIndexAsync();
if (index is VaultIndex vaultIndex)
{
return new ImageDirectoryVault(rootPath, vaultIndex);
return new ImageVault(rootPath, vaultIndex);
}
return null;
}
}
public class AudioVault : MediaVault
{
private AudioVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
public static async Task<AudioVault?> FromAsync(string rootPath)
{
var factory = new IndexFactory(rootPath, IndexType.Vault);
var index = await factory.BuildIndexAsync();
if (index is VaultIndex vaultIndex)
{
return new AudioVault(rootPath, vaultIndex);
}
return null;
}
}
@@ -0,0 +1,17 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Factory for creating media vaults - simple dictionary-based approach
/// </summary>
public static class MediaVaultFactory
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static async Task<MediaVault?> From(string rootPath, MediaVaultType mediaType)
{
return await _registry.CreateVaultAsync(mediaType, rootPath);
}
}
@@ -0,0 +1,149 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Simple dictionary-based registry for media type factories
/// </summary>
public class SimpleMediaTypeRegistry : IMediaTypeRegistry
{
private readonly Dictionary<MediaVaultType, Func<object, FileBinary>> _binaryFactories = new();
private readonly Dictionary<MediaVaultType, Func<object, FileBinary>> _binaryFromDtoFactories = new();
private readonly Dictionary<MediaVaultType, Func<FileBinary, FileBinaryDto>> _dtoFactories = new();
private readonly Dictionary<MediaVaultType, Func<string, string, object, MetaData>> _metaDataFromMediaFactories = new();
private readonly Dictionary<MediaVaultType, Func<FileBinary, MetaData, object>> _paramsFactories = new();
private readonly Dictionary<MediaVaultType, Func<string, Task<MediaVault?>>> _vaultFactories = new();
private readonly Dictionary<MediaVaultType, Type> _binaryTypes = new();
private readonly Dictionary<MediaVaultType, Type> _dtoTypes = new();
private readonly Dictionary<MediaVaultType, Type> _paramsTypes = new();
private readonly Dictionary<MediaVaultType, Type> _metaDataTypes = new();
public SimpleMediaTypeRegistry()
{
// Clean one-line registrations with generics - no reflection!
RegisterType<MediaBinary, MediaBinaryParams, MediaBinaryDto, MetaData>(
MediaVaultType.Media,
p => new MediaBinary(p),
dto => MediaBinary.From(dto),
binary => new MediaBinaryDto(binary),
(key, ext, _) => new MetaData(key, ext),
(binary, meta) => new MediaBinaryParams(binary.Buffer, binary.Size, meta.Extension));
RegisterType<ImageBinary, ImageBinaryParams, ImageBinaryDto, ImageMetaData>(
MediaVaultType.Image,
p => new ImageBinary(p),
dto => ImageBinary.From(dto),
binary => new ImageBinaryDto(binary),
(key, ext, media) => media is ImageBinary img ? new ImageMetaData(key, ext, img.AspectRatio) : new MetaData(key, ext),
(binary, meta) => meta is ImageMetaData imgMeta
? new ImageBinaryParams(binary.Buffer, binary.Size, meta.Extension, imgMeta.AspectRatio)
: throw new ArgumentException("ImageBinary requires ImageMetaData"),
async path => await ImageVault.FromAsync(path));
RegisterType<AudioBinary, AudioBinaryParams, AudioBinaryDto, AudioMetaData>(
MediaVaultType.Audio,
p => new AudioBinary(p),
dto => AudioBinary.From(dto),
binary => new AudioBinaryDto(binary),
(key, ext, media) => media is AudioBinary audio ? new AudioMetaData(key, ext, audio.Duration, audio.Bitrate) : new MetaData(key, ext),
(binary, meta) => meta is AudioMetaData audioMeta
? new AudioBinaryParams(binary.Buffer, binary.Size, meta.Extension, audioMeta.Duration, audioMeta.Bitrate)
: throw new ArgumentException("AudioBinary requires AudioMetaData"),
async path => await AudioVault.FromAsync(path));
}
private void RegisterType<TBinary, TParams, TDto, TMetaData>(
MediaVaultType vaultType,
Func<TParams, TBinary> binaryFactory,
Func<TDto, TBinary> binaryFromDtoFactory,
Func<TBinary, TDto> dtoFactory,
Func<string, string, object, MetaData> metaDataFactory,
Func<FileBinary, MetaData, object> paramsFactory,
Func<string, Task<MediaVault?>>? vaultFactory = null)
where TBinary : FileBinary
where TParams : FileBinaryParams
where TDto : FileBinaryDto
where TMetaData : MetaData
{
_binaryFactories[vaultType] = p => binaryFactory((TParams)p);
_binaryFromDtoFactories[vaultType] = dto => binaryFromDtoFactory((TDto)dto);
_dtoFactories[vaultType] = binary => dtoFactory((TBinary)binary);
_metaDataFromMediaFactories[vaultType] = metaDataFactory;
_paramsFactories[vaultType] = paramsFactory;
_binaryTypes[vaultType] = typeof(TBinary);
_dtoTypes[vaultType] = typeof(TDto);
_paramsTypes[vaultType] = typeof(TParams);
_metaDataTypes[vaultType] = typeof(TMetaData);
if (vaultFactory != null)
_vaultFactories[vaultType] = vaultFactory;
}
// Public interface implementation - allows external registration
public void RegisterMediaType<TBinary, TParams, TDto, TMetaData, TVault>(MediaVaultType vaultType)
where TBinary : FileBinary
where TParams : FileBinaryParams
where TDto : FileBinaryDto
where TMetaData : MetaData
{
// For now, we can't auto-generate the factories without reflection
// This would need to be implemented if external registration is needed
throw new NotImplementedException("Use RegisterType method for internal registration. External registration not yet implemented.");
}
public FileBinary CreateBinary(MediaVaultType vaultType, object parameters)
{
return _binaryFactories.TryGetValue(vaultType, out var factory)
? factory(parameters)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public FileBinary CreateBinaryFromDto(MediaVaultType vaultType, object dto)
{
return _binaryFromDtoFactories.TryGetValue(vaultType, out var factory)
? factory(dto)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public FileBinaryDto CreateDto(MediaVaultType vaultType, FileBinary binary)
{
return _dtoFactories.TryGetValue(vaultType, out var factory)
? factory(binary)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public MetaData CreateMetaDataFromMedia(MediaVaultType vaultType, string entryKey, string extension, object media)
{
return _metaDataFromMediaFactories.TryGetValue(vaultType, out var factory)
? factory(entryKey, extension, media)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public object CreateParams(MediaVaultType vaultType, FileBinary fileBinary, MetaData metaData)
{
return _paramsFactories.TryGetValue(vaultType, out var factory)
? factory(fileBinary, metaData)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public async Task<MediaVault?> CreateVaultAsync(MediaVaultType vaultType, string rootPath)
{
return _vaultFactories.TryGetValue(vaultType, out var factory)
? await factory(rootPath)
: null;
}
public Type GetBinaryType(MediaVaultType vaultType) =>
_binaryTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetDtoType(MediaVaultType vaultType) =>
_dtoTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetParamsType(MediaVaultType vaultType) =>
_paramsTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetMetaDataType(MediaVaultType vaultType) =>
_metaDataTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
}
@@ -6,25 +6,43 @@ namespace DeepDrftContent.FileDatabase.Utils;
/// <summary>
/// A map implementation that uses structural equality for keys by serializing them to JSON.
/// This provides the same behavior as the TypeScript StructuralMap.
/// Optimized with caching to avoid repeated serialization.
/// </summary>
/// <typeparam name="TKey">The key type</typeparam>
/// <typeparam name="TValue">The value type</typeparam>
public class StructuralMap<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
public class StructuralMap<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> where TKey : notnull
{
private readonly Dictionary<string, KeyValuePair<TKey, TValue>> _innerMap = new();
private readonly Dictionary<TKey, string> _keyStringCache = new();
/// <summary>
/// Converts a key to its string representation for structural comparison
/// Uses caching to avoid expensive serialization on repeated lookups
/// </summary>
private string GetKeyString(TKey key)
{
return key switch
if (key == null) return "null";
// For reference types, use cache to avoid repeated serialization
if (!typeof(TKey).IsValueType && _keyStringCache.TryGetValue(key, out var cached))
{
return cached;
}
var keyString = key switch
{
null => "null",
string s => s,
int or long or float or double or decimal => key.ToString()!,
_ => JsonSerializer.Serialize(key)
};
// Cache for reference types only (value types are cheap to convert)
if (!typeof(TKey).IsValueType)
{
_keyStringCache[key] = keyString;
}
return keyString;
}
/// <summary>
@@ -6,24 +6,42 @@ namespace DeepDrftContent.FileDatabase.Utils;
/// <summary>
/// A set implementation that uses structural equality for values by serializing them to JSON.
/// This provides the same behavior as the TypeScript StructuralSet.
/// Optimized with caching to avoid repeated serialization.
/// </summary>
/// <typeparam name="T">The value type</typeparam>
public class StructuralSet<T> : IEnumerable<T>
public class StructuralSet<T> : IEnumerable<T> where T : notnull
{
private readonly Dictionary<string, T> _innerMap = new();
private readonly Dictionary<T, string> _valueStringCache = new();
/// <summary>
/// Converts a value to its string representation for structural comparison
/// Uses caching to avoid expensive serialization on repeated lookups
/// </summary>
private string GetValueString(T value)
{
return value switch
if (value == null) return "null";
// For reference types, use cache to avoid repeated serialization
if (!typeof(T).IsValueType && _valueStringCache.TryGetValue(value, out var cached))
{
return cached;
}
var valueString = value switch
{
null => "null",
string s => s,
int or long or float or double or decimal => value.ToString()!,
_ => JsonSerializer.Serialize(value)
};
// Cache for reference types only (value types are cheap to convert)
if (!typeof(T).IsValueType)
{
_valueStringCache[value] = valueString;
}
return valueString;
}
/// <summary>
+17 -4
View File
@@ -1,18 +1,31 @@
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Services;
using DeepDrftContent.Models;
namespace DeepDrftContent
{
public static class Startup
{
public static void ConfigureDomainServices(WebApplicationBuilder builder)
public static async Task 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));
var fileDatabase = await FileDatabase.Services.FileDatabase.FromAsync(fileDatabaseSettings.VaultPath);
if (fileDatabase is null) { throw new Exception("Unable to initialize file database"); }
builder.Services.AddSingleton(fileDatabase);
await InitializeTrackVault(fileDatabase);
}
private static async Task InitializeTrackVault(FileDatabase.Services.FileDatabase fileDatabase)
{
var vaultKey = new EntryKey("tracks", MediaVaultType.Audio);
if (!fileDatabase.HasVault(vaultKey))
{
await fileDatabase.CreateVaultAsync(vaultKey);
}
}
}
}