Files
Nexus.Reader/src/NexusReader.Infrastructure/Services/BookStorageService.cs
T
Antigravity 5a2223a4c8 feat: Ingestion Pipeline Stabilization and WASM Service Proxies (#42)
This PR stabilizes the Nexus Ingestion Engine by implementing functional service proxies for the Blazor WASM client and refining the backend infrastructure for real-time progress tracking and database compatibility.

### Key Changes
- **Infrastructure Stabilization**:
  - Implemented production-grade `EbookRepository` with PostgreSQL `EF.Functions.ILike` support.
  - Enforced `IsReadyForReading = false` state for newly added ebooks (resolves #35).
  - Updated `SignalRSyncBroadcaster` to support targeted user messaging and ingestion-specific progress updates (resolves #37).
- **WASM Client Functional Proxies**:
  - Replaced "Throwing" dummy services with `WasmEbookRepository`, `WasmSyncBroadcaster`, `WasmBookStorageService`, and `WasmEmbeddingGenerator`.
  - These services proxy requests to the backend via a new set of Minimal API endpoints in `NexusReader.Web`.
- **Domain Refinement**:
  - Added `IsReadyForReading` flag to the `Ebook` entity to manage background AI processing states.

### Related Issues
- Fixes #35
- Fixes #36
- Fixes #37

---------

Co-authored-by: Marek Jasiński <jasins.marek@gmail.com>
Reviewed-on: #42
Co-authored-by: Antigravity <antigravity@google.com>
Co-committed-by: Antigravity <antigravity@google.com>
2026-05-13 18:24:24 +00:00

74 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Hosting;
using NexusReader.Application.Abstractions.Services;
namespace NexusReader.Infrastructure.Services;
/// <summary>
/// Infrastructure implementation of book storage using local filesystem.
/// All paths returned are relative to the web root.
/// </summary>
public class BookStorageService : IBookStorageService
{
private readonly IWebHostEnvironment _environment;
public BookStorageService(IWebHostEnvironment environment)
{
_environment = environment;
}
public async Task<string> SaveEbookAsync(byte[] data, string fileName)
{
using var stream = new MemoryStream(data);
return await SaveEbookAsync(stream, fileName);
}
public async Task<string> SaveEbookAsync(Stream data, string fileName)
{
var uploadsFolder = Path.Combine(_environment.WebRootPath, "uploads");
EnsureDirectoryExists(uploadsFolder);
var uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
var filePath = Path.Combine(uploadsFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await data.CopyToAsync(fileStream);
}
// Use forward-slash explicitly: Path.Combine produces backslashes on Windows
// which would cause 404s when stored as web-relative paths.
return $"uploads/{uniqueFileName}";
}
public async Task<string?> SaveCoverAsync(byte[] data, string fileName)
{
if (data == null || data.Length == 0) return null;
using var stream = new MemoryStream(data);
return await SaveCoverAsync(stream, fileName);
}
public async Task<string?> SaveCoverAsync(Stream data, string fileName)
{
var coversFolder = Path.Combine(_environment.WebRootPath, "covers");
EnsureDirectoryExists(coversFolder);
var uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
var filePath = Path.Combine(coversFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await data.CopyToAsync(fileStream);
}
return $"covers/{uniqueFileName}";
}
private void EnsureDirectoryExists(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}