feat(data): rename *.Services projects, lift TrackEntity onto BlazorBlocks data layer, regenerate initial Postgres migration
DeepDrftWeb.Services → DeepDrftData; DeepDrftContent.Services → DeepDrftContent.Data. TrackEntity:BaseEntity, TrackRepository:Repository<>, TrackManager:Manager<>+ITrackService. Drops DeepDrftModels PagingParameters/PagedResult in favour of Models.Common.* from BlazorBlocks. InitialCreate migration captures full schema including is_deleted index.
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
using DeepDrftContent.Data.FileDatabase.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Main file database class that orchestrates multiple media vaults.
|
||||
/// Includes file watching for automatic index reloading when modified by external processes.
|
||||
/// </summary>
|
||||
public class FileDatabase : DirectoryIndexDirectory, IDisposable
|
||||
{
|
||||
private readonly StructuralMap<string, MediaVault> _vaults;
|
||||
private readonly IndexWatcher _indexWatcher;
|
||||
private readonly IndexFactoryService _indexFactory;
|
||||
private readonly ILogger<FileDatabase> _logger;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Factory method to create a FileDatabase instance
|
||||
/// </summary>
|
||||
public static async Task<FileDatabase?> FromAsync(string rootPath, ILogger<FileDatabase>? logger = null)
|
||||
{
|
||||
var factoryService = new IndexFactoryService();
|
||||
var rootIndex = await factoryService.LoadOrCreateDirectoryIndexAsync(rootPath);
|
||||
|
||||
if (rootIndex != null)
|
||||
{
|
||||
var db = new FileDatabase(rootPath, rootIndex, factoryService, logger);
|
||||
await db.InitVaultsAsync();
|
||||
return db;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private FileDatabase(string rootPath, IDirectoryIndex index, IndexFactoryService indexFactory, ILogger<FileDatabase>? logger = null) : base(rootPath, index)
|
||||
{
|
||||
_vaults = new StructuralMap<string, MediaVault>();
|
||||
_indexWatcher = new IndexWatcher();
|
||||
_indexFactory = indexFactory;
|
||||
_logger = logger ?? Microsoft.Extensions.Logging.Abstractions.NullLogger<FileDatabase>.Instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes all vaults found in the index
|
||||
/// </summary>
|
||||
private async Task InitVaultsAsync()
|
||||
{
|
||||
foreach (var vaultId in GetIndexEntries())
|
||||
{
|
||||
var vaultType = await GetVaultTypeFromIndex(vaultId);
|
||||
if (vaultType.HasValue)
|
||||
{
|
||||
await InitVaultAsync(vaultId, vaultType.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a specific vault and sets up file watching for its index
|
||||
/// </summary>
|
||||
private async Task InitVaultAsync(string vaultId, MediaVaultType vaultType)
|
||||
{
|
||||
var path = Path.Combine(RootPath, vaultId);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultType, _indexFactory);
|
||||
|
||||
if (directoryVault != null)
|
||||
{
|
||||
_vaults.Set(vaultId, directoryVault);
|
||||
|
||||
// Watch the vault's index file for external modifications
|
||||
_indexWatcher.Watch(path, () =>
|
||||
{
|
||||
// Reload the index asynchronously when file changes
|
||||
_ = directoryVault.ReloadIndexAsync();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets vault type from the vault's index file
|
||||
/// </summary>
|
||||
private async Task<MediaVaultType?> GetVaultTypeFromIndex(string vaultId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var vaultPath = Path.Combine(RootPath, vaultId);
|
||||
var index = await _indexFactory.LoadIndexAsync(IndexType.Vault, vaultPath);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
{
|
||||
return vaultIndex.VaultType;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If we can't load the index, we can't determine the vault type
|
||||
// This might happen for legacy vaults or corrupted indexes
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a vault exists for the given vault ID
|
||||
/// </summary>
|
||||
public bool HasVault(string vaultId)
|
||||
{
|
||||
return _vaults.Has(vaultId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a vault by vault ID
|
||||
/// </summary>
|
||||
public MediaVault? GetVault(string vaultId)
|
||||
{
|
||||
return HasVault(vaultId) ? _vaults.Get(vaultId) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new vault. Propagates exceptions to the caller — vault creation failure is not
|
||||
/// silently swallowable because a partially-created vault would leave the index inconsistent.
|
||||
/// </summary>
|
||||
public async Task CreateVaultAsync(string vaultId, MediaVaultType vaultType)
|
||||
{
|
||||
var path = Path.Combine(RootPath, vaultId);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultType, _indexFactory);
|
||||
|
||||
if (directoryVault != null)
|
||||
{
|
||||
_vaults.Set(vaultId, directoryVault);
|
||||
await AddToIndexAsync(vaultId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a resource from a specific vault (MediaVaultType inferred from T)
|
||||
/// </summary>
|
||||
public async Task<T?> LoadResourceAsync<T>(string vaultId, string entryId)
|
||||
where T : FileBinary
|
||||
{
|
||||
try
|
||||
{
|
||||
var vault = _vaults.Get(vaultId);
|
||||
if (vault != null)
|
||||
{
|
||||
return await vault.GetEntryAsync<T>(entryId);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Swallow exceptions and return null, matching TypeScript behavior
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a resource in a specific vault (MediaVaultType inferred from media type)
|
||||
/// </summary>
|
||||
public async Task<bool> RegisterResourceAsync(string vaultId, string entryId, FileBinary media)
|
||||
{
|
||||
try
|
||||
{
|
||||
var directoryVault = _vaults.Get(vaultId);
|
||||
if (directoryVault != null)
|
||||
{
|
||||
await directoryVault.AddEntryAsync(entryId, media);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Swallow exceptions and return false, matching TypeScript behavior
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a resource from a specific vault. Returns null if the vault does not exist,
|
||||
/// false if the entry was not found, true if the entry was removed. Distinguishing
|
||||
/// "no such vault" / "no such entry" / "removed" lets the HTTP host map cleanly to
|
||||
/// 404 vs. 200. Follows the FileDatabase error-swallow contract: any unexpected failure
|
||||
/// returns null so callers can surface 5xx without try/catch at the controller layer.
|
||||
/// </summary>
|
||||
public async Task<bool?> RemoveResourceAsync(string vaultId, string entryId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var directoryVault = _vaults.Get(vaultId);
|
||||
if (directoryVault == null)
|
||||
return null;
|
||||
|
||||
return await directoryVault.RemoveEntryAsync(entryId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "RemoveResourceAsync failed for vault {VaultName} key {Key}", vaultId, entryId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all vault IDs managed by this database
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> GetVaultIds()
|
||||
{
|
||||
return _vaults.Keys.ToList().AsReadOnly();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of vaults
|
||||
/// </summary>
|
||||
public int GetVaultCount()
|
||||
{
|
||||
return _vaults.Size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the file database and stops all file watchers
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
_indexWatcher.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Abstractions;
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
using DeepDrftContent.Data.FileDatabase.Utils;
|
||||
using IndexType = DeepDrftContent.Data.FileDatabase.Services.IndexType;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Factory service for creating and managing indexes
|
||||
/// </summary>
|
||||
public class IndexFactoryService : IIndexFactory, IIndexDataFactory
|
||||
{
|
||||
private readonly Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators;
|
||||
private readonly Dictionary<IndexType, Func<IIndex, object>> _indexDataCreators;
|
||||
|
||||
public IndexFactoryService()
|
||||
{
|
||||
_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<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath)
|
||||
{
|
||||
var indexData = new DirectoryIndexData(Path.GetFileName(rootPath));
|
||||
var index = new DirectoryIndex(indexData);
|
||||
|
||||
// Ensure directory exists and save the index
|
||||
await FileUtils.MakeVaultDirectoryAsync(rootPath);
|
||||
await SaveIndexAsync(rootPath, IndexType.Directory, 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<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var index = await LoadIndexAsync(IndexType.Directory, rootPath);
|
||||
return index as IDirectoryIndex;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return await CreateDirectoryIndexAsync(rootPath);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
|
||||
{
|
||||
var vaultIndexData = new VaultIndexData(Path.GetFileName(rootPath), vaultType);
|
||||
var index = new VaultIndex(vaultIndexData);
|
||||
|
||||
// Ensure directory exists and save the index
|
||||
await FileUtils.MakeVaultDirectoryAsync(rootPath);
|
||||
await SaveIndexAsync(rootPath, IndexType.Vault, index);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public async Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
|
||||
{
|
||||
try
|
||||
{
|
||||
var index = await LoadIndexAsync(IndexType.Vault, rootPath);
|
||||
return index as IVaultIndex;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return await CreateVaultIndexAsync(rootPath, vaultType);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Abstractions;
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
using DeepDrftContent.Data.FileDatabase.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Enum representing different types of indexes
|
||||
/// </summary>
|
||||
public enum IndexType
|
||||
{
|
||||
Directory,
|
||||
Vault
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for index containers
|
||||
/// </summary>
|
||||
public abstract class AbstractIndexContainer
|
||||
{
|
||||
protected IndexType Type { get; }
|
||||
public string RootPath { get; }
|
||||
private readonly IIndexDataFactory _indexDataFactory;
|
||||
|
||||
protected AbstractIndexContainer(string path, IndexType type, IIndexDataFactory? indexDataFactory = null)
|
||||
{
|
||||
RootPath = path;
|
||||
Type = type;
|
||||
_indexDataFactory = indexDataFactory ?? new IndexFactoryService();
|
||||
}
|
||||
|
||||
public string GetKey() => Path.GetFileName(RootPath);
|
||||
|
||||
protected async Task SaveIndexAsync<T>(T index) where T : IIndex
|
||||
{
|
||||
var indexPath = Path.Combine(RootPath, "index");
|
||||
var indexData = _indexDataFactory.CreateIndexData(Type, index);
|
||||
await FileUtils.PutObjectAsync(indexPath, indexData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for directory containers that manage indexes
|
||||
/// </summary>
|
||||
public abstract class IndexDirectory : AbstractIndexContainer
|
||||
{
|
||||
protected IEntryQueryable Index { get; set; }
|
||||
|
||||
protected IndexDirectory(string rootPath, IndexType type, IEntryQueryable index, IIndexDataFactory? indexDataFactory = null)
|
||||
: base(rootPath, type, indexDataFactory)
|
||||
{
|
||||
Index = index;
|
||||
}
|
||||
|
||||
protected IReadOnlyList<string> GetIndexEntries() => Index.GetEntries();
|
||||
|
||||
public int GetIndexSize() => Index.GetEntriesSize();
|
||||
|
||||
public virtual Task<bool> HasIndexEntry(string entryId) => Task.FromResult(Index.HasEntry(entryId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Directory index directory implementation
|
||||
/// </summary>
|
||||
public class DirectoryIndexDirectory : IndexDirectory
|
||||
{
|
||||
private readonly IDirectoryIndex _directoryIndex;
|
||||
private readonly SemaphoreSlim _indexLock = new(1, 1);
|
||||
|
||||
public DirectoryIndexDirectory(string rootPath, IDirectoryIndex index, IIndexDataFactory? indexDataFactory = null)
|
||||
: base(rootPath, IndexType.Directory, index, indexDataFactory)
|
||||
{
|
||||
_directoryIndex = index;
|
||||
}
|
||||
|
||||
protected async Task AddToIndexAsync(string entryId)
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
_directoryIndex.PutEntry(entryId);
|
||||
await SaveIndexAsync(_directoryIndex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Vault index directory implementation with support for index reloading
|
||||
/// </summary>
|
||||
public class VaultIndexDirectory : IndexDirectory
|
||||
{
|
||||
private IVaultIndex _vaultIndex;
|
||||
private readonly SemaphoreSlim _indexLock = new(1, 1);
|
||||
private readonly IndexFactoryService _factoryService;
|
||||
private readonly ILogger<VaultIndexDirectory>? _logger;
|
||||
|
||||
public VaultIndexDirectory(string rootPath, IVaultIndex index, IIndexDataFactory? indexDataFactory = null, ILogger<VaultIndexDirectory>? logger = null, IndexFactoryService? factoryService = null)
|
||||
: base(rootPath, IndexType.Vault, index, indexDataFactory ?? factoryService)
|
||||
{
|
||||
_vaultIndex = index;
|
||||
_logger = logger;
|
||||
_factoryService = factoryService ?? new IndexFactoryService();
|
||||
}
|
||||
|
||||
protected async Task AddToIndexAsync(string entryId, MetaData metaData)
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
_vaultIndex.PutEntry(entryId, metaData);
|
||||
await SaveIndexAsync(_vaultIndex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an entry from the index under the index lock, persisting on success.
|
||||
/// Returns the removed entry's metadata, or null if the entry did not exist.
|
||||
/// Caller is responsible for any backing-file cleanup using the returned metadata.
|
||||
/// </summary>
|
||||
protected async Task<MetaData?> RemoveFromIndexAsync(string entryId)
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var metaData = _vaultIndex.GetEntry(entryId);
|
||||
if (metaData == null)
|
||||
return null;
|
||||
|
||||
if (!_vaultIndex.RemoveEntry(entryId))
|
||||
return null;
|
||||
|
||||
await SaveIndexAsync(_vaultIndex);
|
||||
return metaData;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reloads the index from disk. Called when the index file is modified externally.
|
||||
/// </summary>
|
||||
public async Task ReloadIndexAsync()
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var newIndex = await _factoryService.LoadIndexAsync(IndexType.Vault, RootPath);
|
||||
if (newIndex is IVaultIndex vaultIndex)
|
||||
{
|
||||
_vaultIndex = vaultIndex;
|
||||
Index = vaultIndex;
|
||||
if (_logger != null)
|
||||
_logger.LogDebug("VaultIndexDirectory: Reloaded index for {RootPath}, {EntryCount} entries", RootPath, vaultIndex.GetEntriesSize());
|
||||
else
|
||||
Console.WriteLine($"VaultIndexDirectory: Reloaded index for {RootPath}, {vaultIndex.GetEntriesSize()} entries");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogWarning(ex, "VaultIndexDirectory: Failed to reload index for {RootPath}", RootPath);
|
||||
else
|
||||
Console.WriteLine($"VaultIndexDirectory: Failed to reload index for {RootPath}: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe check for index entry
|
||||
/// </summary>
|
||||
public override async Task<bool> HasIndexEntry(string entryId)
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
return _vaultIndex.HasEntry(entryId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe get entry metadata
|
||||
/// </summary>
|
||||
public async Task<MetaData?> GetEntryMetadata(string entryId)
|
||||
{
|
||||
await _indexLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
return _vaultIndex.GetEntry(entryId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_indexLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Watches index files for external modifications and triggers reloads.
|
||||
/// Uses FileSystemWatcher to detect changes made by other processes (e.g., CLI).
|
||||
/// </summary>
|
||||
public class IndexWatcher : IDisposable
|
||||
{
|
||||
private readonly Dictionary<string, FileSystemWatcher> _watchers = new();
|
||||
private readonly Dictionary<string, Action> _reloadCallbacks = new();
|
||||
private readonly object _lock = new();
|
||||
private readonly ILogger<IndexWatcher>? _logger;
|
||||
private bool _disposed;
|
||||
|
||||
public IndexWatcher(ILogger<IndexWatcher>? logger = null)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an index file to be watched for changes.
|
||||
/// </summary>
|
||||
/// <param name="indexPath">Full path to the directory containing the index file</param>
|
||||
/// <param name="onChanged">Callback to invoke when the index file changes</param>
|
||||
public void Watch(string indexPath, Action onChanged)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
// Already watching this path
|
||||
if (_watchers.ContainsKey(indexPath))
|
||||
{
|
||||
_reloadCallbacks[indexPath] = onChanged;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var watcher = new FileSystemWatcher(indexPath)
|
||||
{
|
||||
Filter = "index",
|
||||
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime,
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
|
||||
watcher.Changed += OnIndexChanged;
|
||||
watcher.Created += OnIndexChanged;
|
||||
|
||||
_watchers[indexPath] = watcher;
|
||||
_reloadCallbacks[indexPath] = onChanged;
|
||||
|
||||
if (_logger != null)
|
||||
_logger.LogDebug("IndexWatcher: Watching {IndexPath}/index", indexPath);
|
||||
else
|
||||
Console.WriteLine($"IndexWatcher: Watching {indexPath}/index");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogWarning(ex, "IndexWatcher: Failed to watch {IndexPath}", indexPath);
|
||||
else
|
||||
Console.WriteLine($"IndexWatcher: Failed to watch {indexPath}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops watching an index file.
|
||||
/// </summary>
|
||||
public void Unwatch(string indexPath)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_watchers.TryGetValue(indexPath, out var watcher))
|
||||
{
|
||||
watcher.EnableRaisingEvents = false;
|
||||
watcher.Dispose();
|
||||
_watchers.Remove(indexPath);
|
||||
_reloadCallbacks.Remove(indexPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnIndexChanged(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
var watcher = sender as FileSystemWatcher;
|
||||
if (watcher == null) return;
|
||||
|
||||
var indexPath = watcher.Path;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_reloadCallbacks.TryGetValue(indexPath, out var callback))
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogDebug("IndexWatcher: Index changed at {IndexPath}, triggering reload", indexPath);
|
||||
else
|
||||
Console.WriteLine($"IndexWatcher: Index changed at {indexPath}, triggering reload");
|
||||
|
||||
// Invoke callback on a background thread to avoid blocking the watcher
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
callback();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogWarning(ex, "IndexWatcher: Reload callback failed for {IndexPath}", indexPath);
|
||||
else
|
||||
Console.WriteLine($"IndexWatcher: Reload callback failed: {ex.Message}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
foreach (var watcher in _watchers.Values)
|
||||
{
|
||||
watcher.EnableRaisingEvents = false;
|
||||
watcher.Dispose();
|
||||
}
|
||||
_watchers.Clear();
|
||||
_reloadCallbacks.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
using DeepDrftContent.Data.FileDatabase.Utils;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for media vaults that store and manage media files
|
||||
/// </summary>
|
||||
public abstract class MediaVault : VaultIndexDirectory
|
||||
{
|
||||
protected MediaVault(string rootPath, VaultIndex index, IndexFactoryService? factoryService = null)
|
||||
: base(rootPath, index, factoryService: factoryService) { }
|
||||
|
||||
/// <summary>
|
||||
/// Generates a media key from an entry key by sanitizing special characters
|
||||
/// </summary>
|
||||
protected string GetMediaKey(string entryKey, string extension)
|
||||
{
|
||||
var sanitized = Regex.Replace(entryKey, @"[^a-zA-Z0-9]", "-");
|
||||
return $"{sanitized}{extension}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full file path for a media file from an entry key
|
||||
/// </summary>
|
||||
protected string GetMediaPathFromEntryKey(string entryKey, string extension)
|
||||
{
|
||||
return Path.Combine(RootPath, GetMediaKey(entryKey, extension));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full file path for a media file from a media key
|
||||
/// </summary>
|
||||
protected string GetMediaPathFromMediaKey(string mediaKey)
|
||||
{
|
||||
return Path.Combine(RootPath, mediaKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new entry to the vault with the specified media data (MediaVaultType inferred from media type)
|
||||
/// </summary>
|
||||
public async Task AddEntryAsync(string entryId, FileBinary media)
|
||||
{
|
||||
// Extract properties from media object based on type
|
||||
var (buffer, extension) = ExtractMediaProperties(media);
|
||||
|
||||
// Infer MediaVaultType from the media object type
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType(media.GetType());
|
||||
|
||||
var mediaPath = GetMediaPathFromEntryKey(entryId, extension);
|
||||
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryId, extension, media);
|
||||
|
||||
// Use string-based index operations
|
||||
await AddToIndexAsync(entryId, metaData);
|
||||
await FileUtils.PutFileAsync(mediaPath, buffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an entry from the vault (MediaVaultType inferred from T)
|
||||
/// </summary>
|
||||
public async Task<T?> GetEntryAsync<T>(string entryId) where T : FileBinary
|
||||
{
|
||||
// Infer MediaVaultType from the generic type T
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType<T>();
|
||||
|
||||
// Use thread-safe method from VaultIndexDirectory
|
||||
if (!await HasIndexEntry(entryId))
|
||||
return null;
|
||||
|
||||
// Use thread-safe metadata retrieval
|
||||
var metaData = await GetEntryMetadata(entryId);
|
||||
if (metaData == null)
|
||||
return null;
|
||||
|
||||
var mediaPath = GetMediaPathFromEntryKey(metaData.MediaKey, metaData.Extension);
|
||||
|
||||
if (!FileUtils.FileExists(mediaPath))
|
||||
return null;
|
||||
|
||||
var fileBinary = await FileUtils.FetchFileAsync(mediaPath);
|
||||
var parameters = MediaParamsFactory.Create(vaultType, fileBinary, metaData);
|
||||
|
||||
var result = FileBinaryFactory.Create(vaultType, parameters);
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a read-only stream over an entry's backing file plus its metadata
|
||||
/// (extension/MIME), without buffering the file into memory.
|
||||
/// Returns null if the entry is unknown or the backing file is missing.
|
||||
///
|
||||
/// Use this when the caller will forward bytes to a network response — the
|
||||
/// existing <see cref="GetEntryAsync{T}"/> allocates a full <c>byte[]</c>
|
||||
/// and pushes large WAVs onto the LOH for every request.
|
||||
///
|
||||
/// The caller owns the returned stream and must dispose it. Error-handling
|
||||
/// follows the same swallow-and-return-null contract as the rest of the
|
||||
/// FileDatabase API; the caller checks for null.
|
||||
/// </summary>
|
||||
public async Task<MediaStream?> GetEntryStreamAsync(string entryId)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!await HasIndexEntry(entryId))
|
||||
return null;
|
||||
|
||||
var metaData = await GetEntryMetadata(entryId);
|
||||
if (metaData == null)
|
||||
return null;
|
||||
|
||||
var mediaPath = GetMediaPathFromEntryKey(metaData.MediaKey, metaData.Extension);
|
||||
if (!FileUtils.FileExists(mediaPath))
|
||||
return null;
|
||||
|
||||
// Async-capable, sequential-scan FileStream — the response writer will pull
|
||||
// bytes in order. bufferSize matches FileUtils.FetchFileAsync (64 KB).
|
||||
var stream = new FileStream(
|
||||
mediaPath,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.Read,
|
||||
bufferSize: 64 * 1024,
|
||||
useAsync: true);
|
||||
|
||||
return new MediaStream(stream, metaData.Extension);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Match FileDatabase error-swallow contract.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an entry from the vault: drops it from the index (persisting the change)
|
||||
/// and deletes the backing file from disk. Returns true if an entry was removed,
|
||||
/// false if the entry was not present. Follows the FileDatabase error-swallow contract
|
||||
/// for read failures; index/file write failures propagate so the caller can map them
|
||||
/// to a 5xx.
|
||||
/// </summary>
|
||||
public async Task<bool> RemoveEntryAsync(string entryId)
|
||||
{
|
||||
var metaData = await RemoveFromIndexAsync(entryId);
|
||||
if (metaData == null)
|
||||
return false;
|
||||
|
||||
// Index already persisted; if the file is missing or fails to delete, the entry
|
||||
// is still gone from the catalogue. Treat a missing file as success (callers asked
|
||||
// for the entry to go away, and it has). A failure deleting an existing file leaves
|
||||
// an orphan on disk; surface it to the caller via exception so the host can log,
|
||||
// matching the AddEntryAsync error-propagation shape.
|
||||
var mediaPath = GetMediaPathFromEntryKey(metaData.MediaKey, metaData.Extension);
|
||||
if (FileUtils.FileExists(mediaPath))
|
||||
{
|
||||
File.Delete(mediaPath);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts buffer and extension from a media binary
|
||||
/// </summary>
|
||||
private static (byte[] buffer, string extension) ExtractMediaProperties(FileBinary media)
|
||||
{
|
||||
return media switch
|
||||
{
|
||||
ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension),
|
||||
AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension),
|
||||
MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension),
|
||||
FileBinary fileBinary => throw new ArgumentException($"FileBinary must be a specific media type (ImageBinary, AudioBinary, or MediaBinary), not base FileBinary"),
|
||||
_ => throw new ArgumentException($"Unsupported media type: {media.GetType()}")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete implementation of MediaVault for image storage
|
||||
/// </summary>
|
||||
public class ImageVault : MediaVault
|
||||
{
|
||||
private ImageVault(string rootPath, VaultIndex index, IndexFactoryService? factoryService = null)
|
||||
: base(rootPath, index, factoryService) { }
|
||||
|
||||
/// <summary>
|
||||
/// Factory method to create an ImageVault instance
|
||||
/// </summary>
|
||||
public static async Task<ImageVault?> FromAsync(string rootPath, IndexFactoryService? factoryService = null)
|
||||
{
|
||||
var factory = factoryService ?? new IndexFactoryService();
|
||||
var index = await factory.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Image);
|
||||
|
||||
if (index != null)
|
||||
{
|
||||
return new ImageVault(rootPath, (VaultIndex)index, factory);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class AudioVault : MediaVault
|
||||
{
|
||||
private AudioVault(string rootPath, VaultIndex index, IndexFactoryService? factoryService = null)
|
||||
: base(rootPath, index, factoryService) { }
|
||||
|
||||
public static async Task<AudioVault?> FromAsync(string rootPath, IndexFactoryService? factoryService = null)
|
||||
{
|
||||
var factory = factoryService ?? new IndexFactoryService();
|
||||
var index = await factory.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Audio);
|
||||
|
||||
if (index != null)
|
||||
{
|
||||
return new AudioVault(rootPath, (VaultIndex)index, factory);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An open read-only stream over a vault entry plus the extension needed to
|
||||
/// resolve its MIME type. Caller owns the stream and must dispose it.
|
||||
/// </summary>
|
||||
public sealed class MediaStream : IDisposable, IAsyncDisposable
|
||||
{
|
||||
public Stream Stream { get; }
|
||||
public string Extension { get; }
|
||||
|
||||
public MediaStream(Stream stream, string extension)
|
||||
{
|
||||
Stream = stream;
|
||||
Extension = extension;
|
||||
}
|
||||
|
||||
public void Dispose() => Stream.Dispose();
|
||||
public ValueTask DisposeAsync() => Stream.DisposeAsync();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
|
||||
namespace DeepDrftContent.Data.FileDatabase.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Factory for creating media vaults
|
||||
/// </summary>
|
||||
public static class MediaVaultFactory
|
||||
{
|
||||
public static async Task<MediaVault?> From(string rootPath, MediaVaultType mediaType, IndexFactoryService? factoryService = null)
|
||||
{
|
||||
return mediaType switch
|
||||
{
|
||||
MediaVaultType.Image => await ImageVault.FromAsync(rootPath, factoryService),
|
||||
MediaVaultType.Audio => await AudioVault.FromAsync(rootPath, factoryService),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using DeepDrftContent.Data.FileDatabase.Abstractions;
|
||||
using DeepDrftContent.Data.FileDatabase.Models;
|
||||
|
||||
namespace DeepDrftContent.Data.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();
|
||||
|
||||
// Reverse mapping: Type -> MediaVaultType
|
||||
private readonly Dictionary<Type, MediaVaultType> _typeToVaultType = 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);
|
||||
|
||||
// Populate reverse mapping
|
||||
_typeToVaultType[typeof(TBinary)] = vaultType;
|
||||
|
||||
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}");
|
||||
|
||||
public MediaVaultType GetVaultType(Type binaryType)
|
||||
{
|
||||
if (_typeToVaultType.TryGetValue(binaryType, out var vaultType))
|
||||
return vaultType;
|
||||
|
||||
// Check inheritance hierarchy for derived types
|
||||
foreach (var kvp in _typeToVaultType)
|
||||
{
|
||||
if (kvp.Key.IsAssignableFrom(binaryType))
|
||||
return kvp.Value;
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Cannot infer MediaVaultType for {binaryType.Name}. Type not registered.");
|
||||
}
|
||||
|
||||
public MediaVaultType GetVaultType<T>() where T : FileBinary
|
||||
=> GetVaultType(typeof(T));
|
||||
}
|
||||
Reference in New Issue
Block a user