Baustein 6: Datei-Anhaenge synchronisieren (Kapitel 10)
Anhaenge laufen bewusst NICHT ueber den JSON-Ereigniskanal (wuerde ihn
fuer Fotos/Scans stark aufblaehen), sondern ueber einen eigenen
verschluesselten Binaerkanal - analog zum bereits bestehenden Muster
in SnapshotService.
- Neue Endpunkte POST/GET /api/sync/attachments/{storageId} in
LehrerApp.Api (AttachmentStore, dateibasiert je Nutzer)
- EventQueue: neue, vom JSON-Ereignis getrennte Warteliste fuer
ausstehende Uploads (SyncEventPublisher traegt Anhaenge einer
gespeicherten Documentation dort ein)
- AttachmentSyncer laedt ausstehende Anhaenge hoch (in
SyncEngine.SyncNowAsync nach dem Event-Push)
- EventApplier laedt fehlende Anhaenge nach dem Anwenden eines
Documentation-Ereignisses nach - ueber die rohe Collection statt
IAttachmentStorage.Upload, da dieses immer eine neue Id vergaebe und
hier die Original-StorageId erhalten bleiben muss
Round-Trip-Tests belegen byteidentische Uebertragung.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
namespace LehrerApp.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Rohdaten-Ablage für verschlüsselte Datei-Anhänge, getrennt vom Ereignis-/Snapshot-Speicher.
|
||||
/// Server sieht nur verschlüsselte Bytes, kein LiteDB nötig - einfache Dateien pro Nutzer/Id.
|
||||
/// </summary>
|
||||
public class AttachmentStore(string dataPath)
|
||||
{
|
||||
private readonly string _root = Path.Combine(dataPath, "attachments");
|
||||
|
||||
public async Task StoreAsync(string userId, string storageId, Stream content)
|
||||
{
|
||||
var dir = Path.Combine(_root, Safe(userId));
|
||||
Directory.CreateDirectory(dir);
|
||||
await using var file = File.Create(Path.Combine(dir, Safe(storageId)));
|
||||
await content.CopyToAsync(file);
|
||||
}
|
||||
|
||||
public Stream? OpenRead(string userId, string storageId)
|
||||
{
|
||||
var path = Path.Combine(_root, Safe(userId), Safe(storageId));
|
||||
return File.Exists(path) ? File.OpenRead(path) : null;
|
||||
}
|
||||
|
||||
// storageId kommt als Routen-Parameter vom Client - nie ungeprüft in einen Dateipfad
|
||||
// übernehmen (Path-Traversal).
|
||||
private static string Safe(string value) =>
|
||||
string.Concat(value.Where(c => char.IsLetterOrDigit(c) || c == '-'));
|
||||
}
|
||||
@@ -48,6 +48,30 @@ public static class Endpoints
|
||||
});
|
||||
}
|
||||
|
||||
// ── Anhänge (eigener Binärkanal, getrennt vom JSON-Ereigniskanal) ──────────
|
||||
|
||||
public static void MapAttachmentEndpoints(this WebApplication app)
|
||||
{
|
||||
var g = app.MapGroup("/api/sync/attachments").RequireAuthorization();
|
||||
g.MapPost("/{storageId}", async (string storageId, HttpRequest req,
|
||||
ClaimsPrincipal user, AttachmentStore store) =>
|
||||
{
|
||||
var uid = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (uid is null) return Results.Unauthorized();
|
||||
if (req.ContentLength is null or > LehrerApp.Core.Interfaces.IAttachmentStorage.MaxSizeBytes)
|
||||
return Results.BadRequest("Datei zu groß oder Content-Length fehlt.");
|
||||
await store.StoreAsync(uid, storageId, req.Body);
|
||||
return Results.Ok();
|
||||
});
|
||||
g.MapGet("/{storageId}", (string storageId, ClaimsPrincipal user, AttachmentStore store) =>
|
||||
{
|
||||
var uid = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (uid is null) return Results.Unauthorized();
|
||||
var stream = store.OpenRead(uid, storageId);
|
||||
return stream is null ? Results.NotFound() : Results.Stream(stream, "application/octet-stream");
|
||||
});
|
||||
}
|
||||
|
||||
// ── Snapshot (Device-Pairing) ─────────────────────────────────────────────
|
||||
|
||||
public static void MapSnapshotEndpoints(this WebApplication app)
|
||||
|
||||
@@ -30,6 +30,7 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddSingleton<UserStore>(_ => new UserStore(data));
|
||||
builder.Services.AddSingleton<AttachmentStore>(_ => new AttachmentStore(data));
|
||||
builder.Services.AddSingleton<EventStore>(_ => new EventStore(data));
|
||||
builder.Services.AddSingleton<SnapshotStore>(_ => new SnapshotStore(data));
|
||||
builder.Services.AddSingleton<ReadableSnapshotStore>(_ => new ReadableSnapshotStore(data));
|
||||
@@ -41,6 +42,7 @@ app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.MapAuthEndpoints(secret);
|
||||
app.MapSyncEndpoints();
|
||||
app.MapAttachmentEndpoints();
|
||||
app.MapSnapshotEndpoints();
|
||||
app.MapReadableSnapshotEndpoints();
|
||||
app.MapPlainSyncEndpoints();
|
||||
|
||||
Reference in New Issue
Block a user