Backend Services Split into separate projects for reference from other front ends

This commit is contained in:
daniel-c-harvey
2025-09-07 12:56:52 -04:00
parent a5b7ab041e
commit b16bcfc6cd
47 changed files with 153 additions and 114 deletions
@@ -1,12 +0,0 @@
namespace DeepDrftContent.Constants;
/// <summary>
/// Constants for FileDatabase vault names
/// </summary>
public static class VaultConstants
{
/// <summary>
/// Vault name for storing audio tracks
/// </summary>
public const string Tracks = "tracks";
}
@@ -1,5 +1,5 @@
using DeepDrftContent.Constants;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.Services.Constants;
using DeepDrftContent.Services.FileDatabase.Models;
using DeepDrftContent.Middleware;
using Microsoft.AspNetCore.Mvc;
@@ -9,9 +9,9 @@ namespace DeepDrftContent.Controllers;
[Route("api/[controller]")]
public class TrackController : ControllerBase
{
private readonly FileDatabase.Services.FileDatabase _fileDatabase;
private readonly DeepDrftContent.Services.FileDatabase.Services.FileDatabase _fileDatabase;
public TrackController(FileDatabase.Services.FileDatabase fileDatabase)
public TrackController(DeepDrftContent.Services.FileDatabase.Services.FileDatabase fileDatabase)
{
_fileDatabase = fileDatabase;
}
+1
View File
@@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\DeepDrftModels\DeepDrftModels.csproj" />
<ProjectReference Include="..\DeepDrftContent.Services\DeepDrftContent.Services.csproj" />
</ItemGroup>
<ItemGroup>
@@ -1,51 +0,0 @@
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>
/// Loads an existing index of the specified type
/// </summary>
Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath);
/// <summary>
/// Creates a directory index
/// </summary>
Task<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath);
/// <summary>
/// Loads existing directory index or creates new one if loading fails
/// </summary>
Task<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath);
/// <summary>
/// Creates a vault index with the specified vault type
/// </summary>
Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
/// <summary>
/// Loads existing vault index or creates new one with the specified vault type if loading fails
/// </summary>
Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
}
/// <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);
}
@@ -1,79 +0,0 @@
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);
/// <summary>
/// Get the vault type for a binary type (reverse mapping)
/// </summary>
MediaVaultType GetVaultType(Type binaryType);
/// <summary>
/// Get the vault type for a binary type using generics (reverse mapping)
/// </summary>
MediaVaultType GetVaultType<T>() where T : FileBinary;
}
@@ -1,60 +0,0 @@
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Base interface for all index types - minimal contract
/// </summary>
public interface IIndex
{
/// <summary>
/// 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 IDs in this index
/// </summary>
IReadOnlyList<string> GetEntries();
/// <summary>
/// Gets the number of entries in this index
/// </summary>
int GetEntriesSize();
/// <summary>
/// Checks if the index contains the specified entry ID
/// </summary>
bool HasEntry(string entryId);
}
/// <summary>
/// Interface for indexes that support directory operations
/// </summary>
public interface IDirectoryIndex : IEntryQueryable
{
/// <summary>
/// Adds an entry to the directory index
/// </summary>
void PutEntry(string entryId);
}
/// <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(string entryId);
/// <summary>
/// Adds an entry with metadata to the vault index
/// </summary>
void PutEntry(string entryId, MetaData metaData);
}
@@ -1,134 +0,0 @@
using DeepDrftContent.FileDatabase.Utils;
using System.Text.Json.Serialization;
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Base class for index data used in serialization
/// </summary>
public abstract class IndexData
{
public string IndexKey { get; }
protected IndexData(string indexKey)
{
IndexKey = indexKey;
}
}
/// <summary>
/// Serializable data for directory indexes
/// </summary>
public class DirectoryIndexData : IndexData
{
public List<string> Entries { get; set; } = new();
public DirectoryIndexData(string indexKey) : base(indexKey) { }
public static DirectoryIndexData FromIndex(DirectoryIndex index)
{
var data = new DirectoryIndexData(index.GetKey())
{
Entries = index.GetEntries().ToList()
};
return data;
}
}
/// <summary>
/// Entry data for vault index serialization
/// </summary>
public class VaultEntryData
{
public string Key { get; set; } = null!;
public MetaData Value { get; set; } = null!;
}
/// <summary>
/// Serializable data for vault indexes
/// </summary>
public class VaultIndexData : IndexData
{
public List<VaultEntryData> Entries { get; set; } = new();
public MediaVaultType VaultType { get; set; }
public VaultIndexData(string indexKey) : base(indexKey)
{
VaultType = MediaVaultType.Media; // Default vault type for legacy compatibility
}
[JsonConstructor]
public VaultIndexData(string indexKey, MediaVaultType vaultType) : base(indexKey)
{
VaultType = vaultType;
}
public static VaultIndexData FromIndex(VaultIndex index)
{
var data = new VaultIndexData(index.GetKey(), index.VaultType)
{
Entries = index.Entries.Select(kvp => new VaultEntryData { Key = kvp.Key, Value = kvp.Value }).ToList()
};
return data;
}
}
/// <summary>
/// Directory index implementation using StructuralSet for entries
/// </summary>
public class DirectoryIndex : IndexData, IDirectoryIndex
{
public StructuralSet<string> Entries { get; }
public DirectoryIndex(DirectoryIndexData indexData) : base(indexData.IndexKey)
{
Entries = new StructuralSet<string>();
// Load entries from data
foreach (var entry in indexData.Entries)
{
Entries.Add(entry);
}
}
public string GetKey() => IndexKey;
public IReadOnlyList<string> GetEntries() => Entries.ToList().AsReadOnly();
public int GetEntriesSize() => Entries.Size;
public bool HasEntry(string entryId) => Entries.Has(entryId);
public void PutEntry(string entryId) => Entries.Add(entryId);
}
/// <summary>
/// Vault index implementation using StructuralMap for entries with metadata
/// </summary>
public class VaultIndex : IndexData, IVaultIndex
{
public StructuralMap<string, MetaData> Entries { get; }
public MediaVaultType VaultType { get; }
public VaultIndex(VaultIndexData indexData) : base(indexData.IndexKey)
{
Entries = new StructuralMap<string, MetaData>();
VaultType = indexData.VaultType;
// Load entries from data
foreach (var entry in indexData.Entries)
{
Entries.Set(entry.Key, entry.Value);
}
}
public string GetKey() => IndexKey;
public IReadOnlyList<string> GetEntries() => Entries.Keys.ToList().AsReadOnly();
public int GetEntriesSize() => Entries.Size;
public bool HasEntry(string entryId) => Entries.Has(entryId);
public MetaData? GetEntry(string entryId) => Entries.Get(entryId);
public void PutEntry(string entryId, MetaData metaData) => Entries.Set(entryId, metaData);
}
@@ -1,142 +0,0 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Services;
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Type mappings for media vault types - simple dictionary-based approach
/// </summary>
public static class MediaVaultTypeMap
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static Type GetBinaryType(MediaVaultType vaultType) => _registry.GetBinaryType(vaultType);
public static Type GetDtoType(MediaVaultType vaultType) => _registry.GetDtoType(vaultType);
public static Type GetParamsType(MediaVaultType vaultType) => _registry.GetParamsType(vaultType);
public static Type GetMetaDataType(MediaVaultType vaultType) => _registry.GetMetaDataType(vaultType);
/// <summary>
/// Get the vault type for a binary type (reverse mapping)
/// </summary>
public static MediaVaultType GetVaultType(Type binaryType) => _registry.GetVaultType(binaryType);
/// <summary>
/// Get the vault type for a binary type using generics (reverse mapping)
/// </summary>
public static MediaVaultType GetVaultType<T>() where T : FileBinary => _registry.GetVaultType<T>();
}
/// <summary>
/// Factory for creating metadata objects based on vault type
/// </summary>
public static class MetaDataFactory
{
public static MetaData Create(MediaVaultType type, string entryKey, string extension)
{
return type switch
{
MediaVaultType.Media => new MetaData(entryKey, extension),
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 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);
return (T)metaData;
}
}
/// <summary>
/// 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 _registry.CreateParams(type, fileBinary, metaData);
}
public static T Create<T>(MediaVaultType type, FileBinary fileBinary, MetaData metaData)
{
var parameters = Create(type, fileBinary, metaData);
return (T)parameters;
}
}
/// <summary>
/// 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 _registry.CreateBinary(vaultType, parameters);
}
public static T Create<T>(MediaVaultType vaultType, object parameters) where T : FileBinary
{
var binary = Create(vaultType, parameters);
return (T)binary;
}
public static object From(MediaVaultType type, object mediaBinaryDto)
{
return _registry.CreateBinaryFromDto(type, mediaBinaryDto);
}
public static T From<T>(MediaVaultType type, object mediaBinaryDto) where T : FileBinary
{
var binary = From(type, mediaBinaryDto);
return (T)binary;
}
}
/// <summary>
/// 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)
{
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)
{
var dto = From(type, mediaBinary);
return (T)dto;
}
}
@@ -1,251 +0,0 @@
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Parameters for creating a FileBinary
/// </summary>
/// <param name="Buffer">The binary data</param>
/// <param name="Size">The size of the data in bytes</param>
public record FileBinaryParams(byte[] Buffer, int Size);
/// <summary>
/// Base class for file binary data
/// </summary>
public class FileBinary
{
public byte[] Buffer { get; }
public int Size { get; }
public FileBinary(FileBinaryParams parameters)
{
Buffer = parameters.Buffer;
Size = parameters.Size;
}
public static FileBinary From(FileBinaryDto dto)
{
var buffer = Convert.FromBase64String(dto.Base64);
return new FileBinary(new FileBinaryParams(buffer, dto.Size));
}
}
/// <summary>
/// DTO for FileBinary serialization
/// </summary>
/// <param name="Base64">Base64 encoded binary data</param>
/// <param name="Size">Size of the original data</param>
public record FileBinaryDto(string Base64, int Size)
{
public FileBinaryDto(FileBinary fileBinary) : this(
Convert.ToBase64String(fileBinary.Buffer),
fileBinary.Size) { }
}
/// <summary>
/// Parameters for creating a MediaBinary
/// </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>
public record MediaBinaryParams(byte[] Buffer, int Size, string Extension)
: FileBinaryParams(Buffer, Size);
/// <summary>
/// Media binary with extension information
/// </summary>
public class MediaBinary : FileBinary
{
public string Extension { get; }
public MediaBinary(MediaBinaryParams parameters) : base(parameters)
{
Extension = parameters.Extension;
}
public static MediaBinary From(MediaBinaryDto dto)
{
var buffer = Convert.FromBase64String(dto.Base64);
var extension = GetExtensionType(dto.Mime);
return new MediaBinary(new MediaBinaryParams(buffer, dto.Size, extension));
}
private static string GetExtensionType(string mime)
{
return MimeTypeExtensions.GetExtension(mime);
}
}
/// <summary>
/// DTO for MediaBinary 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>
public record MediaBinaryDto(string Base64, int Size, string Mime) : FileBinaryDto(Base64, Size)
{
public MediaBinaryDto(MediaBinary mediaBinary) : this(
Convert.ToBase64String(mediaBinary.Buffer),
mediaBinary.Size,
MimeTypeExtensions.GetMimeType(mediaBinary.Extension)) { }
}
/// <summary>
/// Parameters for creating an ImageBinary
/// </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="AspectRatio">The aspect ratio of the image</param>
public record ImageBinaryParams(byte[] Buffer, int Size, string Extension, double AspectRatio)
: MediaBinaryParams(Buffer, Size, Extension);
/// <summary>
/// Image binary with aspect ratio information
/// </summary>
public class ImageBinary : MediaBinary
{
public double AspectRatio { get; }
public ImageBinary(ImageBinaryParams parameters) : base(parameters)
{
AspectRatio = parameters.AspectRatio;
}
public static ImageBinary From(ImageBinaryDto dto)
{
var buffer = Convert.FromBase64String(dto.Base64);
var extension = GetExtensionType(dto.Mime);
return new ImageBinary(new ImageBinaryParams(buffer, dto.Size, extension, dto.AspectRatio));
}
private static string GetExtensionType(string mime)
{
return MimeTypeExtensions.GetExtension(mime);
}
}
/// <summary>
/// DTO for ImageBinary 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="AspectRatio">The aspect ratio of the image</param>
public record ImageBinaryDto(string Base64, int Size, string Mime, double AspectRatio)
: MediaBinaryDto(Base64, Size, Mime)
{
public ImageBinaryDto(ImageBinary imageBinary) : this(
Convert.ToBase64String(imageBinary.Buffer),
imageBinary.Size,
MimeTypeExtensions.GetMimeType(imageBinary.Extension),
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>
public static class MimeTypeExtensions
{
private static readonly Dictionary<string, string> MimeTypes = new()
{
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
{ ".gif", "image/gif" },
{ ".webp", "image/webp" },
{ ".svg", "image/svg+xml" },
{ ".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()
{
{ "image/jpeg", ".jpg" },
{ "image/png", ".png" },
{ "image/gif", ".gif" },
{ "image/webp", ".webp" },
{ "image/svg+xml", ".svg" },
{ "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)
{
return MimeTypes.TryGetValue(extension.ToLowerInvariant(), out var mime)
? mime
: "application/octet-stream";
}
public static string GetExtension(string mime)
{
return Extensions.TryGetValue(mime.ToLowerInvariant(), out var extension)
? extension
: ".bin";
}
}
@@ -1,11 +0,0 @@
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Enum representing different types of media vaults
/// </summary>
public enum MediaVaultType
{
Media,
Image,
Audio
}
@@ -1,33 +0,0 @@
using System.Text.Json.Serialization;
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Base metadata for media entries
/// </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")]
[JsonDerivedType(typeof(AudioMetaData), typeDiscriminator: "audio")]
public record MetaData(string MediaKey, string Extension);
/// <summary>
/// Extended metadata for image entries, including aspect ratio
/// </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="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);
-130
View File
@@ -1,130 +0,0 @@
# FileDatabase C# Port
This is a C# port of the TypeScript file database system, maintaining architectural integrity while leveraging C# language features.
## Architecture Overview
The C# port preserves the original three-layer architecture:
### 1. **FileDatabase** (Main Orchestrator)
- **Location**: `Services/FileDatabase.cs`
- **Purpose**: Root-level manager coordinating multiple media vaults
- **Key Features**:
- Manages collection of `MediaVault` instances using `StructuralMap<string, MediaVault>`
- Provides async factory method `FromAsync()` for initialization
- Handles vault creation, resource loading, and resource registration
### 2. **MediaVault System** (Storage Containers)
- **Location**: `Services/MediaVault.cs`
- **Components**:
- `MediaVault` (Abstract base class)
- `ImageDirectoryVault` (Concrete implementation for images)
- **Key Features**:
- File path normalization and media key generation
- Generic type-safe operations using `MediaVaultType` enum
- Metadata association with stored files
- Async factory pattern for initialization
### 3. **Index Management** (Metadata & Organization)
- **Location**: `Services/IndexSystem.cs`
- **Two-tier indexing**:
- `DirectoryIndex`: Manages vault entries (what vaults exist)
- `VaultIndex`: Manages media entries within vaults (what files exist + metadata)
- **Features**:
- `IndexFactory` handles index creation/loading
- Automatic JSON serialization to filesystem
- Strong typing with generic constraints
## Key Components
### Models (`Models/` directory)
- **String-based keys**: Simple string identifiers for vault and entry management
- **`MetaData` hierarchy**: Base metadata → `ImageMetaData` with aspect ratio
- **Media Types**: `FileBinary``MediaBinary``ImageBinary`
- **Factory Classes**: Type-safe creation of media objects and metadata
### Utilities (`Utils/` directory)
- **`StructuralMap<TKey, TValue>`**: JSON-based structural equality for complex keys
- **`StructuralSet<T>`**: Set with structural equality semantics
- **`FileUtils`**: Async file I/O operations with chunked reading/writing
## C# Design Improvements
### SOLID Principles Applied
1. **Single Responsibility**: Each class handles one concern
2. **Open/Closed**: Extensible media types via generic constraints
3. **Liskov Substitution**: Proper inheritance hierarchies
4. **Interface Segregation**: Focused interfaces (`IIndex`)
5. **Dependency Inversion**: Abstract base classes and interfaces
### C# Language Features
- **Records**: Immutable data structures for `MetaData` hierarchy
- **Pattern Matching**: Switch expressions for type-safe factory methods
- **Nullable Reference Types**: Explicit nullability handling
- **Async/Await**: Full async support with `Task<T>` and `ValueTask<T>`
- **Generic Constraints**: Strong typing with `where` clauses
### DRY Implementation
- **Factory Pattern**: Centralized object creation logic
- **Generic Type Maps**: Reusable type mappings for different media types
- **Template Method Pattern**: Common functionality in base classes
## Usage Example
```csharp
// Initialize the database
var database = await FileDatabase.FromAsync("/path/to/database");
// Create a vault
var vaultId = "images";
await database.CreateVaultAsync(vaultId, MediaVaultType.Image);
// Store an image (MediaVaultType inferred from ImageBinary)
var imageData = new ImageBinary(new ImageBinaryParams(buffer, size, ".jpg", 1.5));
await database.RegisterResourceAsync("gallery", "photo1", imageData);
// Load an image (MediaVaultType inferred from ImageBinary generic type)
var loadedImage = await database.LoadResourceAsync<ImageBinary>("gallery", "photo1");
```
## Project Structure
```
FileDatabase/
├── Models/
│ ├── [EntryKey removed] # Now using simple string keys
│ ├── MetaData.cs # Metadata hierarchy
│ ├── MediaModels.cs # Binary data classes
│ ├── MediaFactories.cs # Factory pattern implementations
│ ├── MediaVaultType.cs # Enum for vault types
│ ├── IIndex.cs # Index interface
│ └── IndexData.cs # Index implementations
├── Services/
│ ├── FileDatabase.cs # Main orchestrator
│ ├── MediaVault.cs # Vault system
│ └── IndexSystem.cs # Index management
├── Utils/
│ ├── StructuralMap.cs # Structural equality map
│ ├── StructuralSet.cs # Structural equality set
│ └── FileUtils.cs # File I/O utilities
└── FileDatabase.csproj # Project file (.NET 9.0)
```
## Key Architectural Decisions
1. **Async-First Design**: All I/O operations are asynchronous
2. **Strong Type Safety**: Extensive use of generics and constraints
3. **Structural Equality**: JSON-based equality for composite keys
4. **Separation of Concerns**: Clear boundaries between indexing, storage, and media handling
5. **Factory-Based Initialization**: Handles complex async setup patterns
6. **Metadata-Driven**: Rich metadata system supporting extensible media types
## Differences from TypeScript Version
1. **Explicit Type Safety**: C# compiler enforces type constraints at compile time
2. **Memory Management**: Automatic garbage collection vs manual buffer management
3. **Serialization**: System.Text.Json instead of V8 serialization
4. **Error Handling**: Exceptions vs try/catch patterns (maintained original behavior)
5. **Nullability**: Explicit nullable reference types for better null safety
This port maintains the architectural integrity of the original TypeScript design while leveraging C#'s type system and language features for improved maintainability and performance.
@@ -1,190 +0,0 @@
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Main file database class that orchestrates multiple media vaults
/// </summary>
public class FileDatabase : DirectoryIndexDirectory
{
private readonly StructuralMap<string, MediaVault> _vaults;
/// <summary>
/// Factory method to create a FileDatabase instance
/// </summary>
public static async Task<FileDatabase?> FromAsync(string rootPath)
{
var factoryService = new IndexFactoryService();
var rootIndex = await factoryService.LoadOrCreateDirectoryIndexAsync(rootPath);
if (rootIndex != null)
{
var db = new FileDatabase(rootPath, (DirectoryIndex)rootIndex);
await db.InitVaultsAsync();
return db;
}
return null;
}
private FileDatabase(string rootPath, DirectoryIndex index) : base(rootPath, index)
{
_vaults = new StructuralMap<string, MediaVault>();
}
/// <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
/// </summary>
private async Task InitVaultAsync(string vaultId, MediaVaultType vaultType)
{
var path = Path.Combine(RootPath, vaultId);
var directoryVault = await MediaVaultFactory.From(path, vaultType);
if (directoryVault != null)
{
_vaults.Set(vaultId, directoryVault);
}
}
/// <summary>
/// Gets vault type from the vault's index file
/// </summary>
private async Task<MediaVaultType?> GetVaultTypeFromIndex(string vaultId)
{
try
{
var factoryService = new IndexFactoryService();
var vaultPath = Path.Combine(RootPath, vaultId);
var index = await factoryService.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
/// </summary>
public async Task CreateVaultAsync(string vaultId, MediaVaultType vaultType)
{
try
{
var path = Path.Combine(RootPath, vaultId);
var directoryVault = await MediaVaultFactory.From(path, vaultType);
if (directoryVault != null)
{
_vaults.Set(vaultId, directoryVault);
// Now using string-based index
await AddToIndexAsync(vaultId);
}
}
catch
{
throw;
}
}
/// <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>
/// 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;
}
}
@@ -1,126 +0,0 @@
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<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);
}
}
@@ -1,101 +0,0 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
namespace DeepDrftContent.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; }
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 bool HasIndexEntry(string entryId) => Index.HasEntry(entryId);
}
/// <summary>
/// Directory index directory implementation
/// </summary>
public class DirectoryIndexDirectory : IndexDirectory
{
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(string entryId)
{
_directoryIndex.PutEntry(entryId);
await SaveIndexAsync(_directoryIndex);
}
}
/// <summary>
/// Vault index directory implementation
/// </summary>
public class VaultIndexDirectory : IndexDirectory
{
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(string entryId, MetaData metaData)
{
_vaultIndex.PutEntry(entryId, metaData);
await SaveIndexAsync(_vaultIndex);
}
}
@@ -1,144 +0,0 @@
using System.Text.RegularExpressions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
namespace DeepDrftContent.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) : base(rootPath, index) { }
/// <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>();
if (!HasIndexEntry(entryId))
return null;
if (Index is not VaultIndex vaultIndex)
return null;
var metaData = vaultIndex.GetEntry(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>
/// 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) : base(rootPath, index) { }
/// <summary>
/// Factory method to create an ImageVault instance
/// </summary>
public static async Task<ImageVault?> FromAsync(string rootPath)
{
var factoryService = new IndexFactoryService();
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Image);
if (index != null)
{
return new ImageVault(rootPath, (VaultIndex)index);
}
return null;
}
}
public class AudioVault : MediaVault
{
private AudioVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
public static async Task<AudioVault?> FromAsync(string rootPath)
{
var factoryService = new IndexFactoryService();
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Audio);
if (index != null)
{
return new AudioVault(rootPath, (VaultIndex)index);
}
return null;
}
}
@@ -1,17 +0,0 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Factory for creating media vaults
/// </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);
}
}
@@ -1,173 +0,0 @@
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();
// 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));
}
@@ -1,118 +0,0 @@
using System.Text.Json;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Utils;
/// <summary>
/// Utility class for file I/O operations, matching the TypeScript file utilities
/// </summary>
public static class FileUtils
{
/// <summary>
/// Reads a file and returns it as a FileBinary object
/// </summary>
/// <param name="mediaPath">Path to the media file</param>
/// <returns>FileBinary containing the file data</returns>
public static async Task<FileBinary> FetchFileAsync(string mediaPath)
{
using var fileStream = new FileStream(mediaPath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 64 * 1024);
var buffer = new byte[fileStream.Length];
var totalBytesRead = 0;
while (totalBytesRead < fileStream.Length)
{
var bytesRead = await fileStream.ReadAsync(buffer.AsMemory(totalBytesRead));
if (bytesRead == 0)
throw new EndOfStreamException($"Unexpected end of stream while reading {mediaPath}");
totalBytesRead += bytesRead;
}
return new FileBinary(new FileBinaryParams(buffer, buffer.Length));
}
/// <summary>
/// Writes binary data to a file
/// </summary>
/// <param name="mediaPath">Path where to write the file</param>
/// <param name="buffer">Binary data to write</param>
public static async Task PutFileAsync(string mediaPath, byte[] buffer)
{
const int chunkSize = 64 * 1024;
using var fileStream = new FileStream(mediaPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: chunkSize);
for (int offset = 0; offset < buffer.Length; offset += chunkSize)
{
var length = Math.Min(chunkSize, buffer.Length - offset);
await fileStream.WriteAsync(buffer.AsMemory(offset, length));
}
await fileStream.FlushAsync();
}
/// <summary>
/// Serializes an object to a file using JSON
/// </summary>
/// <param name="filePath">Path to the file</param>
/// <param name="obj">Object to serialize</param>
public static async Task PutObjectAsync<T>(string filePath, T obj)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var json = JsonSerializer.Serialize(obj, options);
await File.WriteAllTextAsync(filePath, json);
}
/// <summary>
/// Deserializes an object from a JSON file
/// </summary>
/// <param name="filePath">Path to the file</param>
/// <returns>Deserialized object</returns>
public static async Task<T> FetchObjectAsync<T>(string filePath)
{
var json = await File.ReadAllTextAsync(filePath);
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var result = JsonSerializer.Deserialize<T>(json, options);
return result ?? throw new InvalidOperationException($"Failed to deserialize object from {filePath}");
}
/// <summary>
/// Creates a directory if it doesn't exist
/// </summary>
/// <param name="directoryPath">Path to the directory</param>
public static Task MakeVaultDirectoryAsync(string directoryPath)
{
Directory.CreateDirectory(directoryPath);
return Task.CompletedTask;
}
/// <summary>
/// Checks if a file exists
/// </summary>
/// <param name="filePath">Path to check</param>
/// <returns>True if file exists</returns>
public static bool FileExists(string filePath)
{
return File.Exists(filePath);
}
/// <summary>
/// Checks if a directory exists
/// </summary>
/// <param name="directoryPath">Path to check</param>
/// <returns>True if directory exists</returns>
public static bool DirectoryExists(string directoryPath)
{
return Directory.Exists(directoryPath);
}
}
@@ -1,125 +0,0 @@
using System.Collections;
using System.Text.Json;
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>> 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)
{
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
{
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>
/// Sets a key-value pair in the map
/// </summary>
public StructuralMap<TKey, TValue> Set(TKey key, TValue value)
{
var keyString = GetKeyString(key);
_innerMap[keyString] = new KeyValuePair<TKey, TValue>(key, value);
return this;
}
/// <summary>
/// Gets a value by key, or default if not found
/// </summary>
public TValue? Get(TKey key)
{
var keyString = GetKeyString(key);
return _innerMap.TryGetValue(keyString, out var pair) ? pair.Value : default;
}
/// <summary>
/// Checks if the map contains the specified key
/// </summary>
public bool Has(TKey key)
{
var keyString = GetKeyString(key);
return _innerMap.ContainsKey(keyString);
}
/// <summary>
/// Removes a key-value pair from the map
/// </summary>
public bool Delete(TKey key)
{
var keyString = GetKeyString(key);
return _innerMap.Remove(keyString);
}
/// <summary>
/// Clears all entries from the map
/// </summary>
public void Clear() => _innerMap.Clear();
/// <summary>
/// Gets the number of entries in the map
/// </summary>
public int Size => _innerMap.Count;
/// <summary>
/// Enumerates all key-value pairs
/// </summary>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _innerMap.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Gets all keys in the map
/// </summary>
public IEnumerable<TKey> Keys => _innerMap.Values.Select(pair => pair.Key);
/// <summary>
/// Gets all values in the map
/// </summary>
public IEnumerable<TValue> Values => _innerMap.Values.Select(pair => pair.Value);
/// <summary>
/// Executes a callback for each key-value pair
/// </summary>
public void ForEach(Action<TValue, TKey, StructuralMap<TKey, TValue>> callback)
{
foreach (var (key, value) in this)
{
callback(value, key, this);
}
}
}
@@ -1,102 +0,0 @@
using System.Collections;
using System.Text.Json;
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> 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)
{
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
{
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>
/// Adds a value to the set
/// </summary>
public StructuralSet<T> Add(T value)
{
var valueString = GetValueString(value);
if (!_innerMap.ContainsKey(valueString))
{
_innerMap[valueString] = value;
}
return this;
}
/// <summary>
/// Checks if the set contains the specified value
/// </summary>
public bool Has(T value)
{
var valueString = GetValueString(value);
return _innerMap.ContainsKey(valueString);
}
/// <summary>
/// Removes a value from the set
/// </summary>
public bool Delete(T value)
{
var valueString = GetValueString(value);
return _innerMap.Remove(valueString);
}
/// <summary>
/// Clears all values from the set
/// </summary>
public void Clear() => _innerMap.Clear();
/// <summary>
/// Gets the number of values in the set
/// </summary>
public int Size => _innerMap.Count;
/// <summary>
/// Enumerates all values in the set
/// </summary>
public IEnumerator<T> GetEnumerator()
{
return _innerMap.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Gets all values in the set
/// </summary>
public IEnumerable<T> Values => _innerMap.Values;
}
@@ -1,161 +0,0 @@
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.Processors;
/// <summary>
/// Service for processing audio files and extracting metadata
/// </summary>
public class AudioProcessor
{
/// <summary>
/// Processes a WAV file and creates an AudioBinary object
/// </summary>
/// <param name="filePath">Path to the WAV file</param>
/// <returns>AudioBinary object with metadata</returns>
public async Task<AudioBinary?> ProcessWavFileAsync(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"WAV file not found: {filePath}");
}
if (!Path.GetExtension(filePath).Equals(".wav", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("File must be a WAV file", nameof(filePath));
}
try
{
var buffer = await File.ReadAllBytesAsync(filePath);
var wavInfo = ExtractWavMetadata(buffer);
var parameters = new AudioBinaryParams(
Buffer: buffer,
Size: buffer.Length,
Extension: ".wav",
Duration: wavInfo.Duration,
Bitrate: wavInfo.Bitrate
);
return new AudioBinary(parameters);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to process WAV file: {ex.Message}", ex);
}
}
/// <summary>
/// Extracts metadata from WAV file buffer
/// </summary>
private WavMetadata ExtractWavMetadata(byte[] buffer)
{
try
{
// WAV file format parsing
// RIFF header starts at byte 0
if (buffer.Length < 44)
{
throw new InvalidDataException("WAV file too short to contain valid header");
}
// Check RIFF signature
var riffSignature = System.Text.Encoding.ASCII.GetString(buffer, 0, 4);
if (riffSignature != "RIFF")
{
throw new InvalidDataException("Invalid WAV file: Missing RIFF signature");
}
// Check WAVE format
var waveSignature = System.Text.Encoding.ASCII.GetString(buffer, 8, 4);
if (waveSignature != "WAVE")
{
throw new InvalidDataException("Invalid WAV file: Missing WAVE signature");
}
// Find fmt chunk
var fmtChunkPos = FindChunk(buffer, "fmt ");
if (fmtChunkPos == -1)
{
throw new InvalidDataException("Invalid WAV file: Missing fmt chunk");
}
// Parse fmt chunk
var fmtChunkSize = BitConverter.ToUInt32(buffer, fmtChunkPos + 4);
var sampleRate = BitConverter.ToUInt32(buffer, fmtChunkPos + 12);
var byteRate = BitConverter.ToUInt32(buffer, fmtChunkPos + 16);
var channels = BitConverter.ToUInt16(buffer, fmtChunkPos + 10);
var bitsPerSample = BitConverter.ToUInt16(buffer, fmtChunkPos + 22);
// Find data chunk
var dataChunkPos = FindChunk(buffer, "data");
if (dataChunkPos == -1)
{
throw new InvalidDataException("Invalid WAV file: Missing data chunk");
}
var dataSize = BitConverter.ToUInt32(buffer, dataChunkPos + 4);
// Calculate duration
var duration = (double)dataSize / byteRate;
// Calculate bitrate (bits per second / 1000 for kbps)
var bitrate = (int)((sampleRate * channels * bitsPerSample) / 1000);
return new WavMetadata
{
Duration = duration,
Bitrate = bitrate,
SampleRate = (int)sampleRate,
Channels = channels,
BitsPerSample = bitsPerSample
};
}
catch (Exception ex)
{
// Fallback to basic metadata if parsing fails
Console.WriteLine($"Warning: Could not parse WAV metadata: {ex.Message}");
return new WavMetadata
{
Duration = 180.0, // Default 3 minutes
Bitrate = 1411, // Default CD quality bitrate for WAV
SampleRate = 44100,
Channels = 2,
BitsPerSample = 16
};
}
}
/// <summary>
/// Finds a chunk in the WAV file buffer
/// </summary>
private int FindChunk(byte[] buffer, string chunkId)
{
var chunkBytes = System.Text.Encoding.ASCII.GetBytes(chunkId);
for (int i = 12; i < buffer.Length - 8; i += 4)
{
if (buffer[i] == chunkBytes[0] &&
buffer[i + 1] == chunkBytes[1] &&
buffer[i + 2] == chunkBytes[2] &&
buffer[i + 3] == chunkBytes[3])
{
return i;
}
}
return -1;
}
/// <summary>
/// WAV file metadata
/// </summary>
private class WavMetadata
{
public double Duration { get; set; }
public int Bitrate { get; set; }
public int SampleRate { get; set; }
public int Channels { get; set; }
public int BitsPerSample { get; set; }
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
using DeepDrftContent;
using DeepDrftContent.FileDatabase.Services;
using DeepDrftContent.Services.FileDatabase.Services;
using DeepDrftContent.Middleware;
using DeepDrftContent.Models;
using Microsoft.AspNetCore.HttpOverrides;
-112
View File
@@ -1,112 +0,0 @@
using DeepDrftContent.Constants;
using DeepDrftContent.FileDatabase.Services;
using DeepDrftContent.Processors;
using DeepDrftModels.Entities;
namespace DeepDrftContent.Services;
/// <summary>
/// Service for managing tracks in both SQL and FileDatabase
/// </summary>
public class TrackService
{
private readonly FileDatabase.Services.FileDatabase _fileDatabase;
private readonly AudioProcessor _audioProcessor;
public TrackService(FileDatabase.Services.FileDatabase fileDatabase, AudioProcessor audioProcessor)
{
_fileDatabase = fileDatabase;
_audioProcessor = audioProcessor;
}
/// <summary>
/// Adds a new track from a WAV file to both databases
/// </summary>
/// <param name="wavFilePath">Path to the WAV file</param>
/// <param name="trackName">Name of the track</param>
/// <param name="artist">Artist name</param>
/// <param name="album">Optional album name</param>
/// <param name="genre">Optional genre</param>
/// <param name="releaseDate">Optional release date</param>
/// <returns>The track entity with generated ID and media path</returns>
public async Task<TrackEntity?> AddTrackFromWavAsync(
string wavFilePath,
string trackName,
string artist,
string? album = null,
string? genre = null,
DateOnly? releaseDate = null)
{
try
{
// Process the WAV file
var audioBinary = await _audioProcessor.ProcessWavFileAsync(wavFilePath);
if (audioBinary == null)
{
throw new InvalidOperationException("Failed to process WAV file");
}
// Generate a unique track ID
var trackId = Guid.NewGuid().ToString();
// Ensure tracks vault exists
if (!_fileDatabase.HasVault(VaultConstants.Tracks))
{
await _fileDatabase.CreateVaultAsync(VaultConstants.Tracks, DeepDrftContent.FileDatabase.Models.MediaVaultType.Audio);
}
// Store the audio in FileDatabase
var success = await _fileDatabase.RegisterResourceAsync(VaultConstants.Tracks, trackId, audioBinary);
if (!success)
{
throw new InvalidOperationException("Failed to store audio in FileDatabase");
}
// Create the track entity for SQL database
var trackEntity = new TrackEntity
{
EntryKey = trackId, // FileDatabase entry ID
TrackName = trackName,
Artist = artist,
Album = album,
Genre = genre,
ReleaseDate = releaseDate
};
return trackEntity;
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to add track: {ex.Message}", ex);
}
}
/// <summary>
/// Retrieves audio binary from FileDatabase
/// </summary>
/// <param name="trackId">Track ID (EntryKey)</param>
/// <returns>Audio binary or null if not found</returns>
public async Task<DeepDrftContent.FileDatabase.Models.AudioBinary?> GetAudioBinaryAsync(string trackId)
{
return await _fileDatabase.LoadResourceAsync<DeepDrftContent.FileDatabase.Models.AudioBinary>(VaultConstants.Tracks, trackId);
}
/// <summary>
/// Checks if FileDatabase is available and tracks vault exists
/// </summary>
public bool IsFileDatabaseReady()
{
return _fileDatabase.HasVault(VaultConstants.Tracks);
}
/// <summary>
/// Initializes the tracks vault if it doesn't exist
/// </summary>
public async Task InitializeTracksVaultAsync()
{
if (!_fileDatabase.HasVault(VaultConstants.Tracks))
{
await _fileDatabase.CreateVaultAsync(VaultConstants.Tracks, DeepDrftContent.FileDatabase.Models.MediaVaultType.Audio);
}
}
}
+5 -5
View File
@@ -1,6 +1,6 @@
using DeepDrftContent.Constants;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Services;
using DeepDrftContent.Services.Constants;
using DeepDrftContent.Services.FileDatabase.Models;
using DeepDrftContent.Services.FileDatabase.Services;
using DeepDrftContent.Models;
namespace DeepDrftContent
@@ -14,13 +14,13 @@ namespace DeepDrftContent
var fileDatabaseSettings = builder.Configuration.GetSection(nameof(FileDatabaseSettings)).Get<FileDatabaseSettings>();
if (fileDatabaseSettings is null) { throw new Exception("File database settings are not configured"); }
var fileDatabase = await FileDatabase.Services.FileDatabase.FromAsync(fileDatabaseSettings.VaultPath);
var fileDatabase = await 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)
private static async Task InitializeTrackVault(FileDatabase fileDatabase)
{
if (!fileDatabase.HasVault(VaultConstants.Tracks))
{