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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 18:06:48 +01:00
1 parent cf6f849cc1
commit b341ade9e9
26 files changed
+5319 -2

No files matched your search

@@ -1,3 +1,4 @@
using Khaemhat.Core.Files;
using Khaemhat.Core.Instance;
using Khaemhat.Core.Security;
using Khaemhat.Data.Mapping;
@@ -106,3 +107,21 @@ public sealed class UserSettingMap : EntityMap<UserSetting>
Map(x => x.Key).MaxLength(200);
}
}
/// <summary>Map of <see cref="Attachment"/> to <c>attachments</c>.</summary>
public sealed class AttachmentMap : EntityMap<Attachment>
{
/// <summary>Creates the map.</summary>
public AttachmentMap()
{
ToTable("attachments");
Map(x => x.Entity).MaxLength(100);
Map(x => x.Name).MaxLength(255);
Map(x => x.Mime).MaxLength(150);
Map(x => x.Sha256).MaxLength(64);
Map(x => x.VersionKey).MaxLength(300);
Map(x => x.GeneratedBy).MaxLength(100);
Map(x => x.StorageKey).MaxLength(60);
Map(x => x.UserName).MaxLength(100);
}
}
@@ -0,0 +1,37 @@
using Khaemhat.Data.Migrations;
using Khaemhat.Data.Schema;
namespace Khaemhat.Data.CompanyDatabase.Migrations;
/// <summary>The attachments of the company records, uploaded and generated (SPEC §9.4, ADR 0021).</summary>
[Migration(20261111_007, "create_attachments")]
public sealed class M20261111_007_CreateAttachments : IMigration
{
/// <inheritdoc />
public void Up(ISchemaBuilder schema)
{
ArgumentNullException.ThrowIfNull(schema);
schema.CreateTable("attachments", t =>
{
t.Id();
t.String("entity", 100).NotNull();
t.Int64("entity_id").NotNull();
t.String("kind", 40).NotNull();
t.String("name", 255).NotNull();
t.String("mime", 150).NotNull();
t.Int64("size").NotNull();
t.String("sha256", 64).NotNull();
t.Int32("version").NotNull();
t.String("version_key", 300).NotNull();
t.String("generated_by", 100);
t.String("storage_key", 60).NotNull();
t.String("user_name", 100);
t.Bool("locked").NotNull().Default(false);
t.DateTimeUtc("deleted_at");
t.AuditColumns();
t.Index("ix_attachments_entity_entity_id", "entity", "entity_id");
t.Unique("ux_attachments_entity_entity_id_version_key_version", "entity", "entity_id", "version_key", "version");
t.Index("ix_attachments_deleted_at", "deleted_at");
});
}
}