Skip to content

feat(inbound): AttachmentDownloader for provider-hosted attachments#29

Open
mpge wants to merge 1 commit intofeat/inbound-email-orchestrationfrom
feat/attachment-downloader
Open

feat(inbound): AttachmentDownloader for provider-hosted attachments#29
mpge wants to merge 1 commit intofeat/inbound-email-orchestrationfrom
feat/attachment-downloader

Conversation

@mpge
Copy link
Copy Markdown
Member

@mpge mpge commented Apr 24, 2026

Summary

Ports the Go reference (escalated-go#35) to .NET. Host apps now have a ready-to-wire worker for the Mailgun-style provider-hosted attachments surfaced in `ProcessResult.PendingAttachmentDownloads` — before this PR those URLs were returned but never fetched.

Design

`AttachmentDownloader`

  • `DownloadAsync(pending, ticketId, replyId?)` → HTTP GET + persist via `IAttachmentStorage` + `Attachment` row on the EF Core context.
  • `DownloadAllAsync` continues past per-attachment failures so a bad URL doesn't block the rest; returns an `AttachmentDownloadResult` per input with either `Persisted` or `Error` set.
  • `AttachmentDownloaderOptions` controls `MaxBytes` size cap (rejects with the `AttachmentTooLargeException` sentinel) and optional `BasicAuth` credentials for providers that gate URLs by API key (Mailgun).
  • `SafeFilename` uses `Path.GetFileName` so `../../etc/passwd` becomes `passwd` — crafted attachment names can't escape the storage root.
  • Response `Content-Type` fallback when the `PendingAttachment`'s own `ContentType` is empty.

`IAttachmentStorage`

  • Minimal contract (`Name` + `PutAsync`). Host apps with S3/Azure/GCS wire their own adapter; the reference `LocalFileAttachmentStorage` writes to local disk with timestamp+ticks filename prefixing so concurrent uploads with the same original name don't collide.

Tests

12 xUnit cases:

  • `DownloadAsync`: happy path (ticket + reply attachable targets), 404 → throw, oversize → `AttachmentTooLargeException`, basic auth header encoding, missing URL guard, response Content-Type fallback.
  • `SafeFilename`: `[Theory]` with `../../etc/passwd`, absolute paths, empty, null, `..` → all neutralized.
  • `DownloadAllAsync`: 3-attachment batch where the middle one fails — other two still persist.
  • `LocalFileAttachmentStorage`: file write, empty-root rejection, unique path per call.

Stacked PR

Based on `feat/inbound-email-orchestration` (#26) so `PendingAttachment` + `ProcessResult` are available. Merge order: #23#24#25#26 → this PR.

Ports the Go reference (escalated-go#35) to .NET. Closes the last
gap in the Phase-5 inbound pipeline: host apps now have a ready-to-wire
worker for Mailgun-style provider-hosted attachments surfaced in
ProcessResult.PendingAttachmentDownloads.

AttachmentDownloader
  - DownloadAsync(pending, ticketId, replyId?) → HTTP GET + persist
    via IAttachmentStorage + Attachment row on the EF Core context.
  - DownloadAllAsync continues past per-attachment failures so a bad
    URL doesn't block the rest; returns AttachmentDownloadResult per
    input (Persisted or Error set).
  - AttachmentDownloaderOptions controls MaxBytes size cap (rejects
    with AttachmentTooLargeException sentinel) + optional BasicAuth
    credentials for providers that gate URLs by API key (Mailgun).
  - SafeFilename uses Path.GetFileName so "../../etc/passwd" becomes
    "passwd" — crafted attachment names can't escape the storage root.
  - Response Content-Type fallback when the PendingAttachment's own
    ContentType is empty.

IAttachmentStorage
  - Minimal contract (Name + PutAsync). Host apps with S3/Azure/GCS
    wire their own adapter; the reference LocalFileAttachmentStorage
    writes to local disk with timestamp+ticks filename prefixing
    so concurrent uploads with the same original name don't collide.

12 xUnit tests cover: happy path (both ticket + reply attachable
targets), 404 → throw + no persist, oversize → AttachmentTooLarge +
no persist, basic auth header encoding, missing URL guard, response
Content-Type fallback, SafeFilename Theory with path-traversal +
edge-case inputs, DownloadAllAsync partial-failure handling, and
LocalFileAttachmentStorage write / empty-root rejection / unique
path per call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant