Path: redb-route/redb.Route/Abstractions/Attachment.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Abstractions/Attachment.cs ---- namespace redb.Route.Abstractions; /// /// One attachment of a message (Apache Camel's Attachment): a named piece of content that travels beside the body, /// such as a file of a multipart/form-data upload, a mail attachment or an MTOM part. Immutable: replace it on the /// message with . /// /// /// The text fields end up in MIME headers when a producer writes the attachment, so a carriage return or line feed in /// any of them is rejected here rather than written onto the wire. /// public sealed class Attachment { private static readonly IReadOnlyDictionary NoHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly string _name = null!; private readonly string? _fileName; private readonly string _contentType = DefaultContentType; private readonly string? _contentId; private readonly IReadOnlyDictionary _headers = NoHeaders; /// Content type of an attachment that does not declare one. public const string DefaultContentType = "application/octet-stream"; /// /// The key the message finds the attachment by: the file name, the form field name or the Content-ID, /// whichever the source of the attachment uses. Names may repeat on one message. /// public required string Name { get => _name; init => _name = RequireText(value, nameof(Name)); } /// Original file name, without a directory, when the attachment came as a file. public string? FileName { get => _fileName; init => _fileName = value is null ? null : RequireText(value, nameof(FileName)); } /// MIME type of the content; when not set. public string ContentType { get => _contentType; init => _contentType = RequireText(value, nameof(ContentType)); } /// MIME Content-ID without the angle brackets, for parts referenced as cid:. public string? ContentId { get => _contentId; init => _contentId = value is null ? null : RequireText(value, nameof(ContentId)); } /// Headers of the MIME part the attachment came from; empty when it did not come from one. public IReadOnlyDictionary Headers { get => _headers; init { ArgumentNullException.ThrowIfNull(value); var copy = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var (key, text) in value) copy[RequireText(key, nameof(Headers))] = RequireText(text, nameof(Headers), allowEmpty: true); _headers = copy; } } /// The content, readable as often as needed. public required AttachmentContent Content { get; init; } /// Length of the content in bytes (attachments[0].length in an expression). public long Length => Content.Length; /// A copy of this attachment with other content (the metadata stays). public Attachment WithContent(AttachmentContent content) { ArgumentNullException.ThrowIfNull(content); return new Attachment { Name = Name, FileName = FileName, ContentType = ContentType, ContentId = ContentId, Headers = Headers, Content = content }; } private static string RequireText(string? value, string field, bool allowEmpty = false) { if (value is null || (!allowEmpty && value.Length == 0)) throw new ArgumentException($"Attachment {field} must not be empty.", field); if (value.AsSpan().IndexOfAny('\r', '\n') >= 0) throw new ArgumentException($"Attachment {field} must not contain a line break.", field); return value; } }