FileDatabase Refactor and normalization
Test cleanup
This commit is contained in:
@@ -37,32 +37,39 @@ public abstract class MediaVault : VaultIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new entry to the vault with the specified media data
|
||||
/// Adds a new entry to the vault with the specified media data (MediaVaultType inferred from media type)
|
||||
/// </summary>
|
||||
public async Task AddEntryAsync(MediaVaultType vaultType, EntryKey entryKey, object media)
|
||||
public async Task AddEntryAsync(string entryId, FileBinary media)
|
||||
{
|
||||
// Extract properties from media object based on type
|
||||
var (buffer, extension) = ExtractMediaProperties(media);
|
||||
|
||||
var mediaPath = GetMediaPathFromEntryKey(entryKey.Key, extension);
|
||||
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryKey.Key, extension, media);
|
||||
// Infer MediaVaultType from the media object type
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType(media.GetType());
|
||||
|
||||
await AddToIndexAsync(entryKey, metaData);
|
||||
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
|
||||
/// Retrieves an entry from the vault (MediaVaultType inferred from T)
|
||||
/// </summary>
|
||||
public async Task<T?> GetEntryAsync<T>(MediaVaultType vaultType, EntryKey entryKey) where T : FileBinary
|
||||
public async Task<T?> GetEntryAsync<T>(string entryId) where T : FileBinary
|
||||
{
|
||||
if (!HasIndexEntry(entryKey))
|
||||
// Infer MediaVaultType from the generic type T
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType<T>();
|
||||
|
||||
if (!HasIndexEntry(entryId))
|
||||
return null;
|
||||
|
||||
if (Index is not VaultIndex vaultIndex)
|
||||
return null;
|
||||
|
||||
var metaData = vaultIndex.GetEntry(entryKey);
|
||||
var metaData = vaultIndex.GetEntry(entryId);
|
||||
if (metaData == null)
|
||||
return null;
|
||||
|
||||
@@ -79,15 +86,16 @@ public abstract class MediaVault : VaultIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts buffer and extension from a media object
|
||||
/// Extracts buffer and extension from a media binary
|
||||
/// </summary>
|
||||
private static (byte[] buffer, string extension) ExtractMediaProperties(object media)
|
||||
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()}")
|
||||
};
|
||||
}
|
||||
@@ -105,12 +113,12 @@ public class ImageVault : MediaVault
|
||||
/// </summary>
|
||||
public static async Task<ImageVault?> FromAsync(string rootPath)
|
||||
{
|
||||
var factory = new IndexFactory(rootPath, IndexType.Vault);
|
||||
var index = await factory.BuildIndexAsync();
|
||||
var factoryService = new IndexFactoryService();
|
||||
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Image);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
if (index != null)
|
||||
{
|
||||
return new ImageVault(rootPath, vaultIndex);
|
||||
return new ImageVault(rootPath, (VaultIndex)index);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -123,12 +131,12 @@ public class AudioVault : MediaVault
|
||||
|
||||
public static async Task<AudioVault?> FromAsync(string rootPath)
|
||||
{
|
||||
var factory = new IndexFactory(rootPath, IndexType.Vault);
|
||||
var index = await factory.BuildIndexAsync();
|
||||
var factoryService = new IndexFactoryService();
|
||||
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Audio);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
if (index != null)
|
||||
{
|
||||
return new AudioVault(rootPath, vaultIndex);
|
||||
return new AudioVault(rootPath, (VaultIndex)index);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user