Files
Khaemhat/tests/Khaemhat.Infrastructure.Tests/Files/AsyncOnlyStream.cs
T
Alby96andClaude Opus 5.5 b341ade9e9 feat: attachments, file store and in-house PDF/CSV/XLSX/ZIP writers (#34)
Documents need attachments and generated files (SPEC 9.4) before the UI of
M3 can show them, and the spec forbids external libraries for PDF and
spreadsheets.

- VolumeFileStore keeps files under <data>/files/<COMPANY>/<yyyy>/<guid>,
  writing a .part file in the same folder with size limit and SHA-256, then
  quota and scanner checks (IFileScanner hook, no-op by default).
- attachments table (migration 007) with kind, version series, lock, trash
  and the uploading user; AttachmentService uploads, adds generated files as
  the next version, lists, opens, zips, locks, trashes, restores and purges
  after 30 days (new system job system.attachments-trash).
- FileGenerationService runs IFileGenerator implementations into the store;
  three generators for the fictitious test document (PDF, CSV, XLSX).
- CsvWriter (Italian Excel defaults, formula-injection guard), XlsxWriter,
  ZipStreamWriter and PdfWriter (standard Type1 fonts, WinAnsi, AFM widths,
  JPEG, Flate, xref) only write asynchronously, so they can stream to an
  HTTP response.
- ADR 0021 records versioning, storage layout, PDF scope and CSV defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:06:48 +01:00

63 lines
2.2 KiB
C#

namespace Khaemhat.Infrastructure.Tests.Files;
/// <summary>
/// A write-only, non-seekable stream that refuses synchronous writes, like an ASP.NET Core response body: the writers
/// must stream to it with asynchronous calls only.
/// </summary>
internal sealed class AsyncOnlyStream : Stream
{
private readonly MemoryStream _inner = new();
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public bool WasDisposed { get; private set; }
public byte[] ToArray() => _inner.ToArray();
public override void Flush() => throw new InvalidOperationException("Synchronous flush.");
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new InvalidOperationException("Synchronous write.");
public override void Write(ReadOnlySpan<byte> buffer) => throw new InvalidOperationException("Synchronous write.");
public override void WriteByte(byte value) => throw new InvalidOperationException("Synchronous write.");
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
_inner.WriteAsync(buffer, offset, count, cancellationToken);
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) =>
_inner.WriteAsync(buffer, cancellationToken);
protected override void Dispose(bool disposing)
{
WasDisposed = true;
base.Dispose(disposing);
}
public override async ValueTask DisposeAsync()
{
WasDisposed = true;
await base.DisposeAsync();
}
}