Appearance
Getting Started
This page wires the module into a host and walks one file through its life. Why each step exists is in ADR 0010; the exact contract of every field and route is in the specification.
Prerequisites
- PostgreSQL for the module's own schema.
- An S3-compatible, Azure Blob or Google Cloud Storage bucket, configured in the
GrydStoragesection thatAddGrydObjectStoragereads. MinIO works through the S3 provider (ServiceUrl+ForcePathStyle). - GrydJobs, because the confirmation hands the file to the scan job through the job queue, and the purges run as recurring jobs.
clamdreachable from the API, for every profile that does not waive the antivirus. See Antivirus & Quarantine for running it.
1. Install
bash
dotnet add package GrydFiles # Core + Application + Infrastructure + API
dotnet add package GrydFiles.Infrastructure.ClamAv # the clamd adapter2. Register
The order is the one the module documents: object storage first, then the module, then its engine.
csharp
using Gryd.Infrastructure.Extensions;
using GrydFiles;
using GrydFiles.Infrastructure;
using GrydFiles.Infrastructure.ClamAv;
builder.Services.AddGrydJobs(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("GrydJobs");
});
builder.Services.AddGrydObjectStorage(builder.Configuration);
builder.Services.AddGrydFiles(builder.Configuration);
builder.Services.AddGrydFilesClamAv(builder.Configuration);
builder.Services.AddGrydFilesHealthCheck(); // optional: the scanner in /health
// The trail and the verdicts are written with each act; this delivers them after the commit (ADR 0012).
builder.Services.AddOutboxMessaging(outbox => outbox.AddGrydFilesOutbox(
(_, options) => options.UseNpgsql(builder.Configuration.GetConnectionString("GrydFiles"))));
builder.Services.AddOutboxWorker(); // on the process that drainsAddGrydFilesOutbox (namespace GrydFiles.Infrastructure.Messaging) goes inside the host's one AddOutboxMessaging. A host without it does not start — the trail and the verdicts would be staged in files.outbox and reach no store and no consumer — and the error says what to compose. AddOutboxWorker is the one half the module cannot require (a producer-only API is legitimate): a process that produces without draining logs it at start and reports gryd_files.outbox.drain_in_process = 0 (details). The process that drains must also compose AddGrydFiles and every consumer's AddGrydFilesConsumer, because the verdict handlers run there.
The trail has to end in a durable IAuditStore (GrydAudit's), or the erasure and the lifting of a legal hold answer FILE_AUDIT_TRAIL_UNAVAILABLE. A host where a consumer erases files in process also registers an IFileActorDirectory before AddGrydFiles — the check of the authors against the identity provider, which GrydFiles does not reference (Erasure). And the bucket needs a lifecycle rule that expires the _transit/ prefix after a day (Antivirus service).
The engine is its own call. AddGrydFilesClamAv registers ClamAV and binds GrydClamAv for the daemon's address, so the module's builder is not involved. Another engine (GuardDuty, an ICAP appliance) goes through the builder instead: AddGrydFiles(configuration, files => files.UseScanner<MyScanner>()). Do not combine the two: AddGrydFilesClamAv replaces whatever UseScanner<T>() registered.
UseScanner<T>() is the only method on the builder. There is no default profile: every upload names a profile of the GrydFiles:Profiles section, and an upload without a known one is refused with FILE_PROFILE_UNKNOWN. The framework itself declares only two profiles, gryd.report and gryd.notification-attachment, for the modules that store files here; a product declares its own in its appsettings (see Configuration).
The module refuses to start on an incoherent section — a profile ceiling above what clamd accepts, a retention mode without its days, a scanner timeout of zero. That is deliberate (D15): the failure is at deploy time, not on the first large upload. It also refuses to start with no engine at all. Without an IFileScanner the scan job cannot run, so every upload would stay in quarantine while the host reported itself healthy.
GrydReports and GrydNotifications
Both store their files here and refuse to start without their profile. Register object storage, GrydJobs and GrydFiles with the scanner before either module. The gryd-api and gryd-module templates do it for you when Reports or Notifications is included: object storage, the module with ClamAV, and the module's outbox with its worker (the template API drains its own outbox; move the worker to another process if you prefer, one that composes AddGrydFiles and every consumer).
3. Migrate
The module owns its schema and its migration history table, so it can sit next to any other module's migrations.
csharp
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
await app.ApplyGrydFilesMigrationsAsync();
}4. One file, end to end
Over HTTP, the client makes three calls to the API and two to the bucket. The API never sees the bytes (RN-GF-06).
http
### 1. Reserve — requires upload:files
POST /api/v1/files
Content-Type: application/json
{
"profile": "nexio.attachment",
"originalFileName": "cotacao.pdf",
"contentType": "application/pdf",
"declaredSizeBytes": 184320
}The answer carries the fileId, the uploadUrl, when it expires and the requiredHeaders the PUT must repeat — exactly those, since the signature covers them. Which ones depends on the provider: always Content-Type; x-goog-content-length-range on GCS, which signs the declared size; x-ms-blob-type on Azure. On S3 the size is signed too, through the Content-Length every client sends on its own. The URL writes to a transit key, never to the object that is later scanned and served (RN-GF-18).
http
### 2. Send the bytes straight to the bucket — not to the API
PUT {uploadUrl}
Content-Type: application/pdf
<the file>
### 3. Confirm — requires upload:files. Answers at once, with status Scanning.
POST /api/v1/files/{fileId}/complete
Content-Type: application/json
{}The scan runs in the background. When GET /api/v1/files/{fileId} reports Available, the file can be referenced and downloaded:
http
### 4. Say who holds it — requires upload:files, and a scope managed by the HTTP surface
POST /api/v1/files/{fileId}/references
Content-Type: application/json
{ "ownerScope": "acme.requisition", "ownerKey": "REQ-2026-0042" }
### 5. A short URL to download it — requires read:files
GET /api/v1/files/{fileId}/download-url?purpose=previewThe download URL is followed from the browser, straight to the bucket, and answers Content-Disposition: attachment with the original name and Content-Type: application/octet-stream. When the owner lets go, DELETE /api/v1/files/{fileId}/references/{referenceId} releases the reference, and the retention job purges the object once the grace period has passed.
The route creates and releases references only for an ownerScope that its consumer registered with ManagedThroughHttpSurface() (RN-GF-23): the surface manages no scope by default, and any other — including a module's own scope, with every permission in the world — answers 403 FILE_OWNER_SCOPE_FORBIDDEN and changes nothing.
A reference can only be created over an Available file. A file still being scanned answers FILE_NOT_AVAILABLE_CONFLICT, an infected one FILE_INFECTED_CONFLICT — the screen should tell "still being checked" from "we found something".
5. From another module in the same host
A module running in the same process does not call the routes, and it does not pass its owner scope as text either. It declares a marker type for its scope in its own assembly, registers it, and receives ports bound to that scope (RN-GF-28):
csharp
public sealed class AttachmentScope : IFileOwnerScope
{
public static string Scope => "nexio.attachment";
}
services.AddGrydFilesConsumer<AttachmentScope>(files => files.Handler<AttachmentOutcomes>());
public sealed class AttachQuote(IFileStore<AttachmentScope> files)
{
public async Task<Result> HandleAsync(Guid fileId, string requisition, CancellationToken ct)
{
var reference = await files.AddReferenceAsync(fileId, requisition, ct);
return reference.IsSuccess
? Result.Success()
: Result.Failure(reference.ErrorMessage!, reference.ErrorCode);
}
}The registration requires the verdict handler: a scope whose owner would never learn what became of its file stops the host at start. IFileOutcomeHandler receives each outcome — Available, Infected, Rejected (with the rejectionCode) and Failed once the automatic passes are spent — after the commit of the verdict, at least once, in the file's tenant and with no user. It must be idempotent by FileOutcome.MessageId, read the file again through its own port and decide by what it finds (a late Available after an Infected ends in Infected), and treat an owner that no longer exists as a normal outcome: release the reference and return.
csharp
public sealed class AttachmentOutcomes(IFileStore<AttachmentScope> files, IAttachments attachments)
: IFileOutcomeHandler
{
public async Task HandleAsync(FileOutcome outcome, CancellationToken ct)
{
var file = await files.GetAsync(outcome.FileId, ct); // the state now, no user needed
if (file.IsFailure) { return; }
if (!await attachments.ExistsAsync(outcome.OwnerKey, outcome.FileId, ct))
{
await files.ReleaseReferenceAsync(outcome.FileId, outcome.TenantId, outcome.OwnerKey, ct);
return;
}
await attachments.RecordAsync(outcome.OwnerKey, file.Data!.Status, ct);
}
}The handler calls its port from its own code, not from a base class of the module: the gate that keeps a port inside its consumer reads the calling assembly. A file without a tenant (a product file) has its verdict applied but cannot yet be notified — the outbox needs a tenant — and gryd_files.scan.verdict_undeliverable counts it.
No signature of IFileStore<TScope> takes a scope, and no module permission is checked behind it: the permissions are policies of the HTTP surface, and the consumer answers for its own caller. A call through a marker that is not the one registered for its scope, or from code that does not reference the marker's assembly (a Type built at run time), is refused with FILE_OWNER_SCOPE_FORBIDDEN before it has any effect. Two markers claiming the same scope stop the host at start.
A registered scope is not reachable by the generic reference routes: they act only on a scope whose consumer asked for it with files.Handler<THandler>().ManagedThroughHttpSurface(). A module that keeps its references through its port never does (RN-GF-23).
Content the server produces itself — a rendered report, for instance — goes through IServerFileStore<TScope>.StoreAsync instead: an explicit tenant, no user required, and the reference the owner expects is created in the same write that releases the file after the scan. This port has no route, and a test keeps it that way.
Next steps
- Configuration — the
GrydFilessection and the profiles - API Reference — every route, error code and permission
- Registered consumers — the scope, the ports and the gate in full
- The verdict contract — what an
IFileOutcomeHandlermust be - Legal holds · Erasure — the control acts
- Audit trail and outbox — what is recorded, and the gap for product files
- Antivirus & Quarantine — running the engine
- Migrating to v10.1 — if you already run the module